Example #1
0
        public static unsafe void WriteFileHeader(FileHeader *header, string path)
        {
            var fd = Syscall.open(path, OpenFlags.O_WRONLY | OpenFlags.O_CREAT,
                                  FilePermissions.ALLPERMS);

            try
            {
                if (fd == -1)
                {
                    ThrowLastError(Marshal.GetLastWin32Error());
                }
                int remaining = sizeof(FileHeader);
                var ptr       = ((byte *)header);
                while (remaining > 0)
                {
                    var written = Syscall.write(fd, ptr, (ulong)remaining);
                    if (written == -1)
                    {
                        ThrowLastError(Marshal.GetLastWin32Error());
                    }

                    remaining -= (int)written;
                    ptr       += written;
                }
                Syscall.fsync(fd);
            }
            finally
            {
                if (fd != -1)
                {
                    Syscall.close(fd);
                }
            }
        }
 protected override void DisposeInternal()
 {
     if (_fd != -1)
     {
         Syscall.close(_fd);
         _fd = -1;
     }
 }
 public override void Dispose()
 {
     base.Dispose();
     if (_fd != -1)
     {
         Syscall.close(_fd);
         _fd = -1;
     }
 }
Example #4
0
        public PosixJournalWriter(StorageEnvironmentOptions options, VoronPathSetting filename, long journalSize)
        {
            try
            {
                _options  = options;
                _filename = filename;
                _maxNumberOf4KbPerSingleWrite = int.MaxValue / (4 * Constants.Size.Kilobyte);

                _fd = Syscall.open(filename.FullPath, OpenFlags.O_WRONLY | options.PosixOpenFlags | PerPlatformValues.OpenFlags.O_CREAT,
                                   FilePermissions.S_IWUSR | FilePermissions.S_IRUSR);

                if (_fd == -1)
                {
                    var err = Marshal.GetLastWin32Error();
                    Syscall.ThrowLastError(err, "when opening " + filename);
                }

                if (RunningOnMacOsx)
                {
                    // mac doesn't support O_DIRECT, we fcntl instead:
                    var rc = Syscall.fcntl(_fd, FcntlCommands.F_NOCACHE, (IntPtr)1);
                    if (rc != 0)
                    {
                        var err = Marshal.GetLastWin32Error();
                        Syscall.ThrowLastError(err, "when fcntl F_NOCACHE for " + filename);
                    }
                }

                var length = new FileInfo(filename.FullPath).Length;
                if (length < journalSize)
                {
                    length = journalSize;
                    try
                    {
                        PosixHelper.AllocateFileSpace(options, _fd, journalSize, filename.FullPath);
                    }
                    catch (Exception)
                    {
                        Syscall.close(_fd);
                        throw;
                    }
                }
                if (Syscall.CheckSyncDirectoryAllowed(_filename.FullPath) && Syscall.SyncDirectory(filename.FullPath) == -1)
                {
                    var err = Marshal.GetLastWin32Error();
                    Syscall.ThrowLastError(err, "when syncing dir for on " + filename);
                }

                NumberOfAllocated4Kb = (int)(length / (4 * Constants.Size.Kilobyte));
            }
            catch
            {
                Dispose();
                throw;
            }
        }
Example #5
0
 public override void Dispose()
 {
     base.Dispose();
     if (_fd != -1)
     {
         Syscall.close(_fd);
         Rt.shm_unlink(_file);
         _fd = -1;
     }
 }
Example #6
0
        public static unsafe void WriteFileHeader(FileHeader *header, VoronPathSetting path)
        {
            bool syncIsNeeded = false;
            var  fd           = Syscall.open(path.FullPath, OpenFlags.O_WRONLY | PerPlatformValues.OpenFlags.O_CREAT,
                                             FilePermissions.S_IWUSR | FilePermissions.S_IRUSR);

            try
            {
                if (fd == -1)
                {
                    var err = Marshal.GetLastWin32Error();
                    Syscall.ThrowLastError(err, "when opening " + path);
                }

                int remaining = sizeof(FileHeader);

                FileInfo fi = new FileInfo(path.FullPath);
                if (fi.Length != remaining)
                {
                    syncIsNeeded = true;
                }

                var ptr = ((byte *)header);
                while (remaining > 0)
                {
                    var written = Syscall.write(fd, ptr, (ulong)remaining);
                    if (written == -1)
                    {
                        var err = Marshal.GetLastWin32Error();
                        Syscall.ThrowLastError(err, "writing to " + path);
                    }

                    remaining -= (int)written;
                    ptr       += written;
                }
                if (Syscall.FSync(fd) == -1)
                {
                    var err = Marshal.GetLastWin32Error();
                    Syscall.ThrowLastError(err, "FSync " + path);
                }

                if (syncIsNeeded)
                {
                    Syscall.FsyncDirectoryFor(path.FullPath);
                }
            }
            finally
            {
                if (fd != -1)
                {
                    Syscall.close(fd);
                    fd = -1;
                }
            }
        }
