Line data Source code
1 : #ifndef HEADER_fd_src_app_platform_fd_file_util_h 2 : #define HEADER_fd_src_app_platform_fd_file_util_h 3 : 4 : #include "../../util/fd_util.h" 5 : 6 : /* Read a uint from the provided file path. On success, returns zero 7 : and writes the value to the provided pointer. On failure, -1 is 8 : returned and errno is set appropriately. 9 : 10 : If the file does not start with a single line, with a uint followed 11 : by EOF or a newline character, it is an error and the errno will 12 : be ERANGE. */ 13 : 14 : int 15 : fd_file_util_read_ulong( char const * path, 16 : ulong * value ); 17 : 18 : int 19 : fd_file_util_read_uint( char const * path, 20 : uint * value ); 21 : 22 : /* Write a uint to the provided file path. On success, returns zero. 23 : On failure, -1 is returned and errno is set appropriately. */ 24 : 25 : int 26 : fd_file_util_write_ulong( char const * path, 27 : ulong value ); 28 : 29 : static inline int 30 : fd_file_util_write_uint( char const * path, 31 0 : uint value ) { 32 0 : return fd_file_util_write_ulong( path, value ); 33 0 : } 34 : 35 : /* fd_file_util_write_cstr writes a string without creating or truncating 36 : the file. Returns 0 on success or -1 with errno set on failure */ 37 : int 38 : fd_file_util_write_cstr( char const * path, 39 : char const * value ); 40 : 41 : /* fd_file_util_mkdir_all() recursively creates directories such that 42 : the full path provided can exist. Directories that already exist are 43 : left as they are, and new directories that are created will be owned 44 : by the given uid and gid and have mode 0700 (rwx for the owner only). 45 : 46 : On success, returns zero. On failure, returns -1 and errno is set 47 : appropriately. Reasons for failure include all of the reasons from 48 : mkdir(2), chown(2) and chmod(2). 49 : 50 : On failure, it is possible for a partial directory structure to have 51 : been created, and this will not be cleaned up. A directory might 52 : have been created, but failed to be chown() or chmod() in which case 53 : it will be left with a different owner. */ 54 : 55 : int 56 : fd_file_util_mkdir_all( const char * path, 57 : uint uid, 58 : uint gid, 59 : int is_dir ); 60 : 61 : /* fd_file_util_rmtree() recursively removes all the contents of a 62 : directory, and then (if remove_root is non-zero) also removes the 63 : directory itself. If remove_root is zero, the directory is left 64 : empty but not removed. 65 : 66 : On success, returns zero. On failure, returns -1 and errno is set 67 : appropriately. 68 : 69 : On failure, the directory may be in any state, with some files and 70 : directories being deleted, and some not. */ 71 : 72 : int 73 : fd_file_util_rmtree( char const * path, 74 : int remove_root ); 75 : 76 : /* fd_file_util_self_exe() retrieves the full path of the current 77 : executable into the path provided. Path should be a buffer with at 78 : least PATH_MAX elements. 79 : 80 : On success, the path is written to the provided buffer and zero is 81 : returned. On failure, -1 is returned and errno is set appropriately. */ 82 : 83 : int 84 : fd_file_util_self_exe( char path[ PATH_MAX ] ); 85 : 86 : /* fd_file_util_read_all() reads all the file contents from the provided 87 : path into a newly `mmap(2)`ed region. Returns MAP_FAILED on failure. 88 : The caller is responsible for unmapping the region when done. 89 : On success, out_sz will be set to the file size. Otherwise, the 90 : value of out_sz is undefined. */ 91 : 92 : char * 93 : fd_file_util_read_all( char const * path, 94 : ulong * out_sz ); 95 : 96 : /* fd_file_util_read_cstr() reads all the file contents from path into 97 : dst, and then zero-terminates dst, and sets *dst_len. Returns NULL 98 : on failure (e.g. read error or insufficient dst_max). In case of 99 : error, *dst and *dst_len are undefined. */ 100 : 101 : char * 102 : fd_file_util_read_cstr( char const * path, 103 : char * dst, 104 : ulong dst_max, 105 : ulong * dst_len ); 106 : 107 : #endif /* HEADER_fd_src_app_platform_fd_file_util_h */