<commit_before>/*
 * diff_file.c :  routines for doing diffs on files
 *
 * ====================================================================
 * Copyright (c) 2000-2003 CollabNet.  All rights reserved.
 *
 * This software is licensed as described in the file COPYING, which
 * you should have received as part of this distribution.  The terms
 * are also available at http://subversion.tigris.org/license-1.html.
 * If newer versions of this license are posted there, you may use a
 * newer version instead, at your option.
 *
 * This software consists of voluntary contributions made by many
 * individuals.  For exact contribution history, see the revision
 * history and logs, available at http://subversion.tigris.org/.
 * ====================================================================
 */


#include <apr.h>
#include <apr_pools.h>
#include <apr_general.h>
#include <apr_md5.h>
#include <apr_file_io.h>
#include <apr_file_info.h>
#include <apr_time.h>
#include <apr_mmap.h>

#include "svn_error.h"
#include "svn_diff.h"
#include "svn_types.h"
#include "svn_string.h"
#include "svn_io.h"
#include "svn_pools.h"


typedef struct svn_diff__file_token_t
{
  apr_off_t   length;
  const char *line;
} svn_diff__file_token_t;


typedef struct svn_diff__file_baton_t
{
  const char *path[3];

  char *buffer[3];
  char *curp[3];
  char *endp[3];

  svn_diff__file_token_t *token;
  svn_boolean_t reuse_token;

  apr_pool_t *pool;
} svn_diff__file_baton_t;


static
int
svn_diff__file_datasource_to_index(svn_diff_datasource_e datasource)
{
  switch (datasource)
    {
    case svn_diff_datasource_original:
      return 0;

    case svn_diff_datasource_modified:
      return 1;

    case svn_diff_datasource_latest:
      return 2;
    }

  return -1;
}

static
svn_error_t *
svn_diff__file_datasource_open(void *baton,
                               svn_diff_datasource_e datasource)
{
  svn_diff__file_baton_t *file_baton = baton;
  apr_file_t *file;
  apr_finfo_t finfo;
  apr_status_t rv;
  int idx;

  idx = svn_diff__file_datasource_to_index(datasource);

  SVN_ERR(svn_io_file_open(&file, file_baton->path[idx],
                           APR_READ, APR_OS_DEFAULT, file_baton->pool));

  rv = apr_file_info_get(&finfo, APR_FINFO_SIZE, file);
  if (rv != APR_SUCCESS)
    {
      return svn_error_createf(rv, NULL, "Failed to get file info '%s'.",
                               file_baton->path[idx]);
    }

#if APR_HAS_MMAP
  {
    apr_mmap_t *mm;

    rv = apr_mmap_create(&mm, file,
                         0, finfo.size, APR_MMAP_READ,
                         file_baton->pool);
    if (rv != APR_SUCCESS)
      {
        return svn_error_createf(rv, NULL, "Failed to mmap file '%s'.",
                                 file_baton->path[idx]);
      }

    file_baton->buffer[idx] = mm->mm;
  }

#else /* APR_HAS_MMAP */
  file_baton->buffer[idx] = apr_palloc(file_baton->pool, finfo.size);
  rv = apr_file_read_full(file, file_baton->buffer[idx], finfo.size, NULL);
  if (rv != APR_SUCCESS)
    {
      return svn_error_createf(rv, NULL, "Failed to read file '%s'.",
                               file_baton->path[idx]);
    }

  rv = apr_file_close(file);
  if (rv != APR_SUCCESS)
    {
      return svn_error_createf(rv, NULL, "failed to close file '%s'.",
                               file_baton->path[idx]);
    }
#endif /* APR_HAS_MMAP */

  file_baton->curp[idx] = file_baton->buffer[idx];
  file_baton->endp[idx] = file_baton->buffer[idx] + finfo.size;

  return NULL;
}<commit_msg>* subversion/libsvn_delta/diff_file.c<commit_after>/*
 * diff_file.c :  routines for doing diffs on files
 *
 * ====================================================================
 * Copyright (c) 2000-2003 CollabNet.  All rights reserved.
 *
 * This software is licensed as described in the file COPYING, which
 * you should have received as part of this distribution.  The terms
 * are also available at http://subversion.tigris.org/license-1.html.
 * If newer versions of this license are posted there, you may use a
 * newer version instead, at your option.
 *
 * This software consists of voluntary contributions made by many
 * individuals.  For exact contribution history, see the revision
 * history and logs, available at http://subversion.tigris.org/.
 * ====================================================================
 */


