Example #1
0
        public PosixJournalWriter(StorageEnvironmentOptions options, string filename, long journalSize)
        {
            _options  = options;
            _filename = filename;
            _maxNumberOfPagesPerSingleWrite = int.MaxValue / _options.PageSize;

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

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

            int result;

            if ((options.SafePosixOpenFlags & PerPlatformValues.OpenFlags.O_DIRECT) == 0)
            {
                // fallocate doesn't supported, we'll use lseek instead
                result = Syscall.AllocateUsingLseek(_fd, journalSize);
            }
            else
            {
                result = Syscall.posix_fallocate(_fd, IntPtr.Zero, (UIntPtr)journalSize);
            }
            if (result != 0)
            {
                PosixHelper.ThrowLastError(result, "when allocating " + filename);
            }

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

            NumberOfAllocatedPages = (int)(journalSize / _options.PageSize);
        }