| 1 | #ifndef INCLUDED_Z_FILE_H |
|---|
| 2 | #define INCLUDED_Z_FILE_H |
|---|
| 3 | |
|---|
| 4 | #include "h-basic.h" |
|---|
| 5 | |
|---|
| 6 | /*** Permissions code ***/ |
|---|
| 7 | |
|---|
| 8 | /** |
|---|
| 9 | * Player's user ID and group ID, respectively. |
|---|
| 10 | * |
|---|
| 11 | * Only relevant to POSIX systems that use main.c, and set there. |
|---|
| 12 | */ |
|---|
| 13 | extern int player_uid; |
|---|
| 14 | extern int player_egid; |
|---|
| 15 | |
|---|
| 16 | /** |
|---|
| 17 | * Drop or grab privileges. |
|---|
| 18 | * |
|---|
| 19 | * This is used on multiuser systems, where the game wants to gain access to |
|---|
| 20 | * system-wide files like the scores, raw files, or savefiles. Reading from |
|---|
| 21 | * these locations is permitted by anyone, but writing to them requires a call |
|---|
| 22 | * to safe_setuid_grab() before opening the file for writing. |
|---|
| 23 | * |
|---|
| 24 | * safe_setuid_drop() should be called immediately after the file has been |
|---|
| 25 | * opened, to prevent security risks, and restores the game's rights so that it |
|---|
| 26 | * cannot write to the system-wide files. |
|---|
| 27 | */ |
|---|
| 28 | void safe_setuid_grab(void); |
|---|
| 29 | void safe_setuid_drop(void); |
|---|
| 30 | |
|---|
| 31 | |
|---|
| 32 | /*** Path building code ***/ |
|---|
| 33 | |
|---|
| 34 | /** |
|---|
| 35 | * Concatenates "leaf" onto the end of "base", using system-specific path |
|---|
| 36 | * separators, and places the result in buf[], truncated to "len" bytes. |
|---|
| 37 | * |
|---|
| 38 | * On Unixes, deals with the tilde as representing home directories. |
|---|
| 39 | */ |
|---|
| 40 | size_t path_build(char *buf, size_t len, const char *base, const char *leaf); |
|---|
| 41 | |
|---|
| 42 | |
|---|
| 43 | |
|---|
| 44 | /*** File access code ***/ |
|---|
| 45 | |
|---|
| 46 | /** Data types **/ |
|---|
| 47 | |
|---|
| 48 | /** |
|---|
| 49 | * An opaque file handle for Angband file handling. |
|---|
| 50 | */ |
|---|
| 51 | typedef struct ang_file ang_file; |
|---|
| 52 | |
|---|
| 53 | /** |
|---|
| 54 | * Specifies what kind of access is required to a file. See file_open(). |
|---|
| 55 | */ |
|---|
| 56 | typedef enum |
|---|
| 57 | { |
|---|
| 58 | MODE_WRITE, |
|---|
| 59 | MODE_READ, |
|---|
| 60 | MODE_APPEND |
|---|
| 61 | } file_mode; |
|---|
| 62 | |
|---|
| 63 | /** |
|---|
| 64 | * Specifies what kind of thing a file is, when writing. See file_open(). |
|---|
| 65 | */ |
|---|
| 66 | typedef enum |
|---|
| 67 | { |
|---|
| 68 | FTYPE_TEXT = 1, |
|---|
| 69 | FTYPE_SAVE, |
|---|
| 70 | FTYPE_RAW, |
|---|
| 71 | FTYPE_HTML |
|---|
| 72 | } file_type; |
|---|
| 73 | |
|---|
| 74 | |
|---|
| 75 | /** Utility functions **/ |
|---|
| 76 | |
|---|
| 77 | /** |
|---|
| 78 | * Returns TRUE if `fname` exists (and is a file), FALSE otherwise. |
|---|
| 79 | */ |
|---|
| 80 | bool file_exists(const char *fname); |
|---|
| 81 | |
|---|
| 82 | /** |
|---|
| 83 | * Tries to delete `fname`. |
|---|
| 84 | * |
|---|
| 85 | * Returns TRUE if successful, FALSE otherwise. |
|---|
| 86 | */ |
|---|
| 87 | bool file_delete(const char *fname); |
|---|
| 88 | |
|---|
| 89 | /** |
|---|
| 90 | * Moves the file `fname` to `newname`. |
|---|
| 91 | * |
|---|
| 92 | * Returns TRUE if successful, FALSE otherwise. |
|---|
| 93 | */ |
|---|
| 94 | bool file_move(const char *fname, const char *newname); |
|---|
| 95 | |
|---|
| 96 | /** |
|---|
| 97 | * Returns TRUE if the file `first` is newer than `second`. |
|---|
| 98 | */ |
|---|
| 99 | bool file_newer(const char *first, const char *second); |
|---|
| 100 | |
|---|
| 101 | |
|---|
| 102 | /** File handle creation **/ |
|---|
| 103 | |
|---|
| 104 | /** |
|---|
| 105 | * Open file `buf`, returning a a file handling representing that file. |
|---|
| 106 | * |
|---|
| 107 | * The file mode specifies what kind of access is required to the file: |
|---|
| 108 | * - MODE_WRITE will overwrite the current contents of the file |
|---|
| 109 | * - MODE_READ will allow read-only access to the file |
|---|
| 110 | * - MODE_APPEND will allow write-only access, but will not overwrite the |
|---|
| 111 | * current contents of the file. |
|---|
| 112 | * |
|---|
| 113 | * The file type is specified to allow systems which don't use file extensions |
|---|
| 114 | * to set the type of the file appropriately. When reading, pass -1 as ftype; |
|---|
| 115 | * when writing, use whichever filetype seems most appropriate. |
|---|
| 116 | * |
|---|
| 117 | * On any kind of error, this function returns NULL. |
|---|
| 118 | */ |
|---|
| 119 | ang_file *file_open(const char *buf, file_mode mode, file_type ftype); |
|---|
| 120 | |
|---|
| 121 | /** |
|---|
| 122 | * Attempt to close the file handle `f`. |
|---|
| 123 | * |
|---|
| 124 | * Returns TRUE if successful, FALSE otherwise. |
|---|
| 125 | */ |
|---|
| 126 | bool file_close(ang_file *f); |
|---|
| 127 | |
|---|
| 128 | |
|---|
| 129 | /** File locking **/ |
|---|
| 130 | |
|---|
| 131 | /** |
|---|
| 132 | * Lock or unlock the file represented by `f` for writing. |
|---|
| 133 | * If the file is not open for writing, this call will fail. |
|---|
| 134 | * |
|---|
| 135 | * If `f` is closed, the file is automatically unlocked. |
|---|
| 136 | */ |
|---|
| 137 | void file_lock(ang_file *f); |
|---|
| 138 | void file_unlock(ang_file *f); |
|---|
| 139 | |
|---|
| 140 | |
|---|
| 141 | /** Line-based IO **/ |
|---|
| 142 | |
|---|
| 143 | /** |
|---|
| 144 | * Get a line of text from the file represented by `f`, placing it into `buf` |
|---|
| 145 | * to a maximum length of `n`. |
|---|
| 146 | * |
|---|
| 147 | * This expands tabs, replaces non-printables with '?', and deals with differing |
|---|
| 148 | * line endings. |
|---|
| 149 | * |
|---|
| 150 | * Returns TRUE when data is returned; FALSE otherwise. |
|---|
| 151 | */ |
|---|
| 152 | bool file_getl(ang_file *f, char *buf, size_t n); |
|---|
| 153 | |
|---|
| 154 | /** |
|---|
| 155 | * Write the string pointed to by `buf` to the file represented by `f`. |
|---|
| 156 | * |
|---|
| 157 | * Returns TRUE if successful, FALSE otherwise. |
|---|
| 158 | */ |
|---|
| 159 | bool file_put(ang_file *f, const char *buf); |
|---|
| 160 | |
|---|
| 161 | /** |
|---|
| 162 | * Format (using strnfmt) the given args, and then call file_put(). |
|---|
| 163 | */ |
|---|
| 164 | bool file_putf(ang_file *f, const char *fmt, ...); |
|---|
| 165 | |
|---|
| 166 | |
|---|
| 167 | /** Byte-based IO */ |
|---|
| 168 | |
|---|
| 169 | /** |
|---|
| 170 | * Seek to position `pos` in the file represented by `f`. |
|---|
| 171 | * |
|---|
| 172 | * Returns TRUE if successful, FALSE otherwise. |
|---|
| 173 | */ |
|---|
| 174 | bool file_seek(ang_file *f, u32b pos); |
|---|
| 175 | |
|---|
| 176 | /** |
|---|
| 177 | * Reads n bytes from file 'f' info buffer 'buf'. |
|---|
| 178 | * \returns Number of bytes read; -1 on error |
|---|
| 179 | */ |
|---|
| 180 | int file_read(ang_file *f, char *buf, size_t n); |
|---|
| 181 | |
|---|
| 182 | /** |
|---|
| 183 | * Write the first `n` bytes following the pointer `buf` to the file represented |
|---|
| 184 | * by `f`. Do not mix with calls to file_writec(). |
|---|
| 185 | * |
|---|
| 186 | * Returns TRUE if successful, FALSE otherwise. |
|---|
| 187 | */ |
|---|
| 188 | bool file_write(ang_file *f, const char *buf, size_t n); |
|---|
| 189 | |
|---|
| 190 | /** |
|---|
| 191 | * Read a byte from the file represented by `f` and place it at the location |
|---|
| 192 | * specified by 'b'. |
|---|
| 193 | * |
|---|
| 194 | * Returns TRUE if successful, FALSE otherwise. |
|---|
| 195 | */ |
|---|
| 196 | bool file_readc(ang_file *f, byte *b); |
|---|
| 197 | |
|---|
| 198 | /** |
|---|
| 199 | * Write the byte `b` to the file represented by `f`. |
|---|
| 200 | * |
|---|
| 201 | * Returns TRUE if successful, FALSE otherwise. |
|---|
| 202 | */ |
|---|
| 203 | bool file_writec(ang_file *f, byte b); |
|---|
| 204 | |
|---|
| 205 | |
|---|
| 206 | |
|---|
| 207 | /*** Directory code ***/ |
|---|
| 208 | |
|---|
| 209 | /** |
|---|
| 210 | * Return whether or not a directory exists. |
|---|
| 211 | */ |
|---|
| 212 | bool dir_exists(const char *dirname); |
|---|
| 213 | |
|---|
| 214 | /** |
|---|
| 215 | * Create's the given directory, creating intermediate directories if |
|---|
| 216 | * needed and possible. Returns whether or not the directory was created |
|---|
| 217 | * successfully. |
|---|
| 218 | */ |
|---|
| 219 | bool dir_create(const char *dirname); |
|---|
| 220 | |
|---|
| 221 | /** |
|---|
| 222 | * An opaque file handle for Angband directory handling. |
|---|
| 223 | */ |
|---|
| 224 | typedef struct ang_dir ang_dir; |
|---|
| 225 | |
|---|
| 226 | |
|---|
| 227 | /** |
|---|
| 228 | * Opens a directory handle. |
|---|
| 229 | * |
|---|
| 230 | * `dirname` must be a system-specific pathname to the directory |
|---|
| 231 | * you want scanned. |
|---|
| 232 | * |
|---|
| 233 | * Returns a valid directory handle on success, NULL otherwise. |
|---|
| 234 | */ |
|---|
| 235 | ang_dir *my_dopen(const char *dirname); |
|---|
| 236 | |
|---|
| 237 | /** |
|---|
| 238 | * Reads a directory entry. |
|---|
| 239 | * |
|---|
| 240 | * `dir` must point to a directory handle previously returned by my_dopen(). |
|---|
| 241 | * `fname` must be a pointer to a writeable chunk of memory `len` long. |
|---|
| 242 | * |
|---|
| 243 | * Returns TRUE on successful reading, FALSE otherwise. |
|---|
| 244 | * (FALSE generally indicates that there are no more files to be read.) |
|---|
| 245 | */ |
|---|
| 246 | bool my_dread(ang_dir *dir, char *fname, size_t len); |
|---|
| 247 | |
|---|
| 248 | /** |
|---|
| 249 | * Close a directory handle. |
|---|
| 250 | */ |
|---|
| 251 | void my_dclose(ang_dir *dir); |
|---|
| 252 | |
|---|
| 253 | #endif |
|---|