NTFileSystemHelper.Set.cs 6.0 KB

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