msg_file_set_info.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. package smb
  2. import (
  3. "context"
  4. "os"
  5. "strings"
  6. "github.com/izouxv/logx"
  7. "imuslab.com/smb/driver/mod/smb/encoder"
  8. )
  9. func init() {
  10. commandRequestMap[CommandSetInfo] = func() DataI {
  11. return &SetInfoRequest{}
  12. }
  13. }
  14. type SetInfoRequest struct {
  15. Header
  16. StructureSize uint16
  17. InfoType InfoType
  18. FileInfoClass FileInformationClass //https://learn.microsoft.com/en-us/openspecs/windows_protocols/ms-fscc/4718fc40-e539-4014-8e33-b675af74e3e1
  19. BufferLength uint32 `smb:"len:Buffer"`
  20. BufferOffset uint16 `smb:"offset:Buffer"`
  21. Reserved uint16
  22. AdditionalInformation uint32
  23. FileId GUID
  24. Buffer []byte
  25. }
  26. type SetInfoResponse struct {
  27. Header
  28. StructureSize uint16
  29. }
  30. func (data *SetInfoRequest) ServerAction(ctx *DataCtx) (interface{}, error) {
  31. data.Header.Flags = SMB2_FLAGS_RESPONSE
  32. // if !data.FileId.IsEqual(LastGUID) {
  33. // panic(-1)
  34. // }
  35. if data.InfoType != SMB2_0_INFO_FILE {
  36. logx.Warnf("data.InfoType NotSupport: %v", data.InfoType)
  37. return ERR(data.Header, STATUS_NOT_SUPPORTED)
  38. }
  39. if len(data.Buffer) > 0 {
  40. fileid := ctx.FileID(data.FileId)
  41. webfile, ok := ctx.session.openedFiles[fileid]
  42. if !ok {
  43. return ERR(data.Header, STATUS_FILE_CLOSED)
  44. }
  45. switch data.FileInfoClass {
  46. case FileBasicInformation:
  47. resp := &FileBasicInformationX{}
  48. if err := encoder.Unmarshal(data.Buffer, resp); err == nil {
  49. ctx.closeAction = func() {
  50. }
  51. }
  52. case FileDispositionInformation:
  53. resp := &FileDispositionInformationX{}
  54. if err := encoder.Unmarshal(data.Buffer, resp); err == nil {
  55. if file, ok := webfile.(*os.File); ok {
  56. //删除文件.
  57. if resp.DeletePending == 1 {
  58. ctx.closeAction = func() {
  59. FilePath := file.Name()
  60. ctx.Handle().FileSystem.RemoveAll(context.Background(), FilePath)
  61. }
  62. }
  63. }
  64. if file, ok := webfile.(*webdavFile); ok {
  65. //删除文件xattr属性
  66. ctx.closeAction = func() {
  67. file.XAttrDelete()
  68. }
  69. }
  70. }
  71. case FileRenameInformation:
  72. resp := &FileRenameInformationX{}
  73. if err := encoder.Unmarshal(data.Buffer, resp); err == nil {
  74. if file, ok := webfile.(*os.File); ok {
  75. FilePath := file.Name()
  76. // ctx.closeAction = func() {
  77. filename, err := encoder.FromUnicode(resp.FileName)
  78. if err != nil {
  79. return ERR(data.Header, STATUS_UNSUCCESSFUL)
  80. }
  81. filename = strings.ReplaceAll(filename, "\\", "/")
  82. NewFilePath := ctx.session.GetAbsPath(filename)
  83. if resp.ReplaceIfExists == 0x01 {
  84. err = ctx.Handle().FileSystem.RemoveAll(context.Background(), NewFilePath)
  85. if err != nil {
  86. return ERR(data.Header, STATUS_UNSUCCESSFUL)
  87. }
  88. }
  89. err = ctx.Handle().FileSystem.Rename(context.Background(), FilePath, NewFilePath)
  90. if err != nil {
  91. return ERR(data.Header, STATUS_UNSUCCESSFUL)
  92. }
  93. // }
  94. }
  95. }
  96. default:
  97. logx.Warnf("data.FileInfoClass NotSupport: %v", data.FileInfoClass)
  98. return ERR(data.Header, STATUS_NOT_SUPPORTED)
  99. }
  100. }
  101. data.Header.Status = StatusOk
  102. resp := SetInfoResponse{
  103. Header: data.Header,
  104. StructureSize: 2,
  105. }
  106. return &resp, nil
  107. }