SMB1FileSystemHelper.Find.cs 4.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. /* Copyright (C) 2014-2017 Tal Aloni <tal.aloni.il@gmail.com>. All rights reserved.
  2. *
  3. * You can redistribute this program and/or modify it under the terms of
  4. * the GNU Lesser Public License as published by the Free Software Foundation,
  5. * either version 3 of the License, or (at your option) any later version.
  6. */
  7. using System;
  8. using System.Collections.Generic;
  9. using System.Text;
  10. using SMBLibrary.SMB1;
  11. using Utilities;
  12. namespace SMBLibrary.Server.SMB1
  13. {
  14. public partial class SMB1FileSystemHelper
  15. {
  16. /// <exception cref="SMBLibrary.UnsupportedInformationLevelException"></exception>
  17. public static FindInformationList GetFindInformationList(List<FileSystemEntry> entries, FindInformationLevel informationLevel, bool isUnicode, bool returnResumeKeys, int maxLength)
  18. {
  19. FindInformationList result = new FindInformationList();
  20. for (int index = 0; index < entries.Count; index++)
  21. {
  22. FindInformation infoEntry = GetFindInformation(entries[index], informationLevel, isUnicode, returnResumeKeys);
  23. result.Add(infoEntry);
  24. if (result.GetLength(isUnicode) > maxLength)
  25. {
  26. result.RemoveAt(result.Count - 1);
  27. break;
  28. }
  29. }
  30. return result;
  31. }
  32. /// <exception cref="SMBLibrary.UnsupportedInformationLevelException"></exception>
  33. public static FindInformation GetFindInformation(FileSystemEntry entry, FindInformationLevel informationLevel, bool isUnicode, bool returnResumeKeys)
  34. {
  35. switch (informationLevel)
  36. {
  37. case FindInformationLevel.SMB_FIND_FILE_DIRECTORY_INFO:
  38. {
  39. FindFileDirectoryInfo result = new FindFileDirectoryInfo();
  40. result.CreationTime = entry.CreationTime;
  41. result.LastAccessTime = entry.LastAccessTime;
  42. result.LastWriteTime = entry.LastWriteTime;
  43. result.LastAttrChangeTime = entry.LastWriteTime;
  44. result.EndOfFile = entry.Size;
  45. result.AllocationSize = NTFileSystemHelper.GetAllocationSize(entry.Size);
  46. result.ExtFileAttributes = GetExtendedFileAttributes(entry);
  47. result.FileName = entry.Name;
  48. return result;
  49. }
  50. case FindInformationLevel.SMB_FIND_FILE_FULL_DIRECTORY_INFO:
  51. {
  52. FindFileFullDirectoryInfo result = new FindFileFullDirectoryInfo();
  53. result.CreationTime = entry.CreationTime;
  54. result.LastAccessTime = entry.LastAccessTime;
  55. result.LastWriteTime = entry.LastWriteTime;
  56. result.LastAttrChangeTime = entry.LastWriteTime;
  57. result.EndOfFile = entry.Size;
  58. result.AllocationSize = NTFileSystemHelper.GetAllocationSize(entry.Size);
  59. result.ExtFileAttributes = GetExtendedFileAttributes(entry);
  60. result.FileName = entry.Name;
  61. return result;
  62. }
  63. case FindInformationLevel.SMB_FIND_FILE_NAMES_INFO:
  64. {
  65. FindFileNamesInfo result = new FindFileNamesInfo();
  66. result.FileName = entry.Name;
  67. return result;
  68. }
  69. case FindInformationLevel.SMB_FIND_FILE_BOTH_DIRECTORY_INFO:
  70. {
  71. FindFileBothDirectoryInfo result = new FindFileBothDirectoryInfo();
  72. result.CreationTime = entry.CreationTime;
  73. result.LastAccessTime = entry.LastAccessTime;
  74. result.LastWriteTime = entry.LastWriteTime;
  75. result.LastChangeTime = entry.LastWriteTime;
  76. result.EndOfFile = entry.Size;
  77. result.AllocationSize = NTFileSystemHelper.GetAllocationSize(entry.Size);
  78. result.ExtFileAttributes = GetExtendedFileAttributes(entry);
  79. result.ShortName = NTFileSystemHelper.GetShortName(entry.Name);
  80. result.FileName = entry.Name;
  81. return result;
  82. }
  83. default:
  84. {
  85. throw new UnsupportedInformationLevelException();
  86. }
  87. }
  88. }
  89. }
  90. }