<commit_before>/*
 * target.c:  functions which operate on a list of targets supplied to 
 *              a subversion subcommand.
 *
 * ====================================================================
 * Copyright (c) 2000-2001 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/.
 * ====================================================================
 */

/* ==================================================================== */




/*** Includes. ***/

#include "svn_string.h"
#include "svn_pools.h"
#include "svn_error.h"
#include "svn_path.h"
#include "apr_file_info.h"



/*** Code. ***/

svn_error_t *
svn_path_get_absolute(svn_stringbuf_t **pabsolute,
                      const svn_stringbuf_t *relative,
                      apr_pool_t *pool)
{
  char buffer[APR_PATH_MAX];
#ifdef WIN32
  if (_fullpath(buffer, relative->data, APR_PATH_MAX) != NULL)
    {
      *pabsolute = svn_stringbuf_create(buffer, pool);
    }
  else 
    {
      /* TODO: (kevin) Create better error messages, once I learn about
         the errors returned from _fullpath() */
      return svn_error_createf(APR_SUCCESS, SVN_ERR_BAD_FILENAME,
                               NULL, pool, "Could not determine absolute "
                               "path of %s", relative->data);
    }
#else
  if (realpath(relative->data, buffer) != NULL)
    {
      *pabsolute = svn_stringbuf_create(buffer, pool);
    }
  else 
    {
      switch (errno)
        {
        case EACCES:
            return svn_error_createf(APR_SUCCESS, SVN_ERR_RA_NOT_AUTHORIZED,
                                     NULL, pool, "Could not get absolute path "
                                     "for %s, because you lack permissions",
                                     relative->data);
            break;
        case EINVAL: /* FALLTHRU */
        case EIO: /* FALLTHRU */
        case ELOOP: /* FALLTHRU */
        case ENAMETOOLONG: /* FALLTHRU */
        case ENOENT: /* FALLTHRU */
        case ENOTDIR:
            return svn_error_createf(APR_SUCCESS, SVN_ERR_BAD_FILENAME,
                                     NULL, pool, "Could not get absolute path "
                                     "for %s, because it is not a valid file "
                                     "name.", relative->data);
        default:
            return svn_error_createf(APR_SUCCESS, SVN_ERR_BAD_FILENAME,
                                     NULL, pool, "Could not determine if %s "
                                     "is a file or directory.", relative->data);
            break;
        }
    }
#endif
  return SVN_NO_ERROR;
}

svn_error_t *
svn_path_split_if_file(svn_stringbuf_t *path,
                       svn_stringbuf_t **pdirectory,
                       svn_stringbuf_t **pfile,
                       apr_pool_t * pool)
{
  apr_finfo_t finfo;
  apr_status_t apr_err = apr_stat(&finfo, path->data, APR_FINFO_TYPE, pool);
  if (apr_err != APR_SUCCESS)
    {
      return svn_error_createf(apr_err, SVN_ERR_BAD_FILENAME, NULL,
                              pool, "Couldn't determine if %s was a file or "
                              "directory.", path->data);
    }
  else
    {
      if (finfo.filetype == APR_DIR)
        {
          *pdirectory = path;
          *pfile = svn_stringbuf_create("", pool);
        }
      else if (finfo.filetype == APR_REG)
        {
          svn_path_split(path, pdirectory, pfile, svn_path_local_style, pool);
        }
      else 
        {
          return svn_error_createf(APR_SUCCESS, SVN_ERR_BAD_FILENAME, NULL, pool,
                                  "%s is neither a file nor a directory name.",
                                  path->data);
        }
    }
  return SVN_NO_ERROR;
}