Example #7
0
 public void Dispose()
 {
     Disposed = true;
     GC.SuppressFinalize(this);
     if (_fdReads != -1)
     {
         Syscall.close(_fdReads);
     }
     Syscall.close(_fd);
     if (DeleteOnClose)
     {
         File.Delete(_filename);
     }
 }
Example #8
0
        public static unsafe void WriteFileHeader(FileHeader *header, string path)
        {
            var fd = Syscall.open(path, OpenFlags.O_WRONLY | OpenFlags.O_CREAT,
                                  FilePermissions.S_IWUSR | FilePermissions.S_IRUSR);

            try
            {
                if (fd == -1)
                {
                    var err = Marshal.GetLastWin32Error();
                    ThrowLastError(err, "when opening " + path);
                }

                int remaining = sizeof(FileHeader);
                var ptr       = ((byte *)header);
                while (remaining > 0)
                {
                    var written = Syscall.write(fd, ptr, (ulong)remaining);
                    if (written == -1)
                    {
                        var err = Marshal.GetLastWin32Error();
                        ThrowLastError(err, "writing to " + path);
                    }

                    remaining -= (int)written;
                    ptr       += written;
                }
                if (Syscall.fsync(fd) == -1)
                {
                    var err = Marshal.GetLastWin32Error();
                    ThrowLastError(err, "fsync " + path);
                }
                if (CheckSyncDirectoryAllowed(path) && SyncDirectory(path) == -1)
                {
                    var err = Marshal.GetLastWin32Error();
                    ThrowLastError(err, "fsync dir " + path);
                }
            }
            finally
            {
                if (fd != -1)
                {
                    Syscall.close(fd);
                    fd = -1;
                }
            }
        }
Example #9
0
        public static int SyncDirectory(string path)
        {
            var dir = Path.GetDirectoryName(path);
            var fd  = Syscall.open(dir, 0, 0);

            if (fd == -1)
            {
                return(-1);
            }
            var fsyncRc = Syscall.fsync(fd);

            if (fsyncRc == -1)
            {
                return(-1);
            }
            return(Syscall.close(fd));
        }
Example #10
0
 protected override void DisposeInternal()
 {
     if (_fd != -1)
     {
         // note that the orders of operations is important here, we first unlink the file
         // we are supposed to be the only one using it, so Linux would be ready to delete it
         // and hopefully when we close it, won't waste any time trying to sync the memory state
         // to disk just to discard it
         if (DeleteOnClose)
         {
             Syscall.unlink(FileName.FullPath);
             // explicitly ignoring the result here, there isn't
             // much we can do to recover from being unable to delete it
         }
         Syscall.close(_fd);
         _fd = -1;
     }
 }
Example #11
0
        public static unsafe bool TryReadFileHeader(FileHeader *header, VoronPathSetting path)
        {
            var fd = Syscall.open(path.FullPath, OpenFlags.O_RDONLY, FilePermissions.S_IRUSR);

            try
            {
                if (fd == -1)
                {
                    var lastError = Marshal.GetLastWin32Error();
                    if (((Errno)lastError) == Errno.EACCES)
                    {
                        return(false);
                    }
                    Syscall.ThrowLastError(lastError);
                }
                int remaining = sizeof(FileHeader);
                var ptr       = ((byte *)header);
                while (remaining > 0)
                {
                    var read = Syscall.read(fd, ptr, (ulong)remaining);
                    if (read == -1)
                    {
                        var err = Marshal.GetLastWin32Error();
                        Syscall.ThrowLastError(err);
                    }

                    if (read == 0)
                    {
                        return(false); // truncated file?
                    }
                    remaining -= (int)read;
                    ptr       += read;
                }
                return(true);
            }
            finally
            {
                if (fd != -1)
                {
                    Syscall.close(fd);
                    fd = -1;
                }
            }
        }
 public void Dispose()
 {
     Disposed = true;
     GC.SuppressFinalize(this);
     _options.IoMetrics.FileClosed(_filename.FullPath);
     if (_fdReads != -1)
     {
         Syscall.close(_fdReads);
         _fdReads = -1;
     }
     if (_fd != -1)
     {
         Syscall.close(_fd);
         _fd = -1;
     }
     if (DeleteOnClose)
     {
         _options.TryStoreJournalForReuse(_filename);
     }
 }
Example #13
0
 public void Dispose()
 {
     Disposed = true;
     GC.SuppressFinalize(this);
     if (_fdReads != -1)
     {
         Syscall.close(_fdReads);
     }
     Syscall.close(_fd);
     if (DeleteOnClose)
     {
         try
         {
             File.Delete(_filename);
         }
         catch (Exception)
         {
             // nothing to do here
         }
     }
 }