123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164 |
- /* Copyright (C) 2014-2017 Tal Aloni <tal.aloni.il@gmail.com>. All rights reserved.
- *
- * You can redistribute this program and/or modify it under the terms of
- * the GNU Lesser Public License as published by the Free Software Foundation,
- * either version 3 of the License, or (at your option) any later version.
- */
- using System;
- using System.Collections.Generic;
- using SMBLibrary.SMB1;
- using Utilities;
- namespace SMBLibrary.SMB1
- {
- public class FindInformationHelper
- {
- /// <exception cref="SMBLibrary.UnsupportedInformationLevelException"></exception>
- public static FileInformationClass ToFileInformationClass(FindInformationLevel informationLevel)
- {
- switch (informationLevel)
- {
- case FindInformationLevel.SMB_FIND_FILE_DIRECTORY_INFO:
- return FileInformationClass.FileDirectoryInformation;
- case FindInformationLevel.SMB_FIND_FILE_FULL_DIRECTORY_INFO:
- return FileInformationClass.FileFullDirectoryInformation;
- case FindInformationLevel.SMB_FIND_FILE_NAMES_INFO:
- return FileInformationClass.FileNamesInformation;
- case FindInformationLevel.SMB_FIND_FILE_BOTH_DIRECTORY_INFO:
- return FileInformationClass.FileBothDirectoryInformation;
- case FindInformationLevel.SMB_FIND_FILE_ID_FULL_DIRECTORY_INFO:
- return FileInformationClass.FileIdFullDirectoryInformation;
- case FindInformationLevel.SMB_FIND_FILE_ID_BOTH_DIRECTORY_INFO:
- return FileInformationClass.FileIdBothDirectoryInformation;
- default:
- throw new UnsupportedInformationLevelException();
- }
- }
- /// <exception cref="SMBLibrary.UnsupportedInformationLevelException"></exception>
- public static FindInformationList ToFindInformationList(List<QueryDirectoryFileInformation> entries, bool isUnicode, int maxLength)
- {
- FindInformationList result = new FindInformationList();
- int pageLength = 0;
- for (int index = 0; index < entries.Count; index++)
- {
- FindInformation infoEntry = ToFindInformation(entries[index]);
- int entryLength = infoEntry.GetLength(isUnicode);
- if (pageLength + entryLength <= maxLength)
- {
- result.Add(infoEntry);
- pageLength += entryLength;
- }
- else
- {
- break;
- }
- }
- return result;
- }
- /// <exception cref="SMBLibrary.UnsupportedInformationLevelException"></exception>
- public static FindInformation ToFindInformation(QueryDirectoryFileInformation fileInformation)
- {
- if (fileInformation is FileDirectoryInformation)
- {
- FileDirectoryInformation fileDirectoryInfo = (FileDirectoryInformation)fileInformation;
- FindFileDirectoryInfo result = new FindFileDirectoryInfo();
- result.FileIndex = fileDirectoryInfo.FileIndex;
- result.CreationTime = fileDirectoryInfo.CreationTime;
- result.LastAccessTime = fileDirectoryInfo.LastAccessTime;
- result.LastWriteTime = fileDirectoryInfo.LastWriteTime;
- result.LastAttrChangeTime = fileDirectoryInfo.LastWriteTime;
- result.EndOfFile = fileDirectoryInfo.EndOfFile;
- result.AllocationSize = fileDirectoryInfo.AllocationSize;
- result.ExtFileAttributes = (ExtendedFileAttributes)fileDirectoryInfo.FileAttributes;
- result.FileName = fileDirectoryInfo.FileName;
- return result;
- }
- else if (fileInformation is FileFullDirectoryInformation)
- {
- FileFullDirectoryInformation fileFullDirectoryInfo = (FileFullDirectoryInformation)fileInformation;
- FindFileFullDirectoryInfo result = new FindFileFullDirectoryInfo();
- result.FileIndex = fileFullDirectoryInfo.FileIndex;
- result.CreationTime = fileFullDirectoryInfo.CreationTime;
- result.LastAccessTime = fileFullDirectoryInfo.LastAccessTime;
- result.LastWriteTime = fileFullDirectoryInfo.LastWriteTime;
- result.LastAttrChangeTime = fileFullDirectoryInfo.LastWriteTime;
- result.EndOfFile = fileFullDirectoryInfo.EndOfFile;
- result.AllocationSize = fileFullDirectoryInfo.AllocationSize;
- result.ExtFileAttributes = (ExtendedFileAttributes)fileFullDirectoryInfo.FileAttributes;
- result.EASize = fileFullDirectoryInfo.EaSize;
- result.FileName = fileFullDirectoryInfo.FileName;
- return result;
- }
- else if (fileInformation is FileNamesInformation)
- {
- FileNamesInformation fileNamesInfo = (FileNamesInformation)fileInformation;
- FindFileNamesInfo result = new FindFileNamesInfo();
- result.FileIndex = fileNamesInfo.FileIndex;
- result.FileName = fileNamesInfo.FileName;
- return result;
- }
- else if (fileInformation is FileBothDirectoryInformation)
- {
- FileBothDirectoryInformation fileBothDirectoryInfo = (FileBothDirectoryInformation)fileInformation;
- FindFileBothDirectoryInfo result = new FindFileBothDirectoryInfo();
- result.FileIndex = fileBothDirectoryInfo.FileIndex;
- result.CreationTime = fileBothDirectoryInfo.CreationTime;
- result.LastAccessTime = fileBothDirectoryInfo.LastAccessTime;
- result.LastWriteTime = fileBothDirectoryInfo.LastWriteTime;
- result.LastChangeTime = fileBothDirectoryInfo.LastWriteTime;
- result.EndOfFile = fileBothDirectoryInfo.EndOfFile;
- result.AllocationSize = fileBothDirectoryInfo.AllocationSize;
- result.ExtFileAttributes = (ExtendedFileAttributes)fileBothDirectoryInfo.FileAttributes;
- result.EASize = fileBothDirectoryInfo.EaSize;
- result.Reserved = fileBothDirectoryInfo.Reserved;
- result.ShortName = fileBothDirectoryInfo.ShortName;
- result.FileName = fileBothDirectoryInfo.FileName;
- return result;
- }
- else if (fileInformation is FileIdFullDirectoryInformation)
- {
- FileIdFullDirectoryInformation fileIDFullDirectoryInfo = (FileIdFullDirectoryInformation)fileInformation;
- FindFileIDFullDirectoryInfo result = new FindFileIDFullDirectoryInfo();
- result.FileIndex = fileIDFullDirectoryInfo.FileIndex;
- result.CreationTime = fileIDFullDirectoryInfo.CreationTime;
- result.LastAccessTime = fileIDFullDirectoryInfo.LastAccessTime;
- result.LastWriteTime = fileIDFullDirectoryInfo.LastWriteTime;
- result.LastAttrChangeTime = fileIDFullDirectoryInfo.LastWriteTime;
- result.EndOfFile = fileIDFullDirectoryInfo.EndOfFile;
- result.AllocationSize = fileIDFullDirectoryInfo.AllocationSize;
- result.ExtFileAttributes = (ExtendedFileAttributes)fileIDFullDirectoryInfo.FileAttributes;
- result.EASize = fileIDFullDirectoryInfo.EaSize;
- result.Reserved = fileIDFullDirectoryInfo.Reserved;
- result.FileID = fileIDFullDirectoryInfo.FileId;
- result.FileName = fileIDFullDirectoryInfo.FileName;
- return result;
- }
- else if (fileInformation is FileIdBothDirectoryInformation)
- {
- FileIdBothDirectoryInformation fileIDBothDirectoryInfo = (FileIdBothDirectoryInformation)fileInformation;
- FindFileIDBothDirectoryInfo result = new FindFileIDBothDirectoryInfo();
- result.FileIndex = fileIDBothDirectoryInfo.FileIndex;
- result.CreationTime = fileIDBothDirectoryInfo.CreationTime;
- result.LastAccessTime = fileIDBothDirectoryInfo.LastAccessTime;
- result.LastWriteTime = fileIDBothDirectoryInfo.LastWriteTime;
- result.LastChangeTime = fileIDBothDirectoryInfo.LastWriteTime;
- result.EndOfFile = fileIDBothDirectoryInfo.EndOfFile;
- result.AllocationSize = fileIDBothDirectoryInfo.AllocationSize;
- result.ExtFileAttributes = (ExtendedFileAttributes)fileIDBothDirectoryInfo.FileAttributes;
- result.EASize = fileIDBothDirectoryInfo.EaSize;
- result.Reserved = fileIDBothDirectoryInfo.Reserved1;
- result.ShortName = fileIDBothDirectoryInfo.ShortName;
- result.Reserved2 = fileIDBothDirectoryInfo.Reserved2;
- result.FileID = fileIDBothDirectoryInfo.FileId;
- result.FileName = fileIDBothDirectoryInfo.FileName;
- return result;
- }
- else
- {
- throw new NotImplementedException();
- }
- }
- }
- }
|