]> git.proxmox.com Git - libgit2.git/blob - src/posix.c
error-handling: On-disk config file backend
[libgit2.git] / src / posix.c
1 /*
2 * Copyright (C) 2009-2012 the libgit2 contributors
3 *
4 * This file is part of libgit2, distributed under the GNU GPL v2 with
5 * a Linking Exception. For full terms see the included COPYING file.
6 */
7 #include "common.h"
8 #include "posix.h"
9 #include "path.h"
10 #include <stdio.h>
11 #include <ctype.h>
12
13 #ifndef GIT_WIN32
14
15 int p_open(const char *path, int flags)
16 {
17 return open(path, flags | O_BINARY);
18 }
19
20 int p_creat(const char *path, mode_t mode)
21 {
22 return open(path, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, mode);
23 }
24
25 int p_getcwd(char *buffer_out, size_t size)
26 {
27 char *cwd_buffer;
28
29 assert(buffer_out && size > 0);
30
31 cwd_buffer = getcwd(buffer_out, size);
32
33 if (cwd_buffer == NULL)
34 return -1;
35
36 git_path_mkposix(buffer_out);
37 git_path_string_to_dir(buffer_out, size); //Ensure the path ends with a trailing slash
38
39 return GIT_SUCCESS;
40 }
41
42 int p_rename(const char *from, const char *to)
43 {
44 if (!link(from, to)) {
45 p_unlink(from);
46 return 0;
47 }
48
49 if (!rename(from, to))
50 return 0;
51
52 return -1;
53 }
54
55 #endif
56
57 int p_read(git_file fd, void *buf, size_t cnt)
58 {
59 char *b = buf;
60 while (cnt) {
61 ssize_t r = read(fd, b, cnt);
62 if (r < 0) {
63 if (errno == EINTR || errno == EAGAIN)
64 continue;
65 return -1;
66 }
67 if (!r)
68 break;
69 cnt -= r;
70 b += r;
71 }
72 return (int)(b - (char *)buf);
73 }
74
75 int p_write(git_file fd, const void *buf, size_t cnt)
76 {
77 const char *b = buf;
78 while (cnt) {
79 ssize_t r = write(fd, b, cnt);
80 if (r < 0) {
81 if (errno == EINTR || errno == EAGAIN)
82 continue;
83 return -1;
84 }
85 if (!r) {
86 errno = EPIPE;
87 return -1;
88 }
89 cnt -= r;
90 b += r;
91 }
92 return 0;
93 }