]> git.tdb.fi Git - libs/core.git/blobdiff - source/io/unix/file.cpp
Move most platform-specific code into overlay directories
[libs/core.git] / source / io / unix / file.cpp
diff --git a/source/io/unix/file.cpp b/source/io/unix/file.cpp
new file mode 100644 (file)
index 0000000..b2c9089
--- /dev/null
@@ -0,0 +1,60 @@
+#include <errno.h>
+#include <fcntl.h>
+#include <unistd.h>
+#include <msp/core/systemerror.h>
+#include <msp/strings/format.h>
+#include "file.h"
+#include "handle_private.h"
+
+using namespace std;
+
+namespace Msp {
+namespace IO {
+
+void File::platform_init(const string &fn, CreateMode cm)
+{
+       int flags = 0;
+       switch(mode&M_RDWR)
+       {
+       case M_READ:  flags |= O_RDONLY; break;
+       case M_WRITE: flags |= O_WRONLY; break;
+       case M_RDWR:  flags |= O_RDWR; break;
+       default:;
+       }
+
+       if(mode&M_WRITE)
+       {
+               if(cm&C_CREATE)
+                       flags |= O_CREAT;
+               if(cm&C_TRUNCATE)
+                       flags |= O_TRUNC;
+               if(cm&C_EXCLUSIVE)
+                       flags |= O_EXCL;
+       }
+       if(mode&M_APPEND)
+               flags |= O_APPEND;
+       if(mode&M_NONBLOCK)
+               flags |= O_NONBLOCK;
+
+       *handle = ::open(fn.c_str(), flags, 0666);
+       if(!handle)
+       {
+               int err = errno;
+               if(err==ENOENT)
+                       throw file_not_found(fn);
+               else if(err==EEXIST)
+                       throw file_already_exists(fn);
+               else
+                       throw system_error(format("open(%s)", fn), err);
+       }
+}
+
+void File::sync()
+{
+       signal_flush_required.emit();
+
+       fsync(*handle);
+}
+
+} // namespace IO
+} // namespace Msp