Пример #1
0
        internal static NativeMethods.WIN32_FIND_DATA GetExtendedNonRootDirectoryInfo(string path, out Exception exception)
        {
            exception = null;
            NativeMethods.WIN32_FIND_DATA win32_FIND_DATA = default(NativeMethods.WIN32_FIND_DATA);
            DirectoryInfo di = null;

            exception = MountPointUtil.HandleIOExceptions(delegate
            {
                di = new DirectoryInfo(path);
            });
            if (exception != null)
            {
                return(win32_FIND_DATA);
            }
            if (!di.Exists)
            {
                exception = new DirectoryNotFoundException();
                return(win32_FIND_DATA);
            }
            if (di.Parent == null)
            {
                DiagCore.RetailAssert(false, "An invalid root path was passed in: {0}", new object[]
                {
                    path
                });
                exception = new IOException(string.Format("An invalid root path was passed in: {0}", path));
                return(win32_FIND_DATA);
            }
            NativeMethods.WIN32_FIND_DATA result;
            using (DirectoryEnumerator dirEnum = new DirectoryEnumerator(di.Parent, false, false))
            {
                bool foundNext = false;
                NativeMethods.WIN32_FIND_DATA tempFindData = default(NativeMethods.WIN32_FIND_DATA);
                string dirName;
                exception = MountPointUtil.HandleIOExceptions(delegate
                {
                    foundNext = dirEnum.GetNextDirectoryExtendedInfo(di.Name, out dirName, out tempFindData);
                });
                if (exception != null)
                {
                    result = win32_FIND_DATA;
                }
                else if (!foundNext)
                {
                    exception = new DirectoryNotFoundException();
                    result    = win32_FIND_DATA;
                }
                else
                {
                    exception = null;
                    result    = tempFindData;
                }
            }
            return(result);
        }
Пример #2
0
 private static IEnumerable<NativeMethods.WIN32_FIND_DATA> GetAll(string directory)
 {
     var findData = new NativeMethods.WIN32_FIND_DATA();
     using (SafeFindHandle findHandle = NativeMethods.FindFirstFile(directory + @"\*", findData)) {
         if (!findHandle.IsInvalid) {
             do {
                 if (findData.cFileName != "." && findData.cFileName != "..")
                     yield return findData;
             } while (NativeMethods.FindNextFile(findHandle, findData));
         }
     }
 }
Пример #3
0
        internal static bool IsDirectoryMountPoint(string path, out Exception exception)
        {
            exception = null;
            DirectoryInfo di = null;

            exception = MountPointUtil.HandleIOExceptions(delegate
            {
                di = new DirectoryInfo(path);
            });
            if (exception != null)
            {
                return(false);
            }
            if (!di.Exists)
            {
                return(false);
            }
            if (!BitMasker.IsOn((int)di.Attributes, 1024))
            {
                return(false);
            }
            if (di.Parent == null)
            {
                return(false);
            }
            using (DirectoryEnumerator dirEnum = new DirectoryEnumerator(di.Parent, false, false))
            {
                NativeMethods.WIN32_FIND_DATA findData = default(NativeMethods.WIN32_FIND_DATA);
                bool   foundNext = false;
                string dirName;
                exception = MountPointUtil.HandleIOExceptions(delegate
                {
                    foundNext = dirEnum.GetNextDirectoryExtendedInfo(di.Name, out dirName, out findData);
                });
                if (exception != null)
                {
                    return(false);
                }
                if (!foundNext)
                {
                    return(false);
                }
                if (findData.Reserved0 == 2684354563U)
                {
                    return(true);
                }
            }
            return(false);
        }
Пример #4
0
        /// <summary>
        /// Initializes a new instance of the <see cref="FileData"/> class.
        /// </summary>
        /// <param name="dir">The directory that the file is stored at</param>
        /// <param name="findData">WIN32_FIND_DATA structure that this
        /// object wraps.</param>
        public FileData(string dir, NativeMethods.WIN32_FIND_DATA findData)
        {
            this.Attributes = findData.dwFileAttributes;


            this.CreationTimeUtc = ConvertDateTime(findData.ftCreationTime_dwHighDateTime,
                                                   findData.ftCreationTime_dwLowDateTime);

            this.LastAccessTimeUtc = ConvertDateTime(findData.ftLastAccessTime_dwHighDateTime,
                                                     findData.ftLastAccessTime_dwLowDateTime);

            this.LastWriteTimeUtc = ConvertDateTime(findData.ftLastWriteTime_dwHighDateTime,
                                                    findData.ftLastWriteTime_dwLowDateTime);

            this.Size = CombineHighLowInts(findData.nFileSizeHigh, findData.nFileSizeLow);

            this.Name = findData.cFileName;
            this.Path = System.IO.Path.Combine(dir, findData.cFileName);
        }
Пример #5
0
        private static void DirectoryCleanup(object cleanupLock, bool isForced)
        {
            string conversionsDirectory = OleConverter.GetConversionsDirectory(false);

            if (conversionsDirectory == null)
            {
                return;
            }
            try
            {
                if (Monitor.TryEnter(cleanupLock) || (isForced && Monitor.TryEnter(cleanupLock, 30000)))
                {
                    NativeMethods.WIN32_FIND_DATA win32_FIND_DATA = default(NativeMethods.WIN32_FIND_DATA);
                    using (SafeFindHandle safeFindHandle = NativeMethods.FindFirstFile(Path.Combine(conversionsDirectory, "*"), out win32_FIND_DATA))
                    {
                        if (!safeFindHandle.IsInvalid)
                        {
                            do
                            {
                                if ((win32_FIND_DATA.FileAttributes & NativeMethods.FileAttributes.Directory) != NativeMethods.FileAttributes.Directory)
                                {
                                    ExDateTime fileCreateTimeUtc = OleConverter.GetFileCreateTimeUtc(ref win32_FIND_DATA);
                                    TimeSpan   t = ExDateTime.UtcNow.Subtract(fileCreateTimeUtc);
                                    if (t > OleConverter.MaxFileLifetime)
                                    {
                                        NativeMethods.DeleteFile(Path.Combine(conversionsDirectory, win32_FIND_DATA.FileName));
                                    }
                                }
                            }while (NativeMethods.FindNextFile(safeFindHandle, out win32_FIND_DATA));
                        }
                    }
                }
            }
            finally
            {
                if (Monitor.IsEntered(cleanupLock))
                {
                    Monitor.Exit(cleanupLock);
                }
            }
        }