#include <apr.h>
#include <apr_pools.h>
#include <apr_general.h>
#include <apr_md5.h>
#include <apr_file_io.h>
#include <apr_file_info.h>
#include <apr_time.h>
#include <apr_mmap.h>

#include "svn_error.h"
#include "svn_diff.h"
#include "svn_types.h"
#include "svn_string.h"
#include "svn_io.h"
#include "svn_pools.h"


typedef struct svn_diff__file_token_t
{
  apr_off_t   length;
  const char *line;
} svn_diff__file_token_t;


typedef struct svn_diff__file_baton_t
{
  const char *path[3];

  char *buffer[3];
  char *curp[3];
  char *endp[3];

  svn_diff__file_token_t *token;
  svn_boolean_t reuse_token;

  apr_pool_t *pool;
} svn_diff__file_baton_t;


static
int
svn_diff__file_datasource_to_index(svn_diff_datasource_e datasource)
{
  switch (datasource)
    {
    case svn_diff_datasource_original:
      return 0;

    case svn_diff_datasource_modified:
      return 1;

    case svn_diff_datasource_latest:
      return 2;
    }

  return -1;
}

static
svn_error_t *
svn_diff__file_datasource_open(void *baton,
                               svn_diff_datasource_e datasource)
{
  svn_diff__file_baton_t *file_baton = baton;
  apr_file_t *file;
  apr_finfo_t finfo;
  apr_status_t rv;
  int idx;

  idx = svn_diff__file_datasource_to_index(datasource);

  SVN_ERR(svn_io_file_open(&file, file_baton->path[idx],
                           APR_READ, APR_OS_DEFAULT, file_baton->pool));

  rv = apr_file_info_get(&finfo, APR_FINFO_SIZE, file);
  if (rv != APR_SUCCESS)
    {
      return svn_error_createf(rv, NULL, "Failed to get file info '%s'.",
                               file_baton->path[idx]);
    }

  if (finfo.size == 0)
    {
      file_baton->buffer[idx] = NULL;
    }

#if APR_HAS_MMAP
  else
    {
      apr_mmap_t *mm;

      rv = apr_mmap_create(&mm, file,
                           0, finfo.size, APR_MMAP_READ,
                           file_baton->pool);
      if (rv != APR_SUCCESS)
        {
          return svn_error_createf(rv, NULL, "Failed to mmap file '%s'.",
                                   file_baton->path[idx]);
        }

      file_baton->buffer[idx] = mm->mm;
    }

#else /* APR_HAS_MMAP */
  else
    {
      file_baton->buffer[idx] = apr_palloc(file_baton->pool, finfo.size);
      rv = apr_file_read_full(file, file_baton->buffer[idx], finfo.size, NULL);
      if (rv != APR_SUCCESS)
        {
          return svn_error_createf(rv, NULL, "Failed to read file '%s'.",
                                   file_baton->path[idx]);
        }
    }

  rv = apr_file_close(file);
  if (rv != APR_SUCCESS)
    {
      return svn_error_createf(rv, NULL, "failed to close file '%s'.",
                               file_baton->path[idx]);
    }
#endif /* APR_HAS_MMAP */

  file_baton->curp[idx] = file_baton->buffer[idx];
  file_baton->endp[idx] = file_baton->buffer[idx] + finfo.size;

  return NULL;
}
{'fraction_non_alphanumeric': 0.14371008053318524, 'fraction_numerical': 0.005415162454873646, 'mean_word_length': 2.986164914222468, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 52, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 3, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2746540', 'n_tokens_mistral': 2678, 'n_tokens_neox': 2560, 'n_words': 550}