Changeset 1033


Ignore:
Timestamp:
5/9/2009 12:04:49 PM (6 years ago)
Author:
lowjoel
Message:

Allow date Created, date Accessed and date Modified values to be set on streams.

Location:
branches/eraser6
Files:
6 edited

Legend:

Unmodified
Added
Removed
  • branches/eraser6/Manager/DirectExecutor.cs

    r1031 r1033  
    698698            //Get the file access times 
    699699            StreamInfo streamInfo = new StreamInfo(file); 
    700             FileInfo fileInfo = streamInfo.File; 
    701             if (fileInfo == null) 
    702                 throw new ArgumentException(S._("The file provided does not exist.")); 
    703  
    704             DateTime lastAccess = fileInfo.LastAccessTime; 
    705             DateTime lastWrite = fileInfo.LastWriteTime; 
    706             DateTime created = fileInfo.CreationTime; 
     700            DateTime lastAccess = streamInfo.LastAccessTime; 
     701            DateTime lastWrite = streamInfo.LastWriteTime; 
     702            DateTime created = streamInfo.CreationTime; 
    707703 
    708704            //And get the file lengths to know how much to overwrite 
     
    740736            { 
    741737                //Reset the file times 
    742                 fileInfo.LastAccessTime = lastAccess; 
    743                 fileInfo.LastWriteTime = lastWrite; 
    744                 fileInfo.CreationTime = created; 
     738                streamInfo.LastAccessTime = lastAccess; 
     739                streamInfo.LastWriteTime = lastWrite; 
     740                streamInfo.CreationTime = created; 
    745741            } 
    746742        } 
  • branches/eraser6/Manager/Strings.en.resx

    r1032 r1033  
    169169    <value>{0} did not have its cluster tips erased. The error returned was: {1}</value> 
    170170  </data> 
    171   <data name="The file provided does not exist." xml:space="preserve"> 
    172     <value>The file provided does not exist.</value> 
    173   </data> 
    174171  <data name="Erasing files..." xml:space="preserve"> 
    175172    <value>Erasing files...</value> 
  • branches/eraser6/Manager/Strings.nl.resx

    r1032 r1033  
    169169    <value>(Untranslated)</value> 
    170170  </data> 
    171   <data name="The file provided does not exist." xml:space="preserve"> 
    172     <value>(Untranslated)</value> 
    173   </data> 
    174171  <data name="Erasing files..." xml:space="preserve"> 
    175172    <value>(Untranslated)</value> 
  • branches/eraser6/Manager/Strings.resx

    r1032 r1033  
    169169    <value>{0} did not have its cluster tips erased. The error returned was: {1}</value> 
    170170  </data> 
    171   <data name="The file provided does not exist." xml:space="preserve"> 
    172     <value>The file provided does not exist.</value> 
    173   </data> 
    174171  <data name="Erasing files..." xml:space="preserve"> 
    175172    <value>Erasing files...</value> 
  • branches/eraser6/Util/KernelApi.cs

    r1005 r1033  
    2626using System.Runtime.InteropServices; 
    2727using Microsoft.Win32.SafeHandles; 
     28using System.ComponentModel; 
    2829 
    2930namespace Eraser.Util 
     
    8384        { 
    8485            return NativeMethods.FreeConsole(); 
     86        } 
     87 
     88        private static DateTime FileTimeToDateTime(System.Runtime.InteropServices.ComTypes.FILETIME value) 
     89        { 
     90            long time = (long)((((ulong)value.dwHighDateTime) << sizeof(int) * 8) | 
     91                (uint)value.dwLowDateTime); 
     92            return DateTime.FromFileTime(time); 
     93        } 
     94 
     95        private static System.Runtime.InteropServices.ComTypes.FILETIME DateTimeToFileTime(DateTime value) 
     96        { 
     97            long time = value.ToFileTime(); 
     98 
     99            System.Runtime.InteropServices.ComTypes.FILETIME result = 
     100                new System.Runtime.InteropServices.ComTypes.FILETIME(); 
     101            result.dwLowDateTime = (int)(time & 0xFFFFFFFFL); 
     102            result.dwHighDateTime = (int)(time >> 32); 
     103 
     104            return result; 
     105        } 
     106 
     107        public static void GetFileTime(SafeFileHandle file, out DateTime creationTime, 
     108            out DateTime accessedTime, out DateTime modifiedTime) 
     109        { 
     110            System.Runtime.InteropServices.ComTypes.FILETIME accessedTimeNative = 
     111                new System.Runtime.InteropServices.ComTypes.FILETIME(); 
     112            System.Runtime.InteropServices.ComTypes.FILETIME modifiedTimeNative = 
     113                new System.Runtime.InteropServices.ComTypes.FILETIME(); 
     114            System.Runtime.InteropServices.ComTypes.FILETIME createdTimeNative = 
     115                new System.Runtime.InteropServices.ComTypes.FILETIME(); 
     116 
     117            if (!NativeMethods.GetFileTime(file, out createdTimeNative, out accessedTimeNative, 
     118                out modifiedTimeNative)) 
     119            { 
     120                throw new Win32Exception(Marshal.GetLastWin32Error()); 
     121            } 
     122 
     123            creationTime = FileTimeToDateTime(createdTimeNative); 
     124            accessedTime = FileTimeToDateTime(accessedTimeNative); 
     125            modifiedTime = FileTimeToDateTime(modifiedTimeNative); 
     126        } 
     127 
     128        public static void SetFileTime(SafeFileHandle file, DateTime creationTime, 
     129            DateTime accessedTime, DateTime modifiedTime) 
     130        { 
     131            System.Runtime.InteropServices.ComTypes.FILETIME accessedTimeNative = 
     132                new System.Runtime.InteropServices.ComTypes.FILETIME(); 
     133            System.Runtime.InteropServices.ComTypes.FILETIME modifiedTimeNative = 
     134                new System.Runtime.InteropServices.ComTypes.FILETIME(); 
     135            System.Runtime.InteropServices.ComTypes.FILETIME createdTimeNative = 
     136                new System.Runtime.InteropServices.ComTypes.FILETIME(); 
     137 
     138            if (!NativeMethods.GetFileTime(file, out createdTimeNative, 
     139                out accessedTimeNative, out modifiedTimeNative)) 
     140            { 
     141                throw new Win32Exception(Marshal.GetLastWin32Error()); 
     142            } 
     143 
     144            if (creationTime != DateTime.MinValue) 
     145                createdTimeNative = DateTimeToFileTime(creationTime); 
     146            if (accessedTime != DateTime.MinValue) 
     147                accessedTimeNative = DateTimeToFileTime(accessedTime); 
     148            if (modifiedTime != DateTime.MinValue) 
     149                modifiedTimeNative = DateTimeToFileTime(modifiedTime); 
     150 
     151            if (!NativeMethods.SetFileTime(file, ref createdTimeNative, 
     152                ref accessedTimeNative, ref modifiedTimeNative)) 
     153            { 
     154                throw new Win32Exception(Marshal.GetLastWin32Error()); 
     155            } 
    85156        } 
    86157 
     
    683754 
    684755            /// <summary> 
     756            /// Retrieves the date and time that a file or directory was created, last 
     757            /// accessed, and last modified. 
     758            /// </summary> 
     759            /// <param name="hFile">A handle to the file or directory for which dates 
     760            /// and times are to be retrieved. The handle must have been created using 
     761            /// the CreateFile function with the GENERIC_READ access right. For more 
     762            /// information, see File Security and Access Rights.</param> 
     763            /// <param name="lpCreationTime">A pointer to a FILETIME structure to 
     764            /// receive the date and time the file or directory was created. This 
     765            /// parameter can be NULL if the application does not require this 
     766            /// information.</param> 
     767            /// <param name="lpLastAccessTime">A pointer to a FILETIME structure to 
     768            /// receive the date and time the file or directory was last accessed. The 
     769            /// last access time includes the last time the file or directory was 
     770            /// written to, read from, or, in the case of executable files, run. This 
     771            /// parameter can be NULL if the application does not require this 
     772            /// information.</param> 
     773            /// <param name="lpLastWriteTime">A pointer to a FILETIME structure to 
     774            /// receive the date and time the file or directory was last written to, 
     775            /// truncated, or overwritten (for example, with WriteFile or SetEndOfFile). 
     776            /// This date and time is not updated when file attributes or security 
     777            /// descriptors are changed. This parameter can be NULL if the application 
     778            /// does not require this information.</param> 
     779            /// <returns>If the function succeeds, the return value is nonzero. 
     780            ///  
     781            /// If the function fails, the return value is zero. To get extended error 
     782            /// information, call Marshal.GetLastWin32Error().</returns> 
     783            [DllImport("Kernel32.dll", SetLastError = true)] 
     784            [return: MarshalAs(UnmanagedType.Bool)] 
     785            public static extern bool GetFileTime(SafeFileHandle hFile, 
     786                out System.Runtime.InteropServices.ComTypes.FILETIME lpCreationTime, 
     787                out System.Runtime.InteropServices.ComTypes.FILETIME lpLastAccessTime, 
     788                out System.Runtime.InteropServices.ComTypes.FILETIME lpLastWriteTime); 
     789 
     790            /// <summary> 
     791            /// Sets the date and time that the specified file or directory was created, 
     792            /// last accessed, or last modified. 
     793            /// </summary> 
     794            /// <param name="hFile">A handle to the file or directory. The handle must 
     795            /// have been created using the CreateFile function with the 
     796            /// FILE_WRITE_ATTRIBUTES access right. For more information, see File 
     797            /// Security and Access Rights.</param> 
     798            /// <param name="lpCreationTime">A pointer to a FILETIME structure that 
     799            /// contains the new creation date and time for the file or directory. 
     800            /// This parameter can be NULL if the application does not need to change 
     801            /// this information.</param> 
     802            /// <param name="lpLastAccessTime">A pointer to a FILETIME structure that 
     803            /// contains the new last access date and time for the file or directory. 
     804            /// The last access time includes the last time the file or directory was 
     805            /// written to, read from, or (in the case of executable files) run. This 
     806            /// parameter can be NULL if the application does not need to change this 
     807            /// information. 
     808            ///  
     809            /// To preserve the existing last access time for a file even after accessing 
     810            /// a file, call SetFileTime immediately after opening the file handle 
     811            /// with this parameter's FILETIME structure members initialized to 
     812            /// 0xFFFFFFFF.</param> 
     813            /// <param name="lpLastWriteTime">A pointer to a FILETIME structure that 
     814            /// contains the new last modified date and time for the file or directory. 
     815            /// This parameter can be NULL if the application does not need to change 
     816            /// this information.</param> 
     817            /// <returns>If the function succeeds, the return value is nonzero. 
     818            ///  
     819            /// If the function fails, the return value is zero. To get extended error 
     820            /// information, call GetLastError.</returns> 
     821            [DllImport("Kernel32.dll", SetLastError = true)] 
     822            [return: MarshalAs(UnmanagedType.Bool)] 
     823            public static extern bool SetFileTime(SafeFileHandle hFile, 
     824                ref System.Runtime.InteropServices.ComTypes.FILETIME lpCreationTime, 
     825                ref System.Runtime.InteropServices.ComTypes.FILETIME lpLastAccessTime, 
     826                ref System.Runtime.InteropServices.ComTypes.FILETIME lpLastWriteTime); 
     827 
     828            /// <summary> 
    685829            /// Retrieves the name of a volume on a computer. FindFirstVolume is used 
    686830            /// to begin scanning the volumes of a computer. 
  • branches/eraser6/Util/StreamInfo.cs

    r1031 r1033  
    9090 
    9191        /// <summary> 
     92        /// Gets the name of the file. 
     93        /// </summary> 
     94        public string Name 
     95        { 
     96            get { return fileName; } 
     97        } 
     98 
     99        /// <summary> 
    92100        /// Gets an instance of the main file. If this object refers to an ADS, the 
    93101        /// result is null. 
     
    160168        } 
    161169 
    162         /// <summary> 
    163         /// Gets the name of the file. 
    164         /// </summary> 
    165         public string Name 
    166         { 
    167             get { return fileName; } 
     170        public DateTime LastAccessTime 
     171        { 
     172            get 
     173            { 
     174                DateTime creationTime, lastAccess, lastWrite; 
     175                using (SafeFileHandle handle = fileHandle) 
     176                    KernelApi.GetFileTime(handle, out creationTime, out lastAccess, 
     177                        out lastWrite); 
     178                return lastAccess; 
     179            } 
     180            set 
     181            { 
     182                using (SafeFileHandle handle = fileHandle) 
     183                    KernelApi.SetFileTime(handle, DateTime.MinValue, value, DateTime.MinValue); 
     184            } 
     185        } 
     186 
     187        public DateTime LastWriteTime 
     188        { 
     189            get 
     190            { 
     191                DateTime creationTime, lastAccess, lastWrite; 
     192                using (SafeFileHandle handle = fileHandle) 
     193                    KernelApi.GetFileTime(handle, out creationTime, out lastAccess, 
     194                        out lastWrite); 
     195                return lastWrite; 
     196            } 
     197            set 
     198            { 
     199                using (SafeFileHandle handle = fileHandle) 
     200                    KernelApi.SetFileTime(handle, DateTime.MinValue, DateTime.MinValue, value); 
     201            } 
     202        } 
     203 
     204        public DateTime CreationTime 
     205        { 
     206            get 
     207            { 
     208                DateTime creationTime, lastAccess, lastWrite; 
     209                using (SafeFileHandle handle = fileHandle) 
     210                    KernelApi.GetFileTime(handle, out creationTime, out lastAccess, 
     211                        out lastWrite); 
     212                return creationTime; 
     213            } 
     214            set 
     215            { 
     216                using (SafeFileHandle handle = fileHandle) 
     217                    KernelApi.SetFileTime(handle, value, DateTime.MinValue, DateTime.MinValue); 
     218            } 
    168219        } 
    169220 
Note: See TracChangeset for help on using the changeset viewer.