Пример #6
0
 SccFileSystemNode(string basePath, NativeMethods.WIN32_FIND_DATA findData)
 {
     _basePath = basePath;
     _findData = findData;
 }
Пример #7
0
        private static SafeFindHandle BeginFind(string normalizedPathWithSearchPattern, out NativeMethods.WIN32_FIND_DATA findData)
        {
            SafeFindHandle handle = NativeMethods.FindFirstFile(normalizedPathWithSearchPattern, out findData);

            if (handle.IsInvalid)
            {
                int errorCode = Marshal.GetLastWin32Error();
                if (errorCode != NativeMethods.ERROR_FILE_NOT_FOUND)
                {
                    throw LongPathCommon.GetExceptionFromWin32Error(errorCode);
                }

                return(null);
            }

            return(handle);
        }
Пример #8
0
 public bool GetNextDirectoryExtendedInfo(string filter, out string directoryName, out NativeMethods.WIN32_FIND_DATA findData)
 {
     return(this.GetNextItem(filter, DirectoryEnumerator.EnumerationType.Directories, out directoryName, out findData, null));
 }
Пример #9
0
        private bool GetNextItem(string filter, DirectoryEnumerator.EnumerationType enumType, out string itemName, out NativeMethods.WIN32_FIND_DATA findData, Predicate <NativeMethods.WIN32_FIND_DATA> extendedFilter = null)
        {
            bool flag  = false;
            bool flag2 = false;

            itemName              = null;
            findData              = default(NativeMethods.WIN32_FIND_DATA);
            findData.FileName     = string.Empty;
            findData.FileSizeHigh = 0U;
            findData.FileSizeLow  = 0U;
            bool flag3;

            do
            {
                if (this.m_safeFindHandle == null)
                {
                    string fileName = Path.Combine(this.m_path.FullName, filter);
                    this.m_safeFindHandle = NativeMethods.FindFirstFile(fileName, out findData);
                    if (this.m_safeFindHandle == null || this.m_safeFindHandle.IsInvalid)
                    {
                        flag3 = false;
                        int lastWin32Error = Marshal.GetLastWin32Error();
                        if (lastWin32Error != 2)
                        {
                            this.ThrowIOException(lastWin32Error, "FindFirstFile");
                        }
                    }
                    else
                    {
                        flag3 = true;
                    }
                }
                else
                {
                    flag3 = NativeMethods.FindNextFile(this.m_safeFindHandle, out findData);
                    if (!flag3)
                    {
                        int lastWin32Error2 = Marshal.GetLastWin32Error();
                        if (lastWin32Error2 != 18)
                        {
                            this.ThrowIOException(lastWin32Error2, "FindNextFile");
                        }
                    }
                }
                if (flag3)
                {
                    if ((findData.FileAttributes & NativeMethods.FileAttributes.Directory) != NativeMethods.FileAttributes.None && !this.IsSpecialDirectoryName(findData.FileName) && (extendedFilter == null || extendedFilter(findData)))
                    {
                        flag2 = true;
                    }
                    switch (enumType)
                    {
                    case DirectoryEnumerator.EnumerationType.Files:
                        if ((findData.FileAttributes & NativeMethods.FileAttributes.Directory) == NativeMethods.FileAttributes.None && NativeMethods.PathMatchSpec(findData.FileName, filter))
                        {
                            flag = true;
                        }
                        break;

                    case DirectoryEnumerator.EnumerationType.Directories:
                        if (flag2)
                        {
                            flag = true;
                        }
                        break;

                    case DirectoryEnumerator.EnumerationType.FilesOrDirectories:
                        if (flag2)
                        {
                            flag = true;
                        }
                        else if ((findData.FileAttributes & NativeMethods.FileAttributes.Directory) == NativeMethods.FileAttributes.None && NativeMethods.PathMatchSpec(findData.FileName, filter))
                        {
                            flag = true;
                        }
                        break;
                    }
                    if (this.m_recurse && flag2)
                    {
                        this.m_realDirectories.Add(findData.FileName);
                    }
                    if (flag && extendedFilter != null && !extendedFilter(findData))
                    {
                        flag = false;
                    }
                }
            }while (flag3 && !flag);
            if (flag)
            {
                if (this.ReturnBaseNames)
                {
                    itemName = findData.FileName;
                }
                else
                {
                    itemName = Path.Combine(this.m_path.FullName, findData.FileName);
                }
            }
            else
            {
                this.ResetFindHandle();
            }
            return(flag);
        }
Пример #10
0
        private static ExDateTime GetFileCreateTimeUtc(ref NativeMethods.WIN32_FIND_DATA findData)
        {
            long fileTime = ((long)findData.CreationTime.dwHighDateTime << 32) + (long)findData.CreationTime.dwLowDateTime;

            return(ExDateTime.FromFileTimeUtc(fileTime));
        }
Пример #11
0
 internal File(string fileName, NativeMethods.WIN32_FIND_DATA fileData)
     : base(fileName)
 {
     findData = fileData;
 }