summaryrefslogtreecommitdiffstats
path: root/src/osxsupport.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/osxsupport.c')
-rw-r--r--src/osxsupport.c17
1 files changed, 17 insertions, 0 deletions
diff --git a/src/osxsupport.c b/src/osxsupport.c
index 141e225..1a18dec 100644
--- a/src/osxsupport.c
+++ b/src/osxsupport.c
@@ -6,6 +6,8 @@
#include <caml/alloc.h>
#include <caml/memory.h>
#ifdef __APPLE__
+#include <sys/types.h>
+#include <sys/stat.h>
#include <sys/attr.h>
#include <unistd.h>
#include <stdio.h>
@@ -100,6 +102,21 @@ CAMLprim value setFileInfos (value path, value fInfo) {
retcode = setattrlist(String_val (path), &attrList, attrBuf.finderInfo,
sizeof attrBuf.finderInfo, options);
+ if (retcode == -1 && errno == EACCES) {
+ /* Unlike with normal Unix attributes, we cannot set OS X attributes
+ if file is read-only. Try making it writable temporarily. */
+ struct stat st;
+ int r = stat(String_val(path), &st);
+ if (r == -1) uerror("setattrlist", path);
+ r = chmod(String_val(path), st.st_mode | S_IWUSR);
+ if (r == -1) uerror("setattrlist", path);
+ /* Try again */
+ retcode = setattrlist(String_val (path), &attrList, attrBuf.finderInfo,
+ sizeof attrBuf.finderInfo, options);
+ /* Whether or not that worked, we should try to set the mode back. */
+ chmod(String_val(path), st.st_mode);
+ }
+
if (retcode == -1) uerror("setattrlist", path);
CAMLreturn (Val_unit);