FileBasicInformation, SMB_SET_FILE_BASIC_INFO: Added support for FILETIME value of -1

This commit is contained in:
Tal Aloni 2017-10-02 12:26:13 +03:00
parent c3a88d59ab
commit 0098cc78f5
5 changed files with 127 additions and 30 deletions

View file

@ -12,7 +12,7 @@ namespace SMBLibrary
{
public class FileTimeHelper
{
public static readonly DateTime MinFileTimeValue = new DateTime(1601, 1, 1);
public static readonly DateTime MinFileTimeValue = new DateTime(1601, 1, 1, 0, 0, 0, DateTimeKind.Utc);
public static DateTime ReadFileTime(byte[] buffer, int offset)
{
@ -87,21 +87,19 @@ namespace SMBLibrary
/// <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)
public static SetFileTime 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");
}
return SetFileTime.FromFileTimeUtc(span);
}
/// <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 void WriteSetFileTime(byte[] buffer, int offset, SetFileTime time)
{
long span = time.ToFileTimeUtc();
LittleEndianWriter.WriteInt64(buffer, offset, span);
}
}
}

View file

@ -17,10 +17,10 @@ namespace SMBLibrary
{
public const int FixedLength = 40;
public DateTime? CreationTime;
public DateTime? LastAccessTime;
public DateTime? LastWriteTime;
public DateTime? ChangeTime;
public SetFileTime CreationTime;
public SetFileTime LastAccessTime;
public SetFileTime LastWriteTime;
public SetFileTime ChangeTime;
public FileAttributes FileAttributes;
public uint Reserved;
@ -40,10 +40,10 @@ namespace SMBLibrary
public override void WriteBytes(byte[] buffer, int offset)
{
FileTimeHelper.WriteFileTime(buffer, offset + 0, CreationTime);
FileTimeHelper.WriteFileTime(buffer, offset + 8, LastAccessTime);
FileTimeHelper.WriteFileTime(buffer, offset + 16, LastWriteTime);
FileTimeHelper.WriteFileTime(buffer, offset + 24, ChangeTime);
FileTimeHelper.WriteSetFileTime(buffer, offset + 0, CreationTime);
FileTimeHelper.WriteSetFileTime(buffer, offset + 8, LastAccessTime);
FileTimeHelper.WriteSetFileTime(buffer, offset + 16, LastWriteTime);
FileTimeHelper.WriteSetFileTime(buffer, offset + 24, ChangeTime);
LittleEndianWriter.WriteUInt32(buffer, offset + 32, (uint)FileAttributes);
LittleEndianWriter.WriteUInt32(buffer, offset + 36, Reserved);
}

View file

@ -0,0 +1,98 @@
/* Copyright (C) 2017 Tal Aloni <tal.aloni.il@gmail.com>. All rights reserved.
*
* You can redistribute this program and/or modify it under the terms of
* the GNU Lesser Public License as published by the Free Software Foundation,
* either version 3 of the License, or (at your option) any later version.
*/
using System;
namespace SMBLibrary
{
/// <summary>
/// [MS-FSCC] 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 struct SetFileTime
{
public bool MustNotChange;
private DateTime? m_time;
public SetFileTime(bool mustNotChange)
{
MustNotChange = mustNotChange;
m_time = null;
}
public SetFileTime(DateTime? time)
{
MustNotChange = false;
m_time = time;
}
public long ToFileTimeUtc()
{
if (MustNotChange)
{
return -1;
}
else if (!m_time.HasValue)
{
return 0;
}
else
{
return Time.Value.ToFileTimeUtc();
}
}
public DateTime? Time
{
get
{
if (MustNotChange)
{
return null;
}
else
{
return m_time;
}
}
set
{
MustNotChange = false;
m_time = value;
}
}
public static SetFileTime FromFileTimeUtc(long span)
{
if (span > 0)
{
DateTime value = DateTime.FromFileTimeUtc(span);
return new SetFileTime(value);
}
else if (span == 0)
{
return new SetFileTime(false);
}
else if (span == -1)
{
return new SetFileTime(true);
}
else
{
throw new System.IO.InvalidDataException("Set FILETIME cannot be less than -1");
}
}
public static implicit operator DateTime?(SetFileTime setTime)
{
return setTime.Time;
}
public static implicit operator SetFileTime(DateTime? time)
{
return new SetFileTime(time);
}
}
}

View file

@ -17,10 +17,10 @@ namespace SMBLibrary.SMB1
{
public const int Length = 40;
public DateTime? CreationTime;
public DateTime? LastAccessTime;
public DateTime? LastWriteTime;
public DateTime? LastChangeTime;
public SetFileTime CreationTime;
public SetFileTime LastAccessTime;
public SetFileTime LastWriteTime;
public SetFileTime LastChangeTime;
public ExtendedFileAttributes ExtFileAttributes;
public uint Reserved;
@ -45,10 +45,10 @@ namespace SMBLibrary.SMB1
public override byte[] GetBytes()
{
byte[] buffer = new byte[Length];
FileTimeHelper.WriteFileTime(buffer, 0, CreationTime);
FileTimeHelper.WriteFileTime(buffer, 8, LastAccessTime);
FileTimeHelper.WriteFileTime(buffer, 16, LastWriteTime);
FileTimeHelper.WriteFileTime(buffer, 24, LastChangeTime);
FileTimeHelper.WriteSetFileTime(buffer, 0, CreationTime);
FileTimeHelper.WriteSetFileTime(buffer, 8, LastAccessTime);
FileTimeHelper.WriteSetFileTime(buffer, 16, LastWriteTime);
FileTimeHelper.WriteSetFileTime(buffer, 24, LastChangeTime);
LittleEndianWriter.WriteUInt32(buffer, 32, (uint)ExtFileAttributes);
LittleEndianWriter.WriteUInt32(buffer, 36, Reserved);
return buffer;

View file

@ -159,6 +159,7 @@
<Compile Include="NTFileStore\Structures\FileInformation\Set\FileRenameInformationType1.cs" />
<Compile Include="NTFileStore\Structures\FileInformation\Set\FileRenameInformationType2.cs" />
<Compile Include="NTFileStore\Structures\FileInformation\Set\FileValidDataLengthInformation.cs" />
<Compile Include="NTFileStore\Structures\FileInformation\Set\SetFileTime.cs" />
<Compile Include="NTFileStore\Structures\FileNotifyInformation.cs" />
<Compile Include="NTFileStore\Structures\FileSystemInformation\FileFsAttributeInformation.cs" />
<Compile Include="NTFileStore\Structures\FileSystemInformation\FileFsControlInformation.cs" />