svn_error_t *
svn_path_condense_targets (svn_stringbuf_t **pbasedir,
                           apr_array_header_t ** pcondensed_targets,
                           const apr_array_header_t *targets,
                           enum svn_path_style style,
                           apr_pool_t *pool)
{
  if (targets->nelts <=0)
    {
      *pbasedir = NULL;
      if (pcondensed_targets)
        *pcondensed_targets = NULL;
    }
  else
    {
      int i, j, num_condensed = targets->nelts;
      svn_stringbuf_t *file;
      svn_boolean_t *removed
        = apr_pcalloc (pool, (targets->nelts * sizeof (svn_boolean_t)));
      
      /* Copy the targets array, but with absolute paths instead of<commit_msg>Check in a months old change:  Start using apr_filepath_merge, instead of our hacked realpath stuff.  Also use svn_error_create properly.<commit_after>/*
 * target.c:  functions which operate on a list of targets supplied to 
 *              a subversion subcommand.
 *
 * ====================================================================
 * Copyright (c) 2000-2001 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/.
 * ====================================================================
 */

/* ==================================================================== */




/*** Includes. ***/

#include "svn_string.h"
#include "svn_pools.h"
#include "svn_error.h"
#include "svn_path.h"
#include "apr_file_info.h"



/*** Code. ***/

svn_error_t *
svn_path_get_absolute(svn_stringbuf_t **pabsolute,
                      const svn_stringbuf_t *relative,
                      apr_pool_t *pool)
{
  /* Why do I get an warning if I just use &real_buffer, in the call to 
     apr_filepath_merge?? */
  char real_buffer[APR_PATH_MAX];
  char * buffer = real_buffer;
  int apr_err = apr_filepath_merge(&buffer, NULL, relative->data,
                                   APR_FILEPATH_NOTRELATIVE &
                                   APR_FILEPATH_NATIVE & 
                                   APR_FILEPATH_TRUENAME, pool);
  if (apr_err == APR_SUCCESS)
    {
      *pabsolute = svn_stringbuf_create(buffer, pool);
      return SVN_NO_ERROR;
    }
  else
    {
      return svn_error_createf(SVN_ERR_BAD_FILENAME, apr_err, NULL, pool,
                               "Couldn't determine absolute path of %s.", 
                               relative->data);
    }
}

svn_error_t *
svn_path_split_if_file(svn_stringbuf_t *path,
                       svn_stringbuf_t **pdirectory,
                       svn_stringbuf_t **pfile,
                       apr_pool_t * pool)
{
  apr_finfo_t finfo;
  apr_status_t apr_err = apr_stat(&finfo, path->data, APR_FINFO_TYPE, pool);
  if (apr_err != APR_SUCCESS)
    {
      return svn_error_createf(SVN_ERR_BAD_FILENAME, apr_err, NULL,
                              pool, "Couldn't determine if %s was a file or "
                              "directory.", path->data);
    }
  else
    {
      if (finfo.filetype == APR_DIR)
        {
          *pdirectory = path;
          *pfile = svn_stringbuf_create("", pool);
        }
      else if (finfo.filetype == APR_REG)
        {
          svn_path_split(path, pdirectory, pfile, svn_path_local_style, pool);
        }
      else 
        {
          return svn_error_createf(SVN_ERR_BAD_FILENAME, 0, NULL, pool,
                                  "%s is neither a file nor a directory name.",
                                  path->data);
        }
    }
  return SVN_NO_ERROR;
}

svn_error_t *
svn_path_condense_targets (svn_stringbuf_t **pbasedir,
                           apr_array_header_t ** pcondensed_targets,
                           const apr_array_header_t *targets,
                           enum svn_path_style style,
                           apr_pool_t *pool)
{
  if (targets->nelts <=0)
    {
      *pbasedir = NULL;
      if (pcondensed_targets)
        *pcondensed_targets = NULL;
    }
  else
    {
      int i, j, num_condensed = targets->nelts;
      svn_stringbuf_t *file;
      svn_boolean_t *removed
        = apr_pcalloc (pool, (targets->nelts * sizeof (svn_boolean_t)));
      
      /* Copy the targets array, but with absolute paths instead of
{'fraction_non_alphanumeric': 0.12952316537030775, 'fraction_numerical': 0.002592717844662383, 'mean_word_length': 1.9811827956989247, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 23, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 18, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4869343', 'n_tokens_mistral': 2717, 'n_tokens_neox': 2613, 'n_words': 659}