|
| 1 | +/* |
| 2 | + * This file is free software; you can redistribute it and/or modify |
| 3 | + * it under the terms of the GNU General Public License, version 2, |
| 4 | + * as published by the Free Software Foundation. |
| 5 | + * |
| 6 | + * In addition to the permissions in the GNU General Public License, |
| 7 | + * the authors give you unlimited permission to link the compiled |
| 8 | + * version of this file into combinations with other programs, |
| 9 | + * and to distribute those combinations without any restriction |
| 10 | + * coming from the use of this file. (The General Public License |
| 11 | + * restrictions do apply in other respects; for example, they cover |
| 12 | + * modification of the file, and distribution when not linked into |
| 13 | + * a combined executable.) |
| 14 | + * |
| 15 | + * This file is distributed in the hope that it will be useful, but |
| 16 | + * WITHOUT ANY WARRANTY; without even the implied warranty of |
| 17 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 18 | + * General Public License for more details. |
| 19 | + * |
| 20 | + * You should have received a copy of the GNU General Public License |
| 21 | + * along with this program; see the file COPYING. If not, write to |
| 22 | + * the Free Software Foundation, 51 Franklin Street, Fifth Floor, |
| 23 | + * Boston, MA 02110-1301, USA. |
| 24 | + */ |
| 25 | + |
| 26 | +#ifndef INCLUDE_git_os_abstraction_h__ |
| 27 | +#define INCLUDE_git_os_abstraction_h__ |
| 28 | + |
| 29 | +#include <unistd.h> |
| 30 | +#include <sys/stat.h> |
| 31 | +#include <fcntl.h> |
| 32 | +#include <sys/types.h> |
| 33 | +#include <time.h> |
| 34 | + |
| 35 | +/** |
| 36 | + * @file git/os/unix.h |
| 37 | + * @brief Portable operating system abstractions |
| 38 | + * @defgroup git_os_abstraction Portable operating system abstractions |
| 39 | + * @ingroup Git |
| 40 | + * @{ |
| 41 | + */ |
| 42 | +GIT_BEGIN_DECL |
| 43 | + |
| 44 | +/** Descriptor to an open file in the filesystem. */ |
| 45 | +typedef int git_file; |
| 46 | + |
| 47 | +/** |
| 48 | + * Open a file by path name. |
| 49 | + * |
| 50 | + * Valid flags are: |
| 51 | + * - O_CREAT: Create the file if it does not yet exist. |
| 52 | + * - O_RDONLY: Open the file for reading. |
| 53 | + * - O_WRONLY: Open the file for writing. |
| 54 | + * - O_RDWR: Open the file for both reading and writing. |
| 55 | + * |
| 56 | + * @param path path name of the file to open. |
| 57 | + * @param flags bitmask of access requested to the file. |
| 58 | + * @return the opened file descriptor; <0 if the open failed. |
| 59 | + */ |
| 60 | +static inline git_file git_fopen(const char *path, int flags) |
| 61 | +{ |
| 62 | + return open(path, flags); |
| 63 | +} |
| 64 | + |
| 65 | +/** |
| 66 | + * Close an open file descriptor. |
| 67 | + * @param fd descriptor to close. |
| 68 | + * @return 0 on success; <0 if the descriptor close failed. |
| 69 | + */ |
| 70 | +static inline int git_fclose(git_file fd) |
| 71 | +{ |
| 72 | + return close(fd); |
| 73 | +} |
| 74 | + |
| 75 | +/** |
| 76 | + * Read from an open file descriptor at the current position. |
| 77 | + * |
| 78 | + * Less than the number of requested bytes may be read. The |
| 79 | + * read is automatically restarted if it fails due to a signal |
| 80 | + * being delivered to the calling thread. |
| 81 | + * |
| 82 | + * @param fd open descriptor. |
| 83 | + * @param buf buffer to store the read data into. |
| 84 | + * @param cnt number of bytes to transfer. |
| 85 | + * @return |
| 86 | + * - On success, actual number of bytes read. |
| 87 | + * - On EOF, 0. |
| 88 | + * - On failure, <0. |
| 89 | + */ |
| 90 | +GIT_EXTERN(ssize_t) git_fread(git_file fd, void *buf, size_t cnt); |
| 91 | + |
| 92 | +/** |
| 93 | + * Write to an open file descriptor at the current position. |
| 94 | + * |
| 95 | + * Less than the number of requested bytes may be written. The |
| 96 | + * write is automatically restarted if it fails due to a signal |
| 97 | + * being delivered to the calling thread. |
| 98 | + * |
| 99 | + * @param fd open descriptor. |
| 100 | + * @param buf buffer to write data from. |
| 101 | + * @param cnt number of bytes to transfer. |
| 102 | + * @return |
| 103 | + * - On success, actual number of bytes written. |
| 104 | + * - On EOF, 0. |
| 105 | + * - On failure, <0. |
| 106 | + */ |
| 107 | +GIT_EXTERN(ssize_t) git_fwrite(git_file fd, void *buf, size_t cnt); |
| 108 | + |
| 109 | +/** @} */ |
| 110 | +GIT_END_DECL |
| 111 | +#endif |
0 commit comments