From 58f4fa84259348029f50a97d84bd3b4492567645 Mon Sep 17 00:00:00 2001 From: Alexei Svitkine Date: Mon, 25 Jun 2012 12:56:58 -0400 Subject: [PATCH] Lock opened disk image files with O_EXLOCK (same as flock()) on Mac OS X to prevent concurrent access from the Finder. --- BasiliskII/src/Unix/sys_unix.cpp | 22 +++++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) diff --git a/BasiliskII/src/Unix/sys_unix.cpp b/BasiliskII/src/Unix/sys_unix.cpp index dc9892e3..b3801149 100644 --- a/BasiliskII/src/Unix/sys_unix.cpp +++ b/BasiliskII/src/Unix/sys_unix.cpp @@ -616,10 +616,26 @@ void *Sys_open(const char *name, bool read_only) } #endif + int open_flags = (read_only ? O_RDONLY : O_RDWR); #if defined(__linux__) || defined(__FreeBSD__) || defined(__NetBSD__) || defined(__MACOSX__) - int fd = open(name, (read_only ? O_RDONLY : O_RDWR) | (is_cdrom ? O_NONBLOCK : 0)); -#else - int fd = open(name, read_only ? O_RDONLY : O_RDWR); + open_flags |= (is_cdrom ? O_NONBLOCK : 0); +#endif +#if defined(__MACOSX__) + open_flags |= (is_file ? O_EXLOCK | O_NONBLOCK : 0); +#endif + int fd = open(name, open_flags); +#if defined(__MACOSX__) + if (fd < 0 && (open_flags & O_EXLOCK)) { + if (errno == EOPNOTSUPP) { + // File system does not support locking. Try again without. + open_flags &= ~O_EXLOCK; + fd = open(name, open_flags); + } else if (errno == EAGAIN) { + // File is likely already locked by another process. + printf("WARNING: Cannot open %s (%s)\n", name, strerror(errno)); + return NULL; + } + } #endif if (fd < 0 && !read_only) { // Read-write failed, try read-only