root/trunk/src/z-file.h
| Revision 918, 6.1 kB (checked in by takkaria, 6 months ago) | |
|---|---|
| |
| Line | |
|---|---|
| 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 | /*** Byte-flipping functions ***/ |
| 45 | |
| 46 | /** |
| 47 | * "Flip" the bits of the integer specified in `arg` to make them big-endian. |
| 48 | * Useful when writing to files intended to be portable across systems. |
| 49 | * |
| 50 | * Returns the flipped value, or the original if the current system is already |
| 51 | * big-endian. |
| 52 | */ |
| 53 | u16b flip_u16b(u16b arg); |
| 54 | u32b flip_u32b(u32b arg); |
| 55 | |
| 56 | |
| 57 | |
| 58 | /*** File access code ***/ |
| 59 | |
| 60 | /** Data types **/ |
| 61 | |
| 62 | /** |
| 63 | * An opaque file handle for Angband file handling. |
| 64 | */ |
| 65 | typedef struct ang_file ang_file; |
| 66 | |
| 67 | /** |
| 68 | * Specifies what kind of access is required to a file. See file_open(). |
| 69 | */ |
| 70 | typedef enum |
| 71 | { |
| 72 | MODE_WRITE, |
| 73 | MODE_READ, |
| 74 | MODE_APPEND |
| 75 | } file_mode; |
| 76 | |
| 77 | /** |
| 78 | * Specifies what kind of thing a file is, when writing. See file_open(). |
| 79 | */ |
| 80 | typedef enum |
| 81 | { |
| 82 | FTYPE_TEXT = 1, |
| 83 | FTYPE_SAVE, |
| 84 | FTYPE_RAW, |
| 85 | FTYPE_HTML |
| 86 | } file_type; |
| 87 | |
| 88 | |
| 89 | /** Utility functions **/ |
| 90 | |
| 91 | /** |
| 92 | * Returns TRUE if `fname` exists (and is a file), FALSE otherwise. |
| 93 | */ |
| 94 | bool file_exists(const char *fname); |
| 95 | |
| 96 | /** |
| 97 | * Tries to delete `fname`. |
| 98 | * |
| 99 | * Returns TRUE if successful, FALSE otherwise. |
| 100 | */ |
| 101 | bool file_delete(const char *fname); |
| 102 | |
| 103 | /** |
| 104 | * Moves the file `fname` to `newname`. |
| 105 | * |
| 106 | * Returns TRUE if successful, FALSE otherwise. |
| 107 | */ |
| 108 | bool file_move(const char *fname, const char *newname); |
| 109 | |
| 110 | /** |
| 111 | * Returns TRUE if the file `first` is newer than `second`. |
| 112 | */ |
| 113 | bool file_newer(const char *first, const char *second); |
| 114 | |
| 115 | |
| 116 | /** File handle creation **/ |
| 117 | |
| 118 | /** |
| 119 | * Open file `buf`, returning a a file handling representing that file. |
| 120 | * |
| 121 | * The file mode specifies what kind of access is required to the file: |
| 122 | * - MODE_WRITE will overwrite the current contents of the file |
| 123 | * - MODE_READ will allow read-only access to the file |
| 124 | * - MODE_APPEND will allow write-only access, but will not overwrite the |
| 125 | * current contents of the file. |
| 126 | * |
| 127 | * The file type is specified to allow systems which don't use file extensions |
| 128 | * to set the type of the file appropriately. When reading, pass -1 as ftype; |
| 129 | * when writing, use whichever filetype seems most appropriate. |
| 130 | * |
| 131 | * On any kind of error, this function returns NULL. |
| 132 | */ |
| 133 | ang_file *file_open(const char *buf, file_mode mode, file_type ftype); |
| 134 | |
| 135 | /** |
| 136 | * Attempt to close the file handle `f`. |
| 137 | * |
| 138 | * Returns TRUE if successful, FALSE otherwise. |
| 139 | */ |
| 140 | bool file_close(ang_file *f); |
| 141 | |
| 142 | |
| 143 | /** File locking **/ |
| 144 | |
| 145 | /** |
| 146 | * Lock or unlock the file represented by `f` for writing. |
| 147 | * If the file is not open for writing, this call will fail. |
| 148 | * |
| 149 | * If `f` is closed, the file is automatically unlocked. |
| 150 | */ |
| 151 | void file_lock(ang_file *f); |
| 152 | void file_unlock(ang_file *f); |
| 153 | |
| 154 | |
| 155 | /** Line-based IO **/ |
| 156 | |
| 157 | /** |
| 158 | * Get a line of text from the file represented by `f`, placing it into `buf` |
| 159 | * to a maximum length of `n`. |
| 160 | * |
| 161 | * This expands tabs, replaces non-printables with '?', and deals with differing |
| 162 | * line endings. |
| 163 | * |
| 164 | * Returns TRUE when data is returned; FALSE otherwise. |
| 165 | */ |
| 166 | bool file_getl(ang_file *f, char *buf, size_t n); |
| 167 | |
| 168 | /** |
| 169 | * Write the string pointed to by `buf` to the file represented by `f`. |
| 170 | * |
| 171 | * Returns TRUE if successful, FALSE otherwise. |
| 172 | */ |
| 173 | bool file_put(ang_file *f, const char *buf); |
| 174 | |
| 175 | /** |
| 176 | * Format (using strnfmt) the given args, and then call file_put(). |
| 177 | */ |
| 178 | bool file_putf(ang_file *f, const char *fmt, ...); |
| 179 | |
| 180 | |
| 181 | /** Byte-based IO */ |
| 182 | |
| 183 | /** |
| 184 | * Seek to position `pos` in the file represented by `f`. |
| 185 | * |
| 186 | * Returns TRUE if successful, FALSE otherwise. |
| 187 | */ |
| 188 | bool file_seek(ang_file *f, u32b pos); |
| 189 | |
| 190 | /** |
| 191 | * Reads `n` bytes from the file represented by `f` into the buffer `buf`. |
| 192 | * Do not mix with calls to file_readc(). |
| 193 | * |
| 194 | * Returns the number of bytes read. |
| 195 | */ |
| 196 | size_t file_read(ang_file *f, char *buf, size_t n); |
| 197 | |
| 198 | /** |
| 199 | * Write the first `n` bytes following the pointer `buf` to the file represented |
| 200 | * by `f`. Do not mix with calls to file_writec(). |
| 201 | * |
| 202 | * Returns TRUE if successful, FALSE otherwise. |
| 203 | */ |
| 204 | bool file_write(ang_file *f, const char *buf, size_t n); |
| 205 | |
| 206 | /** |
| 207 | * Read a byte from the file represented by `f` and place it at the location |
| 208 | * specified by 'b'. |
| 209 | * |
| 210 | * Returns TRUE if successful, FALSE otherwise. |
| 211 | */ |
| 212 | bool file_readc(ang_file *f, byte *b); |
| 213 | |
| 214 | /** |
| 215 | * Write the byte `b` to the file represented by `f`. |
| 216 | * |
| 217 | * Returns TRUE if successful, FALSE otherwise. |
| 218 | */ |
| 219 | bool file_writec(ang_file *f, byte b); |
| 220 | |
| 221 | |
| 222 | |
| 223 | /*** Directory code ***/ |
| 224 | |
| 225 | /** |
| 226 | * An opaque file handle for Angband directory handling. |
| 227 | */ |
| 228 | typedef struct ang_dir ang_dir; |
| 229 | |
| 230 | |
| 231 | /** |
| 232 | * Opens a directory handle. |
| 233 | * |
| 234 | * `dirname` must be a system-specific pathname to the directory |
| 235 | * you want scanned. |
| 236 | * |
| 237 | * Returns a valid directory handle on success, NULL otherwise. |
| 238 | */ |
| 239 | ang_dir *my_dopen(const char *dirname); |
| 240 | |
| 241 | /** |
| 242 | * Reads a directory entry. |
| 243 | * |
| 244 | * `dir` must point to a directory handle previously returned by my_dopen(). |
| 245 | * `fname` must be a pointer to a writeable chunk of memory `len` long. |
| 246 | * |
| 247 | * Returns TRUE on successful reading, FALSE otherwise. |
| 248 | * (FALSE generally indicates that there are no more files to be read.) |
| 249 | */ |
| 250 | bool my_dread(ang_dir *dir, char *fname, size_t len); |
| 251 | |
| 252 | /** |
| 253 | * Close a directory handle. |
| 254 | */ |
| 255 | void my_dclose(ang_dir *dir); |
| 256 | |
| 257 | #endif |
Note: See TracBrowser for help on using the browser.
