示例#1
0
        public void CannotAccessDeletedFile()
        {
            ulong  volumeSerial;
            FileId fileId;
            string path = GetFullPath("F");

            using (FileStream fs = File.Create(path))
            {
                volumeSerial = FileUtilities.GetVolumeSerialNumberByHandle(fs.SafeFileHandle);
                fileId       = FileUtilities.ReadFileUsnByHandle(fs.SafeFileHandle).Value.FileId;
            }

            File.Delete(path);

            VolumeMap map = CreateMapOfAllLocalVolumes();

            using (FileAccessor accessor = map.CreateFileAccessor())
            {
                SafeFileHandle handle;
                FileAccessor.OpenFileByIdResult openResult = accessor.TryOpenFileById(
                    volumeSerial,
                    fileId,
                    FileDesiredAccess.GenericRead,
                    FileShare.ReadWrite,
                    FileFlagsAndAttributes.None,
                    out handle);
                using (handle)
                {
                    XAssert.AreEqual(FileAccessor.OpenFileByIdResult.FailedToFindFile, openResult);
                    XAssert.IsNull(handle);
                }
            }
        }
        /// <inheritdoc />
        public bool TryGetFileHandleAndPathFromFileIdAndVolumeId(FileIdAndVolumeId fileIdAndVolumeId, FileShare fileShare, out SafeFileHandle handle, out string path)
        {
            path = null;

            FileAccessor.OpenFileByIdResult openResult = m_fileAccessor.TryOpenFileById(
                fileIdAndVolumeId.VolumeSerialNumber,
                fileIdAndVolumeId.FileId,
                FileDesiredAccess.GenericRead,
                fileShare,
                FileFlagsAndAttributes.None,
                out handle);

            switch (openResult)
            {
            case FileAccessor.OpenFileByIdResult.FailedToOpenVolume:
            case FileAccessor.OpenFileByIdResult.FailedToFindFile:
            case FileAccessor.OpenFileByIdResult.FailedToAccessExistentFile:
                Contract.Assert(handle == null);
                return(false);

            case FileAccessor.OpenFileByIdResult.Succeeded:
                Contract.Assert(handle != null && !handle.IsInvalid);
                path = FileUtilities.GetFinalPathNameByHandle(handle, volumeGuidPath: false);
                return(true);

            default:
                throw Contract.AssertFailure("Unhandled OpenFileByIdResult");
            }
        }
示例#3
0
        public void CanAccessMultipleFilesById()
        {
            var fileIdAndVolumeSerialPairs = new List <Tuple <FileId, ulong> >();

            for (int i = 0; i < 3; i++)
            {
                string path = GetFullPath("F" + i);
                using (FileStream fs = File.Create(path))
                {
                    fs.WriteByte((byte)i);

                    ulong  volumeSerial = FileUtilities.GetVolumeSerialNumberByHandle(fs.SafeFileHandle);
                    FileId fileId       = FileUtilities.ReadFileUsnByHandle(fs.SafeFileHandle).Value.FileId;
                    fileIdAndVolumeSerialPairs.Add(Tuple.Create(fileId, volumeSerial));
                }
            }

            VolumeMap map = CreateMapOfAllLocalVolumes();

            using (FileAccessor accessor = map.CreateFileAccessor())
            {
                for (int i = 0; i < fileIdAndVolumeSerialPairs.Count; i++)
                {
                    Tuple <FileId, ulong> fileIdAndVolumeSerial = fileIdAndVolumeSerialPairs[i];

                    SafeFileHandle handle;
                    FileAccessor.OpenFileByIdResult openResult = accessor.TryOpenFileById(
                        fileIdAndVolumeSerial.Item2,
                        fileIdAndVolumeSerial.Item1,
                        FileDesiredAccess.GenericRead,
                        FileShare.ReadWrite,
                        FileFlagsAndAttributes.None,
                        out handle);
                    using (handle)
                    {
                        XAssert.AreEqual(openResult, FileAccessor.OpenFileByIdResult.Succeeded);
                        XAssert.IsNotNull(handle);

                        using (var fileStream = new FileStream(handle, FileAccess.Read))
                        {
                            XAssert.AreEqual(i, fileStream.ReadByte());
                        }
                    }
                }
            }
        }