Home | History | Annotate | Download | only in utils
      1 /* Copyright (C) 2007-2009 The Android Open Source Project
      2 **
      3 ** This software is licensed under the terms of the GNU General Public
      4 ** License version 2, as published by the Free Software Foundation, and
      5 ** may be copied, distributed, and modified under those terms.
      6 **
      7 ** This program is distributed in the hope that it will be useful,
      8 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
      9 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     10 ** GNU General Public License for more details.
     11 */
     12 #ifndef _ANDROID_UTILS_PATH_H
     13 #define _ANDROID_UTILS_PATH_H
     14 
     15 #include <android/utils/system.h>
     16 #include <stdint.h>  /* for uint64_t */
     17 
     18 /** MISC FILE AND DIRECTORY HANDLING
     19  **/
     20 
     21 /* O_BINARY is required in the MS C library to avoid opening file
     22  * in text mode (the default, ahhhhh)
     23  */
     24 #if !defined(_WIN32) && !defined(O_BINARY)
     25 #  define  O_BINARY  0
     26 #endif
     27 
     28 /* define  PATH_SEP as a string containing the directory separateor */
     29 #ifdef _WIN32
     30 #  define  PATH_SEP   "\\"
     31 #  define  PATH_SEP_C '\\'
     32 #else
     33 #  define  PATH_SEP   "/"
     34 #  define  PATH_SEP_C '/'
     35 #endif
     36 
     37 /* get MAX_PATH, note that PATH_MAX is set to 260 on Windows for
     38  * stupid backwards-compatibility reason, though any 32-bit version
     39  * of the OS handles much much longer paths
     40  */
     41 #ifdef _WIN32
     42 #  undef   MAX_PATH
     43 #  define  MAX_PATH    1024
     44 #  undef   PATH_MAX
     45 #  define  PATH_MAX    MAX_PATH
     46 #else
     47 #  include <limits.h>
     48 #  define  MAX_PATH    PATH_MAX
     49 #endif
     50 
     51 /* checks that a given file exists */
     52 extern ABool  path_exists( const char*  path );
     53 
     54 /* checks that a path points to a regular file */
     55 extern ABool  path_is_regular( const char*  path );
     56 
     57 /* checks that a path points to a directory */
     58 extern ABool  path_is_dir( const char*  path );
     59 
     60 /* checks that a path is absolute or not */
     61 extern ABool  path_is_absolute( const char*  path );
     62 
     63 /* checks that one can read/write a given (regular) file */
     64 extern ABool  path_can_read( const char*  path );
     65 extern ABool  path_can_write( const char*  path );
     66 
     67 /* checks that one can execute a given file */
     68 extern ABool  path_can_exec( const char* path );
     69 
     70 /* try to make a directory */
     71 extern APosixStatus   path_mkdir( const char*  path, int  mode );
     72 
     73 /* ensure that a given directory exists, create it if not,
     74    0 on success, -1 on error */
     75 extern APosixStatus   path_mkdir_if_needed( const char*  path, int  mode );
     76 
     77 /* return the size of a given file in '*psize'. returns 0 on
     78  * success, -1 on failure (error code in errno) */
     79 extern APosixStatus   path_get_size( const char*  path, uint64_t  *psize );
     80 
     81 /*  path_parent() can be used to return the n-level parent of a given directory
     82  *  this understands . and .. when encountered in the input path.
     83  *
     84  *  the returned string must be freed by the caller.
     85  */
     86 extern char*  path_parent( const char*  path, int  levels );
     87 
     88 /* split a path into a (dirname,basename) pair. the result strings must be freed
     89  * by the caller. Return 0 on success, or -1 on error. Error conditions include
     90  * the following:
     91  *   - 'path' is empty
     92  *   - 'path' is "/" or degenerate cases like "////"
     93  *   - basename is "." or ".."
     94  *
     95  * if there is no directory separator in path, *dirname will be set to "."
     96  * if the path is of type "/foo", then *dirname will be set to "/"
     97  *
     98  * pdirname can be NULL if you don't want the directory name
     99  * pbasename can be NULL if you don't want the base name
    100  */
    101 extern int    path_split( const char*  path, char* *pdirname, char* *pbasename );
    102 
    103 /* a convenience function to retrieve the directory name as returned by
    104  * path_split(). Returns NULL if path_split() returns an error.
    105  * the result string must be freed by the caller
    106  */
    107 extern char*  path_dirname( const char*  path );
    108 
    109 /* a convenience function to retrieve the base name as returned by
    110  * path_split(). Returns NULL if path_split() returns an error.
    111  * the result must be freed by the caller.
    112  */
    113 extern char*  path_basename( const char*  path );
    114 
    115 /* look for a given executable in the system path and return its full path.
    116  * Returns NULL if not found. Note that on Windows this doesn't not append
    117  * an .exe prefix, or other magical thing like Cygwin usually does.
    118  */
    119 extern char*  path_search_exec( const char* filename );
    120 
    121 /* Return the absolute version of a path. If 'path' is already absolute,
    122  * this will be a simple copy. Otherwise, this function will prepend the
    123  * current working directory to the result.
    124  */
    125 extern char*  path_get_absolute( const char* path );
    126 
    127 /** OTHER FILE UTILITIES
    128  **
    129  **  path_empty_file() creates an empty file at a given path location.
    130  **  if the file already exists, it is truncated without warning
    131  **
    132  **  path_copy_file() copies one file into another.
    133  **
    134  **  unlink_file() is equivalent to unlink() on Unix, on Windows,
    135  **  it will handle the case where _unlink() fails because the file is
    136  **  read-only by trying to change its access rights then calling _unlink()
    137  **  again.
    138  **
    139  **  these functions return 0 on success, and -1 on error
    140  **
    141  **  load_text_file() reads a file into a heap-allocated memory block,
    142  **  and appends a 0 to it. the caller must free it
    143  **/
    144 
    145 /* creates an empty file at a given location. If the file already
    146  * exists, it is truncated without warning. returns 0 on success,
    147  * or -1 on failure.
    148  */
    149 extern APosixStatus   path_empty_file( const char*  path );
    150 
    151 /* copies on file into another one. 0 on success, -1 on failure
    152  * (error code in errno). Does not work on directories */
    153 extern APosixStatus   path_copy_file( const char*  dest, const char*  source );
    154 
    155 /* unlink/delete a given file. Note that on Win32, this will
    156  * fail if the program has an opened handle to the file
    157  */
    158 extern APosixStatus   path_delete_file( const char*  path );
    159 
    160 /* try to load a given file into a heap-allocated block.
    161  * if 'pSize' is not NULL, this will set the file's size in '*pSize'
    162  * note that this actually zero-terminates the file for convenience.
    163  * In case of failure, NULL is returned and the error code is in errno
    164  */
    165 extern void*          path_load_file( const char*  path, size_t  *pSize );
    166 
    167 /* */
    168 
    169 #endif /* _ANDROID_UTILS_PATH_H */
    170