Пример #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);
                }
            }
        }
Пример #2
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;
                }
            }
        }
Пример #3
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));
        }
Пример #4
0
        public void Truncate(long size)
        {
            var result = Syscall.ftruncate(_fd, (IntPtr)size);

            if (result == -1)
            {
                var err = Marshal.GetLastWin32Error();
                PosixHelper.ThrowLastError(err, "when truncating " + _filename);
            }
            result = Syscall.fsync(_fd);
            if (result == -1)
            {
                var err = Marshal.GetLastWin32Error();
                PosixHelper.ThrowLastError(err, "when fsycning " + _filename);
            }

            if (PosixHelper.CheckSyncDirectoryAllowed(_filename) && PosixHelper.SyncDirectory(_filename) == -1)
            {
                var err = Marshal.GetLastWin32Error();
                PosixHelper.ThrowLastError(err, "when syncing dir for " + _filename);
            }
        }