Unit OS8: File System Unit OS8: File System
8.5. Windows File and Directory Management
8.5. Windows File and Directory Management
Copyright Notice Copyright Notice
© 2000-2005 David A. Solomon and Mark Russinovich
© 2000-2005 David A. Solomon and Mark Russinovich
These materials are part of the
These materials are part of the Windows Operating Windows Operating System Internals Curriculum Development Kit,
System Internals Curriculum Development Kit, developed by David A. Solomon and Mark E.
developed by David A. Solomon and Mark E.
Russinovich with Andreas Polze Russinovich with Andreas Polze
Microsoft has licensed these materials from David Microsoft has licensed these materials from David Solomon Expert Seminars, Inc. for distribution to Solomon Expert Seminars, Inc. for distribution to academic organizations solely for use in academic academic organizations solely for use in academic environments (and not for commercial use)
environments (and not for commercial use)
Roadmap for Section 8.5 Roadmap for Section 8.5
Windows File I/O API - General Principles Windows File I/O API - General Principles
Moving and Copying Files Moving and Copying Files
Directory Management Directory Management
Directory Searching Directory Searching
File and Directory Attributes
File and Directory Attributes
Windows API I/O:
Windows API I/O:
File and Directory Management File and Directory Management
Windows API provides a number of straightforward Windows API provides a number of straightforward functions to manage files
functions to manage files
Delete, copy, rename files Delete, copy, rename files Create temporary file names Create temporary file names
Absolute pathnames start with drive letter or Absolute pathnames start with drive letter or server name!
server name!
It is not possible to delete an open file in Windows It is not possible to delete an open file in Windows
(but it is possible in Windows9X) (but it is possible in Windows9X)
UNIX unlink() decrements link count UNIX unlink() decrements link count
(but does not necessarily delete file) (but does not necessarily delete file)
BOOL DeleteFile (LPCTSTR lpszFileName)
Moving files Moving files
Copies the named existing file and assigns new name Copies the named existing file and assigns new name An existing file will be replaced only if
An existing file will be replaced only if
fFailIfExists == FALSE fFailIfExists == FALSE
DeleteFile() and CopyFile do not work for directories DeleteFile() and CopyFile do not work for directories Win32 does not support any file linking
Win32 does not support any file linking
(but NTFS and POSIX subsystem do) (but NTFS and POSIX subsystem do)
BOOL CopyFile( LPCTSTR lpszExistingFile, LPCTSTR lpszNewFile,
BOOL fFailIfExists );
Moving Files (contd.) Moving Files (contd.)
MoveFile() fails if the new file already exists MoveFile() fails if the new file already exists (use MoveFileEx() for existing files)
(use MoveFileEx() for existing files)
Windows 9X does not implement MoveFileEx() Windows 9X does not implement MoveFileEx() New files can be on different drives / directories New files can be on different drives / directories New directories must be on same drive
New directories must be on same drive lpszNew == NULL : existing file is deleted lpszNew == NULL : existing file is deleted fdwFlags:
fdwFlags:
MOVEFILE_REPLACE_EXISTING – replace existing destination file MOVEFILE_REPLACE_EXISTING – replace existing destination file
MOVEFILE_COPY_ALLOWED – destination may be on different volume MOVEFILE_COPY_ALLOWED – destination may be on different volume
BOOL MoveFile (LPCTSTR lpszExisting, LPCTSTR lpszNew);
BOOL MoveFileEx( LPCTSTR lpszExisting,
LPCTSTR lpszNew, DWORD fdwFlags);
Directory Management Directory Management
lpszPath points to null-terminated string with the lpszPath points to null-terminated string with the
name of the target directory name of the target directory
Only an empty directory can be rmoved Only an empty directory can be rmoved
lpsa == NULL will create a null-ACL for the new lpsa == NULL will create a null-ACL for the new
BOOL CreateDirectory( LPCTSTR lpszPath, LPSECURITY_ATTRIBUTES lpsa );
BOOL RemoveDirectory( LPCTSTR lpszPath );
Directory Management (contd.) Directory Management (contd.)
Each process has current working directory Each process has current working directory
Each individual drive keeps working directory Each individual drive keeps working directory
GetCurrentDirectory:
GetCurrentDirectory:
ccCurDir is size of buffer in characters (!) ccCurDir is size of buffer in characters (!)
Buffer to small: GetCurrentDirectory() returns required size (!) or Buffer to small: GetCurrentDirectory() returns required size (!) or zero on failure
zero on failure
Call GetCurrentDirectory twice: first to obtain size of buffer, next to Call GetCurrentDirectory twice: first to obtain size of buffer, next to obtain value
obtain value
(or use MAX_PATH constant) (or use MAX_PATH constant)
BOOL SetCurrentDirectory( LPCTSTR lpszCurDir );
DWORD GetCurrentDirectory( DWORD cchCurDir, LPTSTR lpszCurDir );
Directory Searching Directory Searching
Search a directory for files that satisfy a specified name pattern Search a directory for files that satisfy a specified name pattern
Search handles must be obtained via FindFirstFile() and closed via Search handles must be obtained via FindFirstFile() and closed via FindClose()
FindClose() FindFirstFile
FindFirstFile() examines subdirectories and file names() examines subdirectories and file names Return of INVALID_HANDLE_VALUE indicates failure Return of INVALID_HANDLE_VALUE indicates failure
Parameters:
Parameters:
lpszSearchFile points to directory/pathname that can contain wildcard lpszSearchFile points to directory/pathname that can contain wildcard characters (? and *; no regular expressions)
characters (? and *; no regular expressions)
lpffd points to data structure with access informationlpffd points to data structure with access information HANDLE FindFirstFile( LPCTSTR lpszSearchFile,
LPWIN32_FIND_DATA lpffd );
WIN32_FIND_DATA structure WIN32_FIND_DATA structure
typedef struct _WIN32_FIND_DATA { // wfd DWORD dwFileAttributes;
FILETIME ftCreationTime;
FILETIME ftLastAccessTime;
FILETIME ftLastWriteTime;
DWORD nFileSizeHigh;
DWORD nFileSizeLow;
DWORD dwReserved0;
DWORD dwReserved1;
TCHAR cFileName[ MAX_PATH ];
TCHAR cAlternateFileName[ 14 ];
Does not contain path- portion of name
DOS 8.3 name
FILE_ATTRIBUTE_ARCHIVE, FILE_ATTRIBUTE_COMPRESSED, FILE_ATTRIBUTE_DIRECTORY, FILE_ATTRIBUTE_ENCRYPTED, FILE_ATTRIBUTE_HIDDEN,.FILE_ATTRIBUTE_NORMAL, FILE_ATTRIBUTE_OFFLINE, FILE_ATTRIBUTE_READONLY, FILE_ATTRIBUTE_REPARSE_POINT, FILE_ATTRIBUTE_SPARSE_FILE, FILE_ATTRIBUTE_SYSTEM, FILE_ATTRIBUTE_TEMPORARY
Directory Searching (contd.) Directory Searching (contd.)
FindNextFile() returns FALSE in case of invalid arguments or if no FindNextFile() returns FALSE in case of invalid arguments or if no more matching files are found
more matching files are found
GetLastError() returns ERROR_NO_MORE_FILES GetLastError() returns ERROR_NO_MORE_FILES
Use FindClose() to close search handle Use FindClose() to close search handle
CloseHandle() will raise an exception CloseHandle() will raise an exception
GetFileInformationByHandle() obtains same info...
GetFileInformationByHandle() obtains same info...
Programs must do wildcard expansion on their own Programs must do wildcard expansion on their own
BOOL FindNextFile( HANDLE hFindFile, LPWIN32_FIND_DATA lpffd );
BOOL FindClose( HANDLE hFindFile );
More File and Directory Attributes More File and Directory Attributes
File times are 64-bit unsigned integers File times are 64-bit unsigned integers
(time in 100 ns units (10
(time in 100 ns units (1077 / s) since January 1, 1601) / s) since January 1, 1601)
FileTimeToSystemTime()/SystemTimeToFileTime() convert into FileTimeToSystemTime()/SystemTimeToFileTime() convert into years down to milliseconds
years down to milliseconds
(and vice versa)(and vice versa)CompareFileTime(), SetFileTime() CompareFileTime(), SetFileTime() NTFS supports all three file times NTFS supports all three file times
FAT is accurate only for LastAccess-time FAT is accurate only for LastAccess-time
BOOL GetFileTime( HANDLE hFiles, LPFILETIME lpftCreation, LPFILETIME lpftLastAccess, LPFILETIME lpftLastWrite );
File Attributes (contd.) File Attributes (contd.)
Returns file attribute or 0xFFFFFFFF in case of failure Returns file attribute or 0xFFFFFFFF in case of failure Attributes include:
Attributes include:
FILE_ATTRIBUTE_DIRECTORY FILE_ATTRIBUTE_DIRECTORY FILE_ATTRIBUTE_NORMAL FILE_ATTRIBUTE_NORMAL FILE_ATTRIBUTE_READONLY FILE_ATTRIBUTE_READONLY FILE_ATTRIBUTE_TEMPORARY FILE_ATTRIBUTE_TEMPORARY
SetFileAttribute() changes those attributes for a file SetFileAttribute() changes those attributes for a file
DWORD GetFileAttributes( LPCTSTR lpszFileName )
File Flags - controlling read-ahead File Flags - controlling read-ahead
Cache Manager (CM) performs read-ahead and write-back Cache Manager (CM) performs read-ahead and write-back
Reading the next block during sequential access Reading the next block during sequential access Asynch. read-ahead with history for strided access Asynch. read-ahead with history for strided access
history of the last two read requests history of the last two read requests
If a pattern can be determined, cache manager extrapolates it If a pattern can be determined, cache manager extrapolates it
Cache Manager un-maps cached data according to access scheme Cache Manager un-maps cached data according to access scheme
File flags can be specified when opening a file with CreateFile() File flags can be specified when opening a file with CreateFile()
FILE_FLAG_SEQUENTIAL_SCAN FILE_FLAG_SEQUENTIAL_SCAN
Instructs Cache Manager to perform sequential read-ahead Instructs Cache Manager to perform sequential read-ahead
FILE_FLAG_ RANDOM_ACCESS FILE_FLAG_ RANDOM_ACCESS
Instructs Cache Manager not to perform read-ahead Instructs Cache Manager not to perform read-ahead
FILE_FLAG_NO_BUFFERING FILE_FLAG_NO_BUFFERING
Cache Manager shall not be involved with I/O on this file Cache Manager shall not be involved with I/O on this file
Further Reading Further Reading
Mark E. Russinovich and David A. Solomon, Mark E. Russinovich and David A. Solomon,
Microsoft Windows Internals, 4th Edition, Microsoft Press, 2004.
Microsoft Windows Internals, 4th Edition, Microsoft Press, 2004.
Chapter 12 - File Systems Chapter 12 - File Systems
NTFS On-Disk Structure (from pp. 732);
NTFS On-Disk Structure (from pp. 732);
File System Operation (from pp. 700) File System Operation (from pp. 700) Chapter 11 - Cache Manager
Chapter 11 - Cache Manager
File System Interfaces (from pp. 674) File System Interfaces (from pp. 674)
Jeffrey Richter, Advanced Windows, 3rd Edition, Microsoft Press, Jeffrey Richter, Advanced Windows, 3rd Edition, Microsoft Press, September 1997.
September 1997.
Chapter 14 - File Systems Chapter 14 - File Systems
Directory Operations (from pp. 637) Directory Operations (from pp. 637)