compat: make p_open able to accept optional mode when passing the O_CREAT flag This has the nice side effect of making test_attr_repo__staging_properly_normalizes_line_endings_according_to_gitattributes_directives() test pass again on Windows. This test started to fail after commit 674a198 was applied.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
diff --git a/src/posix.c b/src/posix.c
index a3f81d7..cecb538 100644
--- a/src/posix.c
+++ b/src/posix.c
@@ -12,9 +12,20 @@
#ifndef GIT_WIN32
-int p_open(const char *path, int flags)
+int p_open(const char *path, int flags, ...)
{
- return open(path, flags | O_BINARY);
+ mode_t mode = 0;
+
+ if (flags & O_CREAT)
+ {
+ va_list arg_list;
+
+ va_start(arg_list, flags);
+ mode = va_arg(arg_list, mode_t);
+ va_end(arg_list);
+ }
+
+ return open(path, flags | O_BINARY, mode);
}
int p_creat(const char *path, mode_t mode)
diff --git a/src/posix.h b/src/posix.h
index 752d515..d020d94 100644
--- a/src/posix.h
+++ b/src/posix.h
@@ -42,7 +42,7 @@ extern int p_write(git_file fd, const void *buf, size_t cnt);
#define p_close(fd) close(fd)
#define p_umask(m) umask(m)
-extern int p_open(const char *path, int flags);
+extern int p_open(const char *path, int flags, ...);
extern int p_creat(const char *path, mode_t mode);
extern int p_getcwd(char *buffer_out, size_t size);
extern int p_rename(const char *from, const char *to);
diff --git a/src/win32/posix.h b/src/win32/posix.h
index d13d3e3..2666fcc 100644
--- a/src/win32/posix.h
+++ b/src/win32/posix.h
@@ -45,7 +45,7 @@ extern int p_chmod(const char* path, mode_t mode);
extern int p_rmdir(const char* path);
extern int p_access(const char* path, mode_t mode);
extern int p_fsync(int fd);
-extern int p_open(const char *path, int flags);
+extern int p_open(const char *path, int flags, ...);
extern int p_creat(const char *path, mode_t mode);
extern int p_getcwd(char *buffer_out, size_t size);
extern int p_rename(const char *from, const char *to);
diff --git a/src/win32/posix_w32.c b/src/win32/posix_w32.c
index 8af6641..6172918 100644
--- a/src/win32/posix_w32.c
+++ b/src/win32/posix_w32.c
@@ -217,13 +217,27 @@ int p_readlink(const char *link, char *target, size_t target_len)
return dwRet;
}
-int p_open(const char *path, int flags)
+int p_open(const char *path, int flags, ...)
{
int fd;
- wchar_t* buf = gitwin_to_utf16(path);
+ wchar_t* buf;
+ mode_t mode = 0;
+
+ buf = gitwin_to_utf16(path);
if (!buf)
return -1;
- fd = _wopen(buf, flags | _O_BINARY);
+
+ if (flags & O_CREAT)
+ {
+ va_list arg_list;
+
+ va_start(arg_list, flags);
+ mode = va_arg(arg_list, mode_t);
+ va_end(arg_list);
+ }
+
+ fd = _wopen(buf, flags | _O_BINARY, mode);
+
git__free(buf);
return fd;
}
diff --git a/tests-clar/clar_helpers.c b/tests-clar/clar_helpers.c
index 6976140..d180285 100644
--- a/tests-clar/clar_helpers.c
+++ b/tests-clar/clar_helpers.c
@@ -30,7 +30,7 @@ void cl_git_mkfile(const char *filename, const char *content)
void cl_git_write2file(const char *filename, const char *new_content, int flags)
{
- int fd = open(filename, flags, 0644);
+ int fd = p_open(filename, flags, 0644);
cl_assert(fd >= 0);
if (!new_content)
new_content = "\n";