NTFileSystemAdapter.Set.cs 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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.IO;
  10. using Utilities;
  11. namespace SMBLibrary
  12. {
  13. public partial class NTFileSystemAdapter
  14. {
  15. public NTStatus SetFileInformation(object handle, FileInformation information)
  16. {
  17. FileHandle fileHandle = (FileHandle)handle;
  18. if (information is FileBasicInformation)
  19. {
  20. FileBasicInformation basicInformation = (FileBasicInformation)information;
  21. bool isHidden = ((basicInformation.FileAttributes & FileAttributes.Hidden) > 0);
  22. bool isReadonly = (basicInformation.FileAttributes & FileAttributes.ReadOnly) > 0;
  23. bool isArchived = (basicInformation.FileAttributes & FileAttributes.Archive) > 0;
  24. try
  25. {
  26. m_fileSystem.SetAttributes(fileHandle.Path, isHidden, isReadonly, isArchived);
  27. }
  28. catch (Exception ex)
  29. {
  30. NTStatus status = ToNTStatus(ex);
  31. Log(Severity.Debug, "SetFileInformation: Failed to set file attributes on '{0}'. {1}.", fileHandle.Path, status);
  32. return status;
  33. }
  34. try
  35. {
  36. m_fileSystem.SetDates(fileHandle.Path, basicInformation.CreationTime, basicInformation.LastWriteTime, basicInformation.LastAccessTime);
  37. }
  38. catch (Exception ex)
  39. {
  40. NTStatus status = ToNTStatus(ex);
  41. Log(Severity.Debug, "SetFileInformation: Failed to set file dates on '{0}'. {1}.", fileHandle.Path, status);
  42. return status;
  43. }
  44. return NTStatus.STATUS_SUCCESS;
  45. }
  46. else if (information is FileRenameInformationType2)
  47. {
  48. FileRenameInformationType2 renameInformation = (FileRenameInformationType2)information;
  49. string destination = renameInformation.FileName;
  50. if (!destination.StartsWith(@"\"))
  51. {
  52. destination = @"\" + destination;
  53. }
  54. if (fileHandle.Stream != null)
  55. {
  56. fileHandle.Stream.Close();
  57. }
  58. // Note: it's possible that we just want to upcase / downcase a filename letter.
  59. try
  60. {
  61. if (renameInformation.ReplaceIfExists && (m_fileSystem.GetEntry(destination) != null))
  62. {
  63. m_fileSystem.Delete(destination);
  64. }
  65. m_fileSystem.Move(fileHandle.Path, destination);
  66. }
  67. catch (Exception ex)
  68. {
  69. NTStatus status = ToNTStatus(ex);
  70. Log(Severity.Debug, "SetFileInformation: Cannot rename '{0}'. {1}.", fileHandle.Path, status);
  71. return status;
  72. }
  73. fileHandle.Path = destination;
  74. return NTStatus.STATUS_SUCCESS;
  75. }
  76. else if (information is FileDispositionInformation)
  77. {
  78. if (((FileDispositionInformation)information).DeletePending)
  79. {
  80. // We're supposed to delete the file on close, but it's too late to report errors at this late stage
  81. if (fileHandle.Stream != null)
  82. {
  83. fileHandle.Stream.Close();
  84. }
  85. try
  86. {
  87. Log(Severity.Information, "SetFileInformation: Deleting file '{0}'", fileHandle.Path);
  88. m_fileSystem.Delete(fileHandle.Path);
  89. }
  90. catch (Exception ex)
  91. {
  92. NTStatus status = ToNTStatus(ex);
  93. Log(Severity.Information, "SetFileInformation: Error deleting '{0}'. {1}.", status);
  94. return status;
  95. }
  96. }
  97. return NTStatus.STATUS_SUCCESS;
  98. }
  99. else if (information is FileAllocationInformation)
  100. {
  101. long allocationSize = ((FileAllocationInformation)information).AllocationSize;
  102. try
  103. {
  104. fileHandle.Stream.SetLength(allocationSize);
  105. }
  106. catch (Exception ex)
  107. {
  108. NTStatus status = ToNTStatus(ex);
  109. Log(Severity.Debug, "SetFileInformation: Cannot set allocation for '{0}'. {1}.", fileHandle.Path, status);
  110. return status;
  111. }
  112. return NTStatus.STATUS_SUCCESS;
  113. }
  114. else if (information is FileEndOfFileInformation)
  115. {
  116. long endOfFile = ((FileEndOfFileInformation)information).EndOfFile;
  117. try
  118. {
  119. fileHandle.Stream.SetLength(endOfFile);
  120. }
  121. catch (Exception ex)
  122. {
  123. NTStatus status = ToNTStatus(ex);
  124. Log(Severity.Debug, "SetFileInformation: Cannot set end of file for '{0}'. {1}.", fileHandle.Path, status);
  125. return status;
  126. }
  127. return NTStatus.STATUS_SUCCESS;
  128. }
  129. else
  130. {
  131. return NTStatus.STATUS_NOT_IMPLEMENTED;
  132. }
  133. }
  134. }
  135. }