QueryFSInformationHelper.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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 SMBLibrary.SMB1;
  10. using Utilities;
  11. namespace SMBLibrary.SMB1
  12. {
  13. public class QueryFSInformationHelper
  14. {
  15. /// <exception cref="SMBLibrary.UnsupportedInformationLevelException"></exception>
  16. public static FileSystemInformationClass ToFileSystemInformationClass(QueryFSInformationLevel informationLevel)
  17. {
  18. switch (informationLevel)
  19. {
  20. case QueryFSInformationLevel.SMB_QUERY_FS_VOLUME_INFO:
  21. return FileSystemInformationClass.FileFsVolumeInformation;
  22. case QueryFSInformationLevel.SMB_QUERY_FS_SIZE_INFO:
  23. return FileSystemInformationClass.FileFsSizeInformation;
  24. case QueryFSInformationLevel.SMB_QUERY_FS_DEVICE_INFO:
  25. return FileSystemInformationClass.FileFsDeviceInformation;
  26. case QueryFSInformationLevel.SMB_QUERY_FS_ATTRIBUTE_INFO:
  27. return FileSystemInformationClass.FileFsAttributeInformation;
  28. default:
  29. throw new UnsupportedInformationLevelException();
  30. }
  31. }
  32. public static QueryFSInformation FromFileSystemInformation(FileSystemInformation fsInfo)
  33. {
  34. if (fsInfo is FileFsVolumeInformation)
  35. {
  36. FileFsVolumeInformation volumeInfo = (FileFsVolumeInformation)fsInfo;
  37. QueryFSVolumeInfo result = new QueryFSVolumeInfo();
  38. result.VolumeCreationTime = volumeInfo.VolumeCreationTime;
  39. result.SerialNumber = volumeInfo.VolumeSerialNumber;
  40. result.VolumeLabel = volumeInfo.VolumeLabel;
  41. return result;
  42. }
  43. else if (fsInfo is FileFsSizeInformation)
  44. {
  45. FileFsSizeInformation fsSizeInfo = (FileFsSizeInformation)fsInfo;
  46. QueryFSSizeInfo result = new QueryFSSizeInfo();
  47. result.TotalAllocationUnits = fsSizeInfo.TotalAllocationUnits;
  48. result.TotalFreeAllocationUnits = fsSizeInfo.AvailableAllocationUnits;
  49. result.BytesPerSector = fsSizeInfo.BytesPerSector;
  50. result.SectorsPerAllocationUnit = fsSizeInfo.SectorsPerAllocationUnit;
  51. return result;
  52. }
  53. else if (fsInfo is FileFsDeviceInformation)
  54. {
  55. FileFsDeviceInformation fsDeviceInfo = (FileFsDeviceInformation)fsInfo;
  56. QueryFSDeviceInfo result = new QueryFSDeviceInfo();
  57. result.DeviceType = fsDeviceInfo.DeviceType;
  58. result.DeviceCharacteristics = fsDeviceInfo.Characteristics;
  59. return result;
  60. }
  61. else if (fsInfo is FileFsAttributeInformation)
  62. {
  63. FileFsAttributeInformation fsAttributeInfo = (FileFsAttributeInformation)fsInfo;
  64. QueryFSAttibuteInfo result = new QueryFSAttibuteInfo();
  65. result.FileSystemAttributes = fsAttributeInfo.FileSystemAttributes;
  66. result.MaxFileNameLengthInBytes = fsAttributeInfo.MaximumComponentNameLength;
  67. result.FileSystemName = fsAttributeInfo.FileSystemName;
  68. return result;
  69. }
  70. else
  71. {
  72. throw new NotImplementedException();
  73. }
  74. }
  75. }
  76. }