This is a Mac specific question, but in essence it is a unix/bsd system level stuff. I suppose
there are people here that can help me.
I'm using fcntl() to perform locking regions of files. It works well on local volume, but fails
on shared volumes.
This line produces error 45, ENOTSUP on shared volumes:
Code:
fcntl(fp, F_SETLKW, &lk)
Here is an example I wrote to test it. When I try it in Terminal, if I give it ~ as a param all
works well. If I use some folder on shared volume (on either Mac or Windows) it fails:
Code:
#include <stdlib.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/time.h>
#include <sys/resource.h>
#include <unistd.h>
#include <fcntl.h>
#include <string.h>
#include <errno.h>
int main (int argc, const char * argv[])
{
int fp, bytesGone, len;
char tmpStr[256], buff[256];
struct flock lk;
if (argc != 2)
exit (1);
if (argv[1][strlen(argv[1])-1] == '/')
sprintf (tmpStr, "%s%s", argv[1], "MyFile");
else
sprintf (tmpStr, "%s/%s", argv[1], "MyFile");
if ((fp = open (tmpStr, O_RDWR)) < 0) {
if ((fp = open (tmpStr, O_RDWR | O_CREAT, 0666)) < 0) {
printf ("Open file error!\n");
exit (1);
}
}
lseek (fp, 0L, SEEK_SET);
if (write(fp, tmpStr, len = strlen(tmpStr)+1) != len)
exit (1);
lseek (fp, 0L, SEEK_SET);
if (read (fp, tmpStr, len) != len)
exit (1);
lseek (fp, 0L, SEEK_SET);
lk.l_start = 0;
lk.l_len = 8;
lk.l_pid = 0; // getpid ();
lk.l_type = F_WRLCK;
lk.l_whence = SEEK_CUR;
if (fcntl(fp, F_SETLKW, &lk)) { // or F_SETLK, both give ENOTSUP
printf ("Lock error: %d\n", errno);
exit (1);
}
printf ("All is well! Len = %d\n", len);
return (0);
}
There's even some Tech note by Apple that shows how to test if a volume supports file locking and all the volumes I checked passed that test.
Any idea how to solve this or find a workaround.