示例#1
0
        private static OpenFileResult TryOpenDirectory(
            string directoryPath,
            FileDesiredAccess desiredAccess,
            FileShare shareMode,
            FileMode fileMode,
            FileFlagsAndAttributes flagsAndAttributes,
            out SafeFileHandle handle)
        {
            Contract.Requires(!string.IsNullOrEmpty(directoryPath));

            if (flagsAndAttributes.HasFlag(FileFlagsAndAttributes.FileFlagOpenReparsePoint) &&
                IsSymlink(directoryPath))
            {
                return(TryOpenSymlink(directoryPath, desiredAccess, shareMode, fileMode, out handle));
            }

            try
            {
                FileStream fs = new FileStream(directoryPath, fileMode, FileDesiredAccessToFileAccess(desiredAccess), shareMode, DefaultBufferSize, FileFlagsAndAttributesToFileOptions(flagsAndAttributes | FileFlagsAndAttributes.FileFlagBackupSemantics));
                handle = fs.SafeFileHandle;
                return(OpenFileResult.Create(NativeIOConstants.ErrorSuccess, fileMode, handleIsValid: true, openingById: false));
            }
            catch (Exception ex)
            {
                handle = null;
                int nativeErrorCode = (int)NativeErrorCodeForException(ex);
                Logger.Log.StorageTryOpenDirectoryFailure(Events.StaticContext, directoryPath, nativeErrorCode);
                return(OpenFileResult.Create(nativeErrorCode, fileMode, handleIsValid: false, openingById: false));
            }
        }
示例#2
0
        /// <inheritdoc />
        public OpenFileResult TryCreateOrOpenFile(
            string path,
            FileDesiredAccess desiredAccess,
            FileShare shareMode,
            FileMode creationDisposition,
            FileFlagsAndAttributes flagsAndAttributes,
            out SafeFileHandle handle)
        {
            if (flagsAndAttributes.HasFlag(FileFlagsAndAttributes.FileFlagOpenReparsePoint) &&
                IsSymlink(path))
            {
                return(TryOpenSymlink(path, desiredAccess, shareMode, creationDisposition, out handle));
            }

            try
            {
                FileStream fs = new FileStream(path, creationDisposition, FileDesiredAccessToFileAccess(desiredAccess), shareMode, DefaultBufferSize, FileFlagsAndAttributesToFileOptions(flagsAndAttributes));
                handle = fs.SafeFileHandle;
                return(OpenFileResult.Create(NativeIOConstants.ErrorSuccess, creationDisposition, handleIsValid: true, openingById: false));
            }
            catch (Exception ex)
            {
                handle = null;
                var errorCode = NativeErrorCodeForException(ex);
                Logger.Log.StorageTryOpenOrCreateFileFailure(Events.StaticContext, path, (int)creationDisposition, (int)errorCode);
                return(OpenFileResult.Create((int)errorCode, creationDisposition, handleIsValid: false, openingById: false));
            }
        }
示例#3
0
        private OpenFileResult TryOpen(
            string path,
            FileDesiredAccess desiredAccess,
            FileShare shareMode,
            FileMode fileMode,
            bool openSymlink,
            out SafeFileHandle handle)
        {
            try
            {
                var mayBeExistence = TryProbePathExistence(path, openSymlink);

                handle = Open(path, CreateOpenFlags(desiredAccess, shareMode, fileMode, openSymlink), CreateFilePermissions(desiredAccess));

                if (handle.IsInvalid)
                {
                    handle = null;
                    return(CreateErrorResult(Marshal.GetLastWin32Error()));
                }

                int successCode =
                    mayBeExistence.Succeeded && mayBeExistence.Result == PathExistence.ExistsAsFile
                    ? NativeIOConstants.ErrorFileExists
                    : (mayBeExistence.Succeeded && mayBeExistence.Result == PathExistence.ExistsAsDirectory
                        ? NativeIOConstants.ErrorAlreadyExists
                        : NativeIOConstants.ErrorSuccess);

                return(OpenFileResult.Create(path, successCode, fileMode, handleIsValid: true));
            }
            catch (Exception e)
            {
                handle = null;
                return(CreateErrorResult((int)NativeErrorCodeForException(e)));
            }

            OpenFileResult CreateErrorResult(int errorCode)
            {
                Logger.Log.StorageTryOpenOrCreateFileFailure(Events.StaticContext, path, (int)fileMode, (int)errorCode);
                return(OpenFileResult.Create(path, (int)errorCode, fileMode, handleIsValid: false));
            }
        }
示例#4
0
        public void OpenFileResultFindHandlesOnSharingViolation()
        {
            var file = GetFullPath("test.txt");
            var dummyBlockedResult       = OpenFileResult.Create(file, NativeIOConstants.ErrorSharingViolation, FileMode.Create, handleIsValid: false);
            var dummyArbitraryFailResult = OpenFileResult.Create(file, NativeIOConstants.ErrorHandleEof, FileMode.Create, handleIsValid: false);

            using (var stream = new FileStream(file, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.None))
            {
                var dummyBlockedError = dummyBlockedResult.CreateExceptionForError();
                XAssert.IsTrue(dummyBlockedError.Message.Contains(FileUtilitiesMessages.ActiveHandleUsage));

                var dummyBlockedFailure = dummyBlockedResult.CreateFailureForError();
                XAssert.IsTrue(dummyBlockedFailure.DescribeIncludingInnerFailures().Contains(FileUtilitiesMessages.ActiveHandleUsage));

                var dummyArbitraryError = dummyArbitraryFailResult.CreateExceptionForError();
                XAssert.IsFalse(dummyArbitraryError.Message.Contains(FileUtilitiesMessages.ActiveHandleUsage));

                var dummyArbitraryFailure = dummyArbitraryFailResult.CreateFailureForError();
                XAssert.IsFalse(dummyArbitraryFailure.DescribeIncludingInnerFailures().Contains(FileUtilitiesMessages.ActiveHandleUsage));
            }
        }
示例#5
0
        private static OpenFileResult TryOpenSymlink(
            string path,
            FileDesiredAccess desiredAccess,
            FileShare shareMode,
            FileMode fileMode,
            out SafeFileHandle handle)
        {
            try
            {
                int fileHandle = 0;
                if (ShouldCreateAndOpen(fileMode))
                {
                    fileHandle = openAndCreate(path, (int)(CreateOpenFlags(desiredAccess, shareMode, fileMode) | OpenFlags.O_SYMLINK), CreateFilePermissions(desiredAccess));
                }
                else
                {
                    fileHandle = open(path, (int)(CreateOpenFlags(desiredAccess, shareMode, fileMode) | OpenFlags.O_SYMLINK));
                }

                handle = new SafeFileHandle(new IntPtr(fileHandle), ownsHandle: true);
                if (fileHandle <= 0)
                {
                    handle = null;
                    return(CreateErrorResult(Marshal.GetLastWin32Error()));
                }

                return(OpenFileResult.Create(NativeIOConstants.ErrorSuccess, fileMode, handleIsValid: true, openingById: false));
            }
            catch (Exception e)
            {
                handle = null;
                return(CreateErrorResult((int)NativeErrorCodeForException(e)));
            }

            OpenFileResult CreateErrorResult(int errorCode)
            {
                Logger.Log.StorageTryOpenOrCreateFileFailure(Events.StaticContext, path, (int)fileMode, (int)errorCode);
                return(OpenFileResult.Create((int)errorCode, fileMode, handleIsValid: false, openingById: false));
            }
        }