diff options
| author | Vicent Marti <tanoku@gmail.com> | 2011-07-04 11:43:34 +0200 |
|---|---|---|
| committer | Vicent Marti <tanoku@gmail.com> | 2011-07-05 02:04:03 +0200 |
| commit | f79026b4912bcd2336667f4c1663c06e233f0b32 (patch) | |
| tree | 645b776032e924b587fad986aa3f3dc08c98d4c5 /src/posix.c | |
| parent | 678e9e045becdc5d75f2ce2259ed01c3531ee181 (diff) | |
| download | libgit2-f79026b4912bcd2336667f4c1663c06e233f0b32.tar.gz | |
fileops: Cleanup
Cleaned up the structure of the whole OS-abstraction layer.
fileops.c now contains a set of utility methods for file management used
by the library. These are abstractions on top of the original POSIX
calls.
There's a new file called `posix.c` that contains
emulations/reimplementations of all the POSIX calls the library uses.
These are prefixed with `p_`. There's a specific posix file for each
platform (win32 and unix).
All the path-related methods have been moved from `utils.c` to `path.c`
and have their own prefix.
Diffstat (limited to 'src/posix.c')
| -rw-r--r-- | src/posix.c | 74 |
1 files changed, 74 insertions, 0 deletions
diff --git a/src/posix.c b/src/posix.c new file mode 100644 index 000000000..9dbebbd57 --- /dev/null +++ b/src/posix.c @@ -0,0 +1,74 @@ +#include "common.h" +#include "posix.h" +#include "path.h" +#include <stdio.h> +#include <ctype.h> + +int p_open(const char *path, int flags) +{ + return open(path, flags | O_BINARY); +} + +int p_creat(const char *path, int mode) +{ + return open(path, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, mode); +} + +int p_read(git_file fd, void *buf, size_t cnt) +{ + char *b = buf; + while (cnt) { + ssize_t r = read(fd, b, cnt); + if (r < 0) { + if (errno == EINTR || errno == EAGAIN) + continue; + return GIT_EOSERR; + } + if (!r) + break; + cnt -= r; + b += r; + } + return (int)(b - (char *)buf); +} + +int p_write(git_file fd, void *buf, size_t cnt) +{ + char *b = buf; + while (cnt) { + ssize_t r = write(fd, b, cnt); + if (r < 0) { + if (errno == EINTR || errno == EAGAIN) + continue; + return GIT_EOSERR; + } + if (!r) { + errno = EPIPE; + return GIT_EOSERR; + } + cnt -= r; + b += r; + } + return GIT_SUCCESS; +} + +int p_getcwd(char *buffer_out, size_t size) +{ + char *cwd_buffer; + + assert(buffer_out && size > 0); + +#ifdef GIT_WIN32 + cwd_buffer = _getcwd(buffer_out, size); +#else + cwd_buffer = getcwd(buffer_out, size); +#endif + + if (cwd_buffer == NULL) + return git__throw(GIT_EOSERR, "Failed to retrieve current working directory"); + + git_path_mkposix(buffer_out); + + git_path_join(buffer_out, buffer_out, ""); //Ensure the path ends with a trailing slash + return GIT_SUCCESS; +} |
