Browse Source

FileBasicInformation, SMB_SET_FILE_BASIC_INFO: Bugfix: InvalidDataException was thrown if FILETIME value was set to -1

Tal Aloni 7 years ago
parent
commit
2b14375876

+ 20 - 0
SMBLibrary/Helpers/FileTimeHelper.cs

@@ -83,5 +83,25 @@ namespace SMBLibrary
             WriteFileTime(buffer, offset, time);
             offset += 8;
         }
+
+        /// <summary>
+        /// When setting file attributes, a value of -1 indicates to the server that it MUST NOT change this attribute for all subsequent operations on the same file handle.
+        /// </summary>
+        public static DateTime? ReadSetFileTime(byte[] buffer, int offset)
+        {
+            long span = LittleEndianConverter.ToInt64(buffer, offset);
+            if (span > 0)
+            {
+                return DateTime.FromFileTimeUtc(span);
+            }
+            else if (span == 0 || span == -1)
+            {
+                return null;
+            }
+            else
+            {
+                throw new System.IO.InvalidDataException("Set FILETIME cannot be set to a value less than -1");
+            }
+        }
     }
 }

+ 4 - 4
SMBLibrary/NTFileStore/Structures/FileInformation/Query/FileBasicInformation.cs

@@ -30,10 +30,10 @@ namespace SMBLibrary
 
         public FileBasicInformation(byte[] buffer, int offset)
         {
-            CreationTime = FileTimeHelper.ReadNullableFileTime(buffer, offset + 0);
-            LastAccessTime = FileTimeHelper.ReadNullableFileTime(buffer, offset + 8);
-            LastWriteTime = FileTimeHelper.ReadNullableFileTime(buffer, offset + 16);
-            ChangeTime = FileTimeHelper.ReadNullableFileTime(buffer, offset + 24);
+            CreationTime = FileTimeHelper.ReadSetFileTime(buffer, offset + 0);
+            LastAccessTime = FileTimeHelper.ReadSetFileTime(buffer, offset + 8);
+            LastWriteTime = FileTimeHelper.ReadSetFileTime(buffer, offset + 16);
+            ChangeTime = FileTimeHelper.ReadSetFileTime(buffer, offset + 24);
             FileAttributes = (FileAttributes)LittleEndianConverter.ToUInt32(buffer, offset + 32);
             Reserved = LittleEndianConverter.ToUInt32(buffer, offset + 36);
         }

+ 4 - 5
SMBLibrary/SMB1FileStore/Structures/SetInformation/SetFileBasicInfo.cs

@@ -6,7 +6,6 @@
  */
 using System;
 using System.Collections.Generic;
-using System.Text;
 using Utilities;
 
 namespace SMBLibrary.SMB1
@@ -35,10 +34,10 @@ namespace SMBLibrary.SMB1
 
         public SetFileBasicInfo(byte[] buffer, int offset)
         {
-            CreationTime = FileTimeHelper.ReadNullableFileTime(buffer, offset + 0);
-            LastAccessTime = FileTimeHelper.ReadNullableFileTime(buffer, offset + 8);
-            LastWriteTime = FileTimeHelper.ReadNullableFileTime(buffer, offset + 16);
-            LastChangeTime = FileTimeHelper.ReadNullableFileTime(buffer, offset + 24);
+            CreationTime = FileTimeHelper.ReadSetFileTime(buffer, offset + 0);
+            LastAccessTime = FileTimeHelper.ReadSetFileTime(buffer, offset + 8);
+            LastWriteTime = FileTimeHelper.ReadSetFileTime(buffer, offset + 16);
+            LastChangeTime = FileTimeHelper.ReadSetFileTime(buffer, offset + 24);
             ExtFileAttributes = (ExtendedFileAttributes)LittleEndianConverter.ToUInt32(buffer, offset + 32);
             Reserved = LittleEndianConverter.ToUInt32(buffer, offset + 36);
         }