CloseHelper.cs 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. /* Copyright (C) 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 SMBLibrary.SMB2;
  11. using Utilities;
  12. namespace SMBLibrary.Server.SMB2
  13. {
  14. internal class CloseHelper
  15. {
  16. internal static SMB2Command GetCloseResponse(CloseRequest request, ISMBShare share, SMB2ConnectionState state)
  17. {
  18. SMB2Session session = state.GetSession(request.Header.SessionID);
  19. OpenFileObject openFile = session.GetOpenFileObject(request.FileId);
  20. if (openFile == null)
  21. {
  22. return new ErrorResponse(request.CommandName, NTStatus.STATUS_FILE_CLOSED);
  23. }
  24. state.LogToServer(Severity.Information, "Close: Closing '{0}{1}'", share.Name, openFile.Path);
  25. NTStatus closeStatus = share.FileStore.CloseFile(openFile.Handle);
  26. if (closeStatus != NTStatus.STATUS_SUCCESS)
  27. {
  28. return new ErrorResponse(request.CommandName, closeStatus);
  29. }
  30. session.RemoveOpenFile(request.FileId);
  31. CloseResponse response = new CloseResponse();
  32. if (request.PostQueryAttributes)
  33. {
  34. FileNetworkOpenInformation fileInfo = NTFileStoreHelper.GetNetworkOpenInformation(share.FileStore, openFile.Path, session.SecurityContext);
  35. if (fileInfo != null)
  36. {
  37. response.CreationTime = fileInfo.CreationTime;
  38. response.LastAccessTime = fileInfo.LastAccessTime;
  39. response.LastWriteTime = fileInfo.LastWriteTime;
  40. response.ChangeTime = fileInfo.ChangeTime;
  41. response.AllocationSize = fileInfo.AllocationSize;
  42. response.EndofFile = fileInfo.EndOfFile;
  43. response.FileAttributes = fileInfo.FileAttributes;
  44. }
  45. }
  46. return response;
  47. }
  48. }
  49. }