input/output with file and directory processing

83
Input/Output With File and Directory Processing

Upload: wren

Post on 08-Jan-2016

78 views

Category:

Documents


6 download

DESCRIPTION

Input/Output With File and Directory Processing. OBJECTIVES. Describe the Windows file systems (compared to UNIX/Linux) Perform sequential file processing Perform console I/O and direct access file I/O Report and analyze system call errors - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Input/Output With File and Directory Processing

Input/OutputWith File and Directory

Processing

Input/OutputWith File and Directory

Processing

Page 2: Input/Output With File and Directory Processing

– 2 –

OBJECTIVESOBJECTIVES

Describe the Windows file systemsDescribe the Windows file systems (compared to UNIX/Linux) (compared to UNIX/Linux)

Perform sequential file processingPerform sequential file processing

Perform console I/O and direct access file I/OPerform console I/O and direct access file I/O

Report and analyze system call errorsReport and analyze system call errors

Describe and use Unicode characters and strings and write generic Describe and use Unicode characters and strings and write generic applications (both Unicode and ASCII characters)applications (both Unicode and ASCII characters)

Use Windows file locking to protect files from concurrent modification Use Windows file locking to protect files from concurrent modification by several processesby several processes

Perform file and directory managementPerform file and directory management

Page 3: Input/Output With File and Directory Processing

File Systems and Sequential I/O

File Systems and Sequential I/O

Page 4: Input/Output With File and Directory Processing

– 4 –

BASIC Windows I/OBASIC Windows I/O

Basic Windows API file processing functions:Basic Windows API file processing functions:

CreateFileCreateFile

ReadFileReadFile

WriteFileWriteFile

CloseHandleCloseHandle

Windows files are:Windows files are: binary text (lines are CR-LF terminated)

Page 5: Input/Output With File and Directory Processing

– 5 –

BASIC UNIX I/OBASIC UNIX I/O

Basic Basic UNIXUNIX file processing functions file processing functions are eqiovalent are eqiovalent::

openopen

readread

writewrite

closeclose

UNIX files are:UNIX files are: Plain byte streams text lines are LF terminated

Page 6: Input/Output With File and Directory Processing

– 6 –

C library I/OC library I/O

Basic Basic CC file processing functions file processing functions are equivalent are equivalent::

ffopenopen

freadfread

fwritefwrite

fclosefclose

Page 7: Input/Output With File and Directory Processing

– 7 –

THE FILE SYSTEMSTHE FILE SYSTEMS

Windows File SystemsWindows File Systems (Virtual) File Allocation Table File System (FAT, VFAT)

The only disk file system for floppies and Windows 9x

NTFS File System (NTFS)Very large (“huge”) files, secure, robustSupported on Windows NT (all versions)

CD-ROM File System (CDFS) Custom file systems

Developed by software vendors

Page 8: Input/Output With File and Directory Processing

– 8 –

Windows FILE NAMING (1/2)Windows FILE NAMING (1/2)

HierarchicalHierarchical

Full pathname can start with a drive nameFull pathname can start with a drive name A:, C:, … Or with a “share” name

\\servername\sharename

Pathname separator is a backslash — Pathname separator is a backslash — \\ You can also use / in C

Directory and file names cannot use ASCII 1–31Directory and file names cannot use ASCII 1–31 Or any of   < > : " | But you can have blanks in file names

Case insensitive but case retainingCase insensitive but case retaining

Page 9: Input/Output With File and Directory Processing

– 9 –

Windows FILE NAMING (2/2)Windows FILE NAMING (2/2)

File, directory names up to 255 characters long File, directory names up to 255 characters long 250 in Windows 9x More with UNICODE

A period A period .. separates a file’s name from its extension separates a file’s name from its extension The period is in the name; there can be more than one

.. and and .... indicate the current directory and its parent indicate the current directory and its parent

No No file linkfile link (as in UNIX) (as in UNIX)

Alternate data streamAlternate data stream Different logical files for the same phisical file A problem for security !!!

Page 10: Input/Output With File and Directory Processing

– 10 –

UNIX file systemsUNIX file systems

Ext2 and Ext3 (Linux)

JFS (AIX)

XFS (IRIX)

Names are case-sensitive

Max. name length implementation dependent

Links used (hard and symbolic)

Page 11: Input/Output With File and Directory Processing

– 11 –

CREATING AND OPENING FILES WindowsCREATING AND OPENING FILES Windows

HANDLE CreateFile (LPCTSTR lpName,HANDLE CreateFile (LPCTSTR lpName,DWORD dwAccess, DWORD dwShareMode,DWORD dwAccess, DWORD dwShareMode,LPSECURITY_ATTRIBUTES lpsa, DWORD dwCreate,LPSECURITY_ATTRIBUTES lpsa, DWORD dwCreate,DWORD dwAttrsAndFlags, HANDLE hTemplateFile)DWORD dwAttrsAndFlags, HANDLE hTemplateFile)

Return: A Return: A HANDLEHANDLE to an open file object to an open file object INVALID_HANDLE_VALUE in case of failure

LPCTSTRLPCTSTR will be described in Part II will be described in Part II

Page 12: Input/Output With File and Directory Processing

– 12 –

CREATING AND OPENING (2/6)CREATING AND OPENING (2/6)

ParametersParameters

lpNamelpName Pointer to the string naming the file Length normally limited to 260 \\?\ is an escape prefix allowing long NT path names

dwAccessdwAccess Access using GENERIC_READ or GENERIC_WRITE Note: Flags can be combined with |

Page 13: Input/Output With File and Directory Processing

– 13 –

CREATING AND OPENING (3/6)CREATING AND OPENING (3/6)

dwShareModedwShareMode 0 — Cannot be shared; not even this process can open another

handle FILE_SHARE_READ — Other processes can read concurrently FILE_SHARE_WRITE — Other processes can write concurrently

lpsalpsa points to a points to a SECURITY_ATTRIBUTESSECURITY_ATTRIBUTES structure structure NULL for now

Page 14: Input/Output With File and Directory Processing

– 14 –

CREATING AND OPENING (4/6)CREATING AND OPENING (4/6)

dwCreatedwCreate — Create a file, overwrite one, etc. — Create a file, overwrite one, etc. CREATE_NEW — Fails if the file exists CREATE_ALWAYS — An existing file will be overwritten OPEN_EXISTING — Fail if the file does not exist OPEN_ALWAYS — Open the file or create it if it doesn’t exist TRUNCATE_EXISTING — File length will be set to zero

Note: There is no “open to append” modeNote: There is no “open to append” mode You will need to position to the end of file

Page 15: Input/Output With File and Directory Processing

– 15 –

CREATING AND OPENING (5/6)CREATING AND OPENING (5/6)

dwAttrsAndFlagsdwAttrsAndFlags — 16 flags and attributes including: — 16 flags and attributes including: FILE_ATTRIBUTE_NORMAL — No other attributes are set FILE_ATTRIBUTE_READONLY — Cannot write or delete FILE_FLAG_OVERLAPPED FILE_FLAG_SEQUENTIAL_SCAN and FILE_FLAG_RANDOM_ACCESS provide performance hints

Attributes are properties of the files themselvesAttributes are properties of the files themselves

Flags are associated with a specific Flags are associated with a specific HANDLEHANDLE Different HANDLEs to the same file can have different flags

Example: One HANDLE is “overlapped,” another notOr, one has FILE_FLAG_SEQUENTIAL_SCAN and another FILE_FLAG_RANDOM_ACCESS

Page 16: Input/Output With File and Directory Processing

– 16 –

CREATING AND OPENING (6/6)CREATING AND OPENING (6/6)

File processing flags include:File processing flags include: FILE_FLAG_WRITE_THROUGH FILE_FLAG_NO_BUFFERING FILE_FLAG_DELETE_ON_CLOSE

hTemplateFilehTemplateFile — Handle of an open — Handle of an open GENERIC_READGENERIC_READ file file Use the same attributes for the new file

Page 17: Input/Output With File and Directory Processing

– 17 –

READING FILES (1/2)READING FILES (1/2)

BOOL ReadFile (HANDLE hFile, LPVOID lpBuffer,BOOL ReadFile (HANDLE hFile, LPVOID lpBuffer,DWORD nNumberOfBytesToRead, DWORD nNumberOfBytesToRead, LPDWORD lpNumberOfBytesRead, LPDWORD lpNumberOfBytesRead, LPOVERLAPPED lpOverlapped)LPOVERLAPPED lpOverlapped)

Return: Return: TRUETRUE if the read succeeds if the read succeeds Even if no bytes were read due to an attempt to read past the

end of file FALSE indicates an invalid handle, a handle without GENERIC_READ access, etc.

Page 18: Input/Output With File and Directory Processing

– 18 –

READING FILES (2/2)READING FILES (2/2)

ParametersParameters

hFilehFile — File handle with — File handle with GENERIC_READGENERIC_READ access access

lpBufferlpBuffer — Me — Memmory buffer to receive the input dataory buffer to receive the input data

nNumberOfBytesToReadnNumberOfBytesToRead

Number of bytes you expect to read

**lpNumberOfBytesReadlpNumberOfBytesRead Actual number of bytes transferred Zero indicates end of file

lpOverlappedlpOverlapped Points to OVERLAPPED structure (NULL for now)

Page 19: Input/Output With File and Directory Processing

– 19 –

WRITING FILESWRITING FILES

BOOL WriteFile (BOOL WriteFile (HANDLE hFile, CONST VOID *lpBuffer,HANDLE hFile, CONST VOID *lpBuffer,DWORD nNumberOfBytesToWrite,DWORD nNumberOfBytesToWrite,LPDWORD lpNumberOfBytesWritten,LPDWORD lpNumberOfBytesWritten,LPOVERLAPPED lpOverlapped)LPOVERLAPPED lpOverlapped)

Return: Return: TRUETRUE if the function succeeds; if the function succeeds; FALSEFALSE otherwise otherwise

Page 20: Input/Output With File and Directory Processing

– 20 –

CLOSING FILESCLOSING FILES

BOOL CloseHandle (HANDLE hObject)BOOL CloseHandle (HANDLE hObject)

Return: Return: TRUETRUE if the function succeeds; if the function succeeds; FALSEFALSE otherwise otherwise

This function is general purpose and will be used to close This function is general purpose and will be used to close handles to many different object typeshandles to many different object types

Page 21: Input/Output With File and Directory Processing

– 21 –

COPYING FILESCOPYING FILES

BOOL CopyFile (LPCTSTR lpExistingFile,BOOL CopyFile (LPCTSTR lpExistingFile,LPCTSTR lpNewFile, BOOL fFailIfExists)LPCTSTR lpNewFile, BOOL fFailIfExists)

If a file with the new name already exists, it will be replaced If a file with the new name already exists, it will be replaced only if only if fFailIfExistsfFailIfExists is is FALSEFALSE

This is a “convenience function” and also provides performanceThis is a “convenience function” and also provides performance

Page 22: Input/Output With File and Directory Processing

Character and String Encoding

Character and String Encoding

Page 23: Input/Output With File and Directory Processing

– 23 –

UNICODE & GENERIC CHARACTERSUNICODE & GENERIC CHARACTERS

Windows NT supports 16-bit charactersWindows NT supports 16-bit characters WCHAR or wchar_t To assure maximum flexibility and source portability, define all

characters and strings using “generic” typeTCHAR

Calculate lengths using sizeof(TCHAR) Include #define UNICODE (to get WCHAR) in all source modules

(or #undef UNICODE to get CHAR) Be consistentDefine UNICODE before #include <windows.h>

Also define _UNICODE consistently for the generic C library

Page 24: Input/Output With File and Directory Processing

– 24 –

GENERIC CHARACTERS (2/3)GENERIC CHARACTERS (2/3)

Use the “generic” C library for all string functions_tprintf in place of printf_stprintf in place of sprintf_tcslen in place of strlen_itot in place of itoa

And MANY moreSee the on line help and the lab programs

Generic versions of some functions are not providede.g. memchr - Replacements are in the lab solutions

Page 25: Input/Output With File and Directory Processing

– 25 –

GENERIC CHARACTERS (3/3)GENERIC CHARACTERS (3/3)

Constant strings in one of three forms (first 2 are ANSI C)Constant strings in one of three forms (first 2 are ANSI C) The last is a macro

"This string uses 8-bit characters"

L"This string uses 16-bit characters"

_T ("This string uses generic characters")Expands to “T…” if UNICODE is not defined; L”T…” if it is

TEXT macro is the same as _T

LPTSTR expands to either char * or wchar_t *

Page 26: Input/Output With File and Directory Processing

– 26 –

THE GENERIC C LIBRARYTHE GENERIC C LIBRARY

Define Define _UNICODE_UNICODE consistently with consistently with UNICODEUNICODE This makes available a wide class of string processing and I/O

functions

_fgettc, _itot, _ttoi, _totupper, _totlower And many more — nearly the complete library Also, locale-specific functions (seven in all):

lstrlen, lstrcmp, lstrcpy, lstrcat, … Be sure to #include <tchar.h> after <windows.h>

Note: AnyNote: Any _ _ keyword or function is specific to Microsoft keyword or function is specific to Microsoft Visual C++ and the Microsoft compilerVisual C++ and the Microsoft compiler

Page 27: Input/Output With File and Directory Processing

– 27 –

GENERIC CHARACTERS AND THE MAIN PROGRAMGENERIC CHARACTERS AND THE MAIN PROGRAM

Windows Windows mainmain is for ASCII; is for ASCII; wmainwmain is for Unicode is for Unicode

In place of In place of int main (argc, char * argv[])int main (argc, char * argv[]) or int main (argc, w_char * argv[])

Use Use #include <tchar.h>#include <tchar.h>

/* this is after <windows.h> *//* this is after <windows.h> */

... ... iint _tmain (int argc, LPTSTR argv[])nt _tmain (int argc, LPTSTR argv[])

The The _tmain_tmain macro then expands to macro then expands to mainmain or or wmainwmain Depending on definition of _UNICODE

This assures correct operation in all circumstances and This assures correct operation in all circumstances and combinationscombinations

Page 28: Input/Output With File and Directory Processing

Standard DevicesStandard Devices

Page 29: Input/Output With File and Directory Processing

– 29 –

STANDARD DEVICESAND CONSOLE I/O (1/7)STANDARD DEVICESAND CONSOLE I/O (1/7)

Both UNIX and Win32 have 3 “standard” devices for input, output and error.

Unix uses first 3 file descriptors (0, 1, 2)

Win32 requires handle and provides a function to get standard device handlers from integer constants HANDLE GetStdHandle(DWORD nStdHandle);

Page 30: Input/Output With File and Directory Processing

– 30 –

STANDARD DEVICESAND CONSOLE I/O (1/7)STANDARD DEVICESAND CONSOLE I/O (1/7)

HANDLE GetStdHandle (DWORD dwDevice)HANDLE GetStdHandle (DWORD dwDevice)

Return: A valid handle or Return: A valid handle or INVALID_HANDLE_VALUEINVALID_HANDLE_VALUE for failure for failure

BOOL SetStdHandle (DWORD IDStdHandle,BOOL SetStdHandle (DWORD IDStdHandle,

HANDLE hHandle)HANDLE hHandle)

Return: Return: TRUETRUE or or FALSEFALSE indicating success or failure indicating success or failure

Page 31: Input/Output With File and Directory Processing

– 31 –

STANDARD DEVICESAND CONSOLE I/O (2/7)STANDARD DEVICESAND CONSOLE I/O (2/7)

dwDevicedwDevice and and IDStdHandleIDStdHandle Must have one of these values:

STD_INPUT_HANDLESTD_OUTPUT_HANDLESTD_ERROR_HANDLE

Page 32: Input/Output With File and Directory Processing

– 32 –

STANDARD DEVICESAND CONSOLE I/O (3/7)STANDARD DEVICESAND CONSOLE I/O (3/7)

hHandlehHandle Specifies an open file that is to be the standard device

Two reserved pathnames for console input (the keyboard):Two reserved pathnames for console input (the keyboard):

""CONIN$CONIN$""

and output (the display):and output (the display):

""CONOUTCONOUT$$""

Page 33: Input/Output With File and Directory Processing

– 33 –

STANDARD DEVICESAND CONSOLE I/O (4/7)STANDARD DEVICESAND CONSOLE I/O (4/7)

BOOL SetConsoleMode (HANDLE hConsole,BOOL SetConsoleMode (HANDLE hConsole,

DWORD fdevMode)DWORD fdevMode)

Return: Return: TRUETRUE if and only if the function succeeds if and only if the function succeeds

hConsolehConsole Must have GENERIC_WRITE access

Page 34: Input/Output With File and Directory Processing

– 34 –

STANDARD DEVICESAND CONSOLE I/O (5/7)STANDARD DEVICESAND CONSOLE I/O (5/7)

fDevModefDevMode — How characters are processed — How characters are processed ENABLE_WINDOW_INPUT ENABLE_LINE_INPUT ENABLE_ECHO_INPUT ENABLE_PROCESSED_INPUT ENABLE_PROCESSED_OUTPUT ENABLE_WRAP_AT_EOL_OUTPUT

Page 35: Input/Output With File and Directory Processing

– 35 –

STANDARD DEVICESAND CONSOLE I/O (6/7)STANDARD DEVICESAND CONSOLE I/O (6/7)

BOOL ReadConsole (HANDLE hConsoleInput,BOOL ReadConsole (HANDLE hConsoleInput,LPVOID lpvBuffer, DWORD cchToRead,LPVOID lpvBuffer, DWORD cchToRead,LPDWORD lpcchRead, LPVOID lpvReserved)LPDWORD lpcchRead, LPVOID lpvReserved)

Return: Return: TRUETRUE if and only if the read succeeds if and only if the read succeeds

Page 36: Input/Output With File and Directory Processing

– 36 –

STANDARD DEVICESAND CONSOLE I/O (7/7)STANDARD DEVICESAND CONSOLE I/O (7/7)

BOOL WriteConsoleBOOL WriteConsole Same as ReadConsole

BOOL FreeConsole (VOID)BOOL FreeConsole (VOID)

BOOL AllocConsole (VOID)BOOL AllocConsole (VOID)

Page 37: Input/Output With File and Directory Processing

– 37 –

EXAMPLE: PrintStrings (1/3)EXAMPLE: PrintStrings (1/3)

#include "envirmnt.h" /* UNICODE is defined here */#include "envirmnt.h" /* UNICODE is defined here */#include <windows.h>#include <windows.h>

BOOL PrintStrings (HANDLE hOut, ...)BOOL PrintStrings (HANDLE hOut, ...)

/* Write the messages to the output handle (hOut)/* Write the messages to the output handle (hOut) Use WriteConsole (to handle Unicode & consoleUse WriteConsole (to handle Unicode & console processing) first, as the output will normallyprocessing) first, as the output will normally be the console. If that fails, use WriteFile.be the console. If that fails, use WriteFile.

hOut: Handle for output file.hOut: Handle for output file. ... : Variable argument list containing TCHAR... : Variable argument list containing TCHAR strings. The list must be terminated with NULL. */strings. The list must be terminated with NULL. */

Page 38: Input/Output With File and Directory Processing

– 38 –

PrintStrings (2/3)PrintStrings (2/3)

{{ DWORD MsgLen, Count;DWORD MsgLen, Count; BOOL Success = TRUE;BOOL Success = TRUE; LPCTSTR pMsg;LPCTSTR pMsg; va_list pMsgList;va_list pMsgList; /* Current message string *//* Current message string */ va_start (pMsgList, hOut);va_start (pMsgList, hOut);

/* Start processing msgs *//* Start processing msgs */ while (((pMsg = va_arg (pMsgList, LPCTSTR)) != NULL) while (((pMsg = va_arg (pMsgList, LPCTSTR)) != NULL) && Success) && Success) {{ MsgLen = _tcslen (pMsg);MsgLen = _tcslen (pMsg);

(CONTINUED)(CONTINUED)

Page 39: Input/Output With File and Directory Processing

– 39 –

PrintStrings (3/3)PrintStrings (3/3)

/* WriteConsole fails if the handle is associated/* WriteConsole fails if the handle is associated with a file rather than with a console */with a file rather than with a console */

if (!WriteConsole (hOut, pMsg, if (!WriteConsole (hOut, pMsg, MsgLen, &Count, NULL) MsgLen, &Count, NULL) && &&

!WriteFile (hOut, pMsg, !WriteFile (hOut, pMsg,

MsgLen * sizeof (TCHAR), &Count, NULL))MsgLen * sizeof (TCHAR), &Count, NULL))

return FALSE;return FALSE; } /* End of while loop - process next message */} /* End of while loop - process next message */ va_end (pMsgList);va_end (pMsgList); return TRUE;return TRUE;}}

Page 40: Input/Output With File and Directory Processing

– 40 –

EXAMPLE: ReportError (1/4)EXAMPLE: ReportError (1/4)

OBJECTIVE:OBJECTIVE: Turn system call errors into meaningful text strings

And print the text

Terminate the program if the error is fatal

Similar to Similar to perror()perror(), but it works in the multithreaded , but it works in the multithreaded Windows environment.Windows environment.

Page 41: Input/Output With File and Directory Processing

– 41 –

EXAMPLE: ReportError (2 of 4)EXAMPLE: ReportError (2 of 4)

#include "envirmnt.h"#include "envirmnt.h"#include <windows.h>#include <windows.h>#include "support.h"#include "support.h"

VOID ReportError (LPCTSTR UserMessage,VOID ReportError (LPCTSTR UserMessage, DWORD ExitCode, BOOL PrintErrorMsg)DWORD ExitCode, BOOL PrintErrorMsg)/* General-purpose function to report system errors./* General-purpose function to report system errors. Obtain the error number and turn it into theObtain the error number and turn it into the system error message. Display this informationsystem error message. Display this information and the user specified message to the standardand the user specified message to the standard error device.error device. UserMessage: Message to be displayed toUserMessage: Message to be displayed to standard error device.standard error device. */*/

Page 42: Input/Output With File and Directory Processing

– 42 –

ReportError (3/4)ReportError (3/4)

ExitCode: 0 - ReturnExitCode: 0 - Return > 0 - ExitProcess with this code> 0 - ExitProcess with this code PrintErrorMessage: Display the last system errorPrintErrorMessage: Display the last system error message if this flag is set. */message if this flag is set. */{{ DWORD eMsgLen, ErrNum = GetLastError ();DWORD eMsgLen, ErrNum = GetLastError (); LPVOID lpvSysMsg;LPVOID lpvSysMsg; HANDLE hStdErr = GetStdHandle (STD_ERROR_HANDLE);HANDLE hStdErr = GetStdHandle (STD_ERROR_HANDLE); PrintMsg (hStdErr, UserMessage);PrintMsg (hStdErr, UserMessage); if (PrintErrorMsg) {if (PrintErrorMsg) {

Page 43: Input/Output With File and Directory Processing

– 43 –

ReportError (4/4)ReportError (4/4)

eMsgLen = FormatMessage (eMsgLen = FormatMessage ( FORMAT_MESSAGE_ALLOCATE_BUFFER |FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,FORMAT_MESSAGE_FROM_SYSTEM, NULL, ErrNum,NULL, ErrNum, MAKELANGID (LANG_DFLT, SUBLANG_DFLT),MAKELANGID (LANG_DFLT, SUBLANG_DFLT), &lpvSysMsg, 0, NULL);&lpvSysMsg, 0, NULL); PrintStrings (hStdErr, TEXT ("\n"),PrintStrings (hStdErr, TEXT ("\n"), lpvSysMsg, TEXT ("\n"), NULL);lpvSysMsg, TEXT ("\n"), NULL); HeapFree (GetProcessHeap (), 0, lpvSysMsg);HeapFree (GetProcessHeap (), 0, lpvSysMsg);

}}if (ExitCode > 0)if (ExitCode > 0)

ExitProcess (ExitCode);ExitProcess (ExitCode);elseelse

return;return;}}

Page 44: Input/Output With File and Directory Processing

– 44 –

FILE POINTERS (1/4)FILE POINTERS (1/4)

DWORD SetFilePointer (HANDLE hFile,DWORD SetFilePointer (HANDLE hFile,LONG lDistanceToMove,LONG lDistanceToMove,PLONG lpDistanceToMoveHigh,PLONG lpDistanceToMoveHigh,DWORD dwMoveMethod)DWORD dwMoveMethod)

Return: The low-order Return: The low-order DWORDDWORD (unsigned) of the new file (unsigned) of the new file pointer. The high-order portion of the new file pointer pointer. The high-order portion of the new file pointer goes to the goes to the DWORDDWORD indicated by indicated by lpDistanceToMoveHighlpDistanceToMoveHigh (if (if non-non-NULLNULL). In case of error, the return value is ). In case of error, the return value is OxFFFFFFFFOxFFFFFFFF..

Note:Note: The file pointer is associated with the The file pointer is associated with the HANDLEHANDLE, not the , not the file. The pointer advances with each read and write.file. The pointer advances with each read and write.

Page 45: Input/Output With File and Directory Processing

– 45 –

FILE POINTERS (2/4)FILE POINTERS (2/4)

Note:Note: NTFS is a 64-bit file system, so file pointers are 64 bits NTFS is a 64-bit file system, so file pointers are 64 bits long. The file pointer is specified with two 32-bit parts.long. The file pointer is specified with two 32-bit parts. Large files are increasingly important in many applications But many users will only require “short” (< 4GB) files

SetFilePointerSetFilePointer Parameters Parameters

hFilehFile — Handle of an open file with read and/or write access — Handle of an open file with read and/or write access

lDistanceToMovelDistanceToMove — — LONGLONG signed distance to move or signed distance to move or unsigned file positionunsigned file position

Page 46: Input/Output With File and Directory Processing

– 46 –

FILE POINTERS (3/4)FILE POINTERS (3/4)

**lpDistanceToMoveHighlpDistanceToMoveHigh High-order portion of the move distance Can be NULL for “small” files

dwMoveMethoddwMoveMethod — Specifies one of these modes: — Specifies one of these modes: FILE_BEGIN — Position from the start of file FILE_CURRENT — Move pointer forward or backward FILE_END — Position backward from end of file

SetEndOfFile ()SetEndOfFile () function resizes the file based on the function resizes the file based on the current file pointercurrent file pointer

Page 47: Input/Output With File and Directory Processing

– 47 –

FILE POINTERS (4/4)FILE POINTERS (4/4)

Use the Use the LARGE_INTEGERLARGE_INTEGER data type (union) for 64-bit file data type (union) for 64-bit file positionspositions

Example coming upExample coming up

MMembers: embers: LONGLONG QuadpartLONGLONG Quadpart Do the 64-bit arithmetic hereDo the 64-bit arithmetic hereDWORD LowPartDWORD LowPartLONG HighPartLONG HighPart

Page 48: Input/Output With File and Directory Processing

– 48 –

Alternative Direct File Access Using the Overlapped Structure (1/2)Alternative Direct File Access Using the Overlapped Structure (1/2)

Example: Use the Offset fields in the overlapped structureExample: Use the Offset fields in the overlapped structure Offset Low order 32 bits OffsetHigh High order 32 bits hEvent Must be NULL. This field is

used with asynchronous I/O Two reserved fields Do not use!!

The example shows how to update a record in a fileThe example shows how to update a record in a file

Page 49: Input/Output With File and Directory Processing

– 49 –

Alternative Direct File Access Using the Overlapped Structure (2/2)Alternative Direct File Access Using the Overlapped Structure (2/2)

OVERLAPPED ov = { 0, 0, 0, 0, NULL };RECORD r; /* Includes “reference count” field */LONGLONG n;LARGE_INTEGER FilePos;DWORD nRd, nWrt;. . ./* Update reference count in the n’th record. */FilePos.QuadPart = n * sizeof (RECORD);ov.Offset = FilePos.LowPart;ov.OffsetHigh = FilePos.HighPart;ReadFile (hFile, &r, sizeof(RECORD), &nRd, &ov);r.RefCount++; /* Update the record. */WriteFile(hFile, &r, sizeof(RECORD), &nWrt, &ov);

Page 50: Input/Output With File and Directory Processing

File LockingFile Locking

Page 51: Input/Output With File and Directory Processing

– 51 –

WIN32 FILE LOCKING (1/5)WIN32 FILE LOCKING (1/5)

Lock all or part of a file Lock can be read-only (sharable) or read-write (exclusive) Lock belongs to a process

Any attempt to access part of a file (using ReadFile or

WriteFile) in violation of a lock will fail

You cannot create conflicting locks on a file Specify whether you should wait for a lock to become available,

or thread can return immediately, indicating whether it obtained the lock

Page 52: Input/Output With File and Directory Processing

– 52 –

WIN32 FILE LOCKING (2/5)WIN32 FILE LOCKING (2/5)

BOOL LockFileEx (HANDLE hFile, DWORD dwFlags,BOOL LockFileEx (HANDLE hFile, DWORD dwFlags,DWORD dwReserved,DWORD dwReserved,DWORD nNumberOfBytesToLockLow,DWORD nNumberOfBytesToLockLow,DWORD nNumberOfBytesToLockHigh,DWORD nNumberOfBytesToLockHigh,LPOVERLAPPED lpOverlapped)LPOVERLAPPED lpOverlapped)

Locks a byte range in an open file for shared (multiple readers) or exclusive (one reader-writer) access

Page 53: Input/Output With File and Directory Processing

– 53 –

WIN32 FILE LOCKING (3/5)WIN32 FILE LOCKING (3/5)

ParametersParameters hFile — Handle of an open file which must have at least one of GENERIC_READ or GENERIC_WRITE access

dwFlags — Determines the lock mode and whether to wait for the lock to become availableLOCKFILE_EXCLUSIVE_LOCK, if set, indicates a request for an

exclusive, read-write, lock; otherwise, it requests a shared (read only) lock

LOCKFILE_FAIL_IMMEDIATELY, if set, specifies that the

function should return immediately with a FALSE if the lock cannot be acquired; otherwise, the call blocks until the lock becomes available

Page 54: Input/Output With File and Directory Processing

– 54 –

WIN32 FILE LOCKING (4/5)WIN32 FILE LOCKING (4/5)

dwReserved must be zero lpOverlapped — Points to an OVERLAPPED data structure

containing the start of the byte rangeThe OVERLAPPED structure contains two data members that must

be set (the others are ignored), namely:DWORD Offset andDWORD OffsetHigh

A file lock is removed with a corresponding A file lock is removed with a corresponding UnlockFileExUnlockFileEx call, using all the same parameters except for call, using all the same parameters except for dwFlagsdwFlags

Page 55: Input/Output With File and Directory Processing

– 55 –

WIN32 FILE LOCKING (5/5)WIN32 FILE LOCKING (5/5)

BOOL UnlockFileEx (HANDLE hFile,BOOL UnlockFileEx (HANDLE hFile,DWORD dwReserved,DWORD dwReserved,DWORD nNumberOfBytesToLockLow,DWORD nNumberOfBytesToLockLow,DWORD nNumberOfBytesToLockHigh,DWORD nNumberOfBytesToLockHigh,LPOVERLAPPED lpOverlapped)LPOVERLAPPED lpOverlapped)

Page 56: Input/Output With File and Directory Processing

– 56 –

FILE LOCKING IN WINDOWS 9xFILE LOCKING IN WINDOWS 9x

Exclusive locks onlyExclusive locks only

BOOL LockFile (HANDLE hFile,BOOL LockFile (HANDLE hFile,DWORD dwOffsetLow, DWORD dwOffsetHighDWORD dwOffsetLow, DWORD dwOffsetHighDWORD nNumberOfBytesToLockLow,DWORD nNumberOfBytesToLockLow,DWORD nNumberOfBytesToLockHigh)DWORD nNumberOfBytesToLockHigh)

BOOL UnlockFile (HANDLE hFile,BOOL UnlockFile (HANDLE hFile,DWORD dwOffsetLow, DWORD dwOffsetHighDWORD dwOffsetLow, DWORD dwOffsetHighDWORD nNumberOfBytesToLockLow,DWORD nNumberOfBytesToLockLow,DWORD nNumberOfBytesToLockHigh)DWORD nNumberOfBytesToLockHigh)

Page 57: Input/Output With File and Directory Processing

– 57 –

WIN32 FILE LOCKING CONSIDERATIONSWIN32 FILE LOCKING CONSIDERATIONS

The unlock must use exactly the same range as a preceding lock

Locks cannot overlap existing locked regions in a file You can lock beyond the range of a file’s length Locks are not inherited by a newly created process

Page 58: Input/Output With File and Directory Processing

– 58 –

LOCK REQUEST LOGICLOCK REQUEST LOGIC

Requested Lock TypeRequested Lock Type

Existing LockExisting Lock Shared LockShared LockExclusive LockExclusive Lock

NoneNone GrantedGranted GrantedGranted

Shared LockShared Lock GrantedGranted RefusedRefused(one or more)(one or more)

Exclusive LockExclusive LockRefusedRefused RefusedRefused

Page 59: Input/Output With File and Directory Processing

– 59 –

I/O OperationI/O Operation

Existing LockExisting Lock ReadRead WriteWrite

NoneNone SucceedsSucceeds SucceedsSucceeds

Shared LockShared Lock Succeeds. It is notSucceeds. It is not RefusedRefused(one or more)(one or more) necessary for the callingnecessary for the calling

process to own a lockprocess to own a lockon the file region.on the file region.

Exclusive LockExclusive LockSucceeds if the calling process owns theSucceeds if the calling process owns thelock. Fails otherwise.lock. Fails otherwise.

LOCKS AND I/O OPERATIONLOCKS AND I/O OPERATION

Page 60: Input/Output With File and Directory Processing

– 60 –

WIN32 FILE LOCKS—SUMMARY (1/2)WIN32 FILE LOCKS—SUMMARY (1/2)

A failed read or write may take the form of a partially complete operation if only a portion of the read or write record is locked

Read and write operations are normally in the form of ReadFile and WriteFile calls, or ReadFileEx and WriteFileEx

Diagnosing a read or write failure requires calling GetLastError

Accessing memory that is mapped to a file is another form of file I/O

Page 61: Input/Output With File and Directory Processing

– 61 –

WIN32 FILE LOCKS—SUMMARY (2/2)WIN32 FILE LOCKS—SUMMARY (2/2)

Lock conflicts are not detected at the time of memory reference; rather, they are detected at the time MapViewOfFile is called

The LockFile function is a limited, special case which gives exclusive access and returns immediately

Page 62: Input/Output With File and Directory Processing

– 62 –

UNIX/Linux file locking (1/2)UNIX/Linux file locking (1/2)

“Advisory” and “Mandatory” locks

Always use POSIX.1 function fcntl or its interface lockf()int fcntl(int fd, int cmd, struct flock * lock);struct flock {short int l_type; /* Type of lock: F_RDLCK, F_WRLCK, or F_UNLCK */

short int l_whence; /* Where ‘l_start’ is relative to */

__off_t l_start; /* Offset where the lock begins. */

__off_t l_len; /* Size of the locked area; zero means EOF */

__pid_t l_pid; /* Process holding the lock. */};

Page 63: Input/Output With File and Directory Processing

– 63 –

UNIX/Linux file locking (2/2)UNIX/Linux file locking (2/2)

3 possible values for cmd: F_GETLK, F_SETLK, F_SETLKW

3 values for whence: SEEK_SET, SEEK_CUR, SEEK_END

Page 64: Input/Output With File and Directory Processing

– 64 –

fcntl is used for 5 purposesfcntl is used for 5 purposes

• duplicate an existing descriptor (cmd=F_DUPFD)

• obtain/define close-on-exec flag for a file descriptor (cmd=F_GETFD or F_SETFD)

• obtain/define file status flag (cmd=F_GETFL or F_SETFL). Possible flags: O_NONBLOCK, O_APPEND, O_ASYNC.

• obtain/define ownership for asynchronous I/O (cmd=F_GETOWN or F_SETOWN)

• obtain/define record locks (cmd=F_GETLK, F_SETLK or F_SETLKW)

Page 65: Input/Output With File and Directory Processing

File and Directory Management

File and Directory Management

Page 66: Input/Output With File and Directory Processing

– 66 –

FILE AND DIRECTORY MANAGEMENTFILE AND DIRECTORY MANAGEMENT

BOOL DeleteFile (LPCTSTR lpFileName)BOOL DeleteFile (LPCTSTR lpFileName)

You cannot delete an open file in Windows NTYou cannot delete an open file in Windows NT

(You can in Windows 9x)(You can in Windows 9x)

Page 67: Input/Output With File and Directory Processing

– 67 –

FILE AND DIRECTORY MANAGEMENT—RENAMING (1/2)FILE AND DIRECTORY MANAGEMENT—RENAMING (1/2)

BOOL MoveFile (LPCTSTR lpExisting,BOOL MoveFile (LPCTSTR lpExisting,LPCTSTR lpNew)LPCTSTR lpNew) Source and target files must be on the same drive

BOOL MoveFileEx (LPCTSTR lpExisting,BOOL MoveFileEx (LPCTSTR lpExisting,LPCTSTR lpNew, DWORD dwFlags)LPCTSTR lpNew, DWORD dwFlags) Source and target files can be on different drives

Note:Note: There are no links as in UNIX There are no links as in UNIX Neither soft links nor hard links Shortcuts are not the same thing; they are only recognized by

the visual shell

Page 68: Input/Output With File and Directory Processing

– 68 –

FILE AND DIRECTORY MANAGEMENT—RENAMING (2/2)FILE AND DIRECTORY MANAGEMENT—RENAMING (2/2)

ParametersParameters

lpExistinglpExisting — The name of the existing file or directory — The name of the existing file or directory

lpNewlpNew — Cannot exist with — Cannot exist with MoveFileMoveFile Must be on same drive

dwFlagsdwFlags MOVEFILE_REPLACE_EXISTING

To replace an existing file

MOVEFILE_COPY_ALLOWEDCopy then delete

Page 69: Input/Output With File and Directory Processing

– 69 –

DIRECTORY MANAGEMENT (1/4)DIRECTORY MANAGEMENT (1/4)

BOOL CreateDirectory (LPCTSTR lpPath,BOOL CreateDirectory (LPCTSTR lpPath,LPSECURITY_ATTRIBUTES lpsa)LPSECURITY_ATTRIBUTES lpsa)

BOOL RemoveDirectory (LPCTSTR lpPath)BOOL RemoveDirectory (LPCTSTR lpPath)

lpPathlpPath Points to a null-terminated string with the directory name

Page 70: Input/Output With File and Directory Processing

– 70 –

DIRECTORY MANAGEMENT (2/4)DIRECTORY MANAGEMENT (2/4)

BOOL SetCurrentDirectory (LPCTSTR lpCurDir)BOOL SetCurrentDirectory (LPCTSTR lpCurDir)

lpCurDirlpCurDir The path to the new current directory

There is actually a current directory maintained for each There is actually a current directory maintained for each drivedrive

SetCurrentDirectory (TEXT("C:"));SetCurrentDirectory (TEXT("C:")); Will set the C: drive directory to its current value

Page 71: Input/Output With File and Directory Processing

– 71 –

DIRECTORY MANAGEMENT (3/4)DIRECTORY MANAGEMENT (3/4)

DWORD GetCurrentDirectory (DWORD cchCurDir,DWORD GetCurrentDirectory (DWORD cchCurDir,

LPTSTR lpCurDir)LPTSTR lpCurDir)

Return:Return: The string length of the returned pathname The required buffer size if the buffer is not large enough

This includes the space for the null string terminator

Zero if the function fails

Page 72: Input/Output With File and Directory Processing

– 72 –

DIRECTORY MANAGEMENT (4/4)DIRECTORY MANAGEMENT (4/4)

Windows uses this technique whenever the result’s length Windows uses this technique whenever the result’s length is not knownis not known

ParametersParameters

cchCurDircchCurDir Character length of the buffer for the directory name

cch - “Count in characters”

lpCurDirlpCurDir Points to the buffer to receive the pathname string

Page 73: Input/Output With File and Directory Processing

– 73 –

TESTING GetCurrentDirectory FOR CORRECT OPERATIONTESTING GetCurrentDirectory FOR CORRECT OPERATION

/* pwd: Print the working directory./* pwd: Print the working directory. Similar to the UNIX pwd command */Similar to the UNIX pwd command *//* This program illustrates:/* This program illustrates: 1. Windows GetCurrentDirectory1. Windows GetCurrentDirectory 2. Testing the length of a returned string */2. Testing the length of a returned string */#include "EvryThng.h"#include "EvryThng.h"

#define DIRNAME_LEN MAX_PATH + 2#define DIRNAME_LEN MAX_PATH + 2int main (int argc, LPCTSTR argv [])int main (int argc, LPCTSTR argv []){{ /* Buffer to receive current directory allows/* Buffer to receive current directory allows CR/LF at the end of the longest possible path. */CR/LF at the end of the longest possible path. */ TCHAR pwdBuffer [DIRNAME_LEN];TCHAR pwdBuffer [DIRNAME_LEN];

Page 74: Input/Output With File and Directory Processing

– 74 –

GetCurrentDirectory (2/2)GetCurrentDirectory (2/2)

DWORD LenCurDir;DWORD LenCurDir; LenCurDir = GetCurrentDirectoryLenCurDir = GetCurrentDirectory (DIRNAME_LEN, pwdBuffer);(DIRNAME_LEN, pwdBuffer); if (LenCurDir == 0)if (LenCurDir == 0) ReportError (TEXTReportError (TEXT ("Failure getting pathname\n"), 1, TRUE);("Failure getting pathname\n"), 1, TRUE); if (LenCurDir > DIRNAME_LEN) if (LenCurDir > DIRNAME_LEN) ReportError (TEXTReportError (TEXT ("Pathname is too long\n"), 2, FALSE);("Pathname is too long\n"), 2, FALSE);

PrintMsg (GetStdHandle (STD_OUTPUT_HANDLE),PrintMsg (GetStdHandle (STD_OUTPUT_HANDLE), pwdBuffer);pwdBuffer); return 0;return 0;}}

Page 75: Input/Output With File and Directory Processing

– 75 –

FILE ATTRIBUTES ANDFILE SEARCHING (1/2)FILE ATTRIBUTES ANDFILE SEARCHING (1/2)

HANDLE FindFirstFile (LPCTSTR lpSearchFile,HANDLE FindFirstFile (LPCTSTR lpSearchFile,

LPWIN32_FIND_DATA lpffd)LPWIN32_FIND_DATA lpffd)

Return: A “search handle”Return: A “search handle” INVALID_HANDLE_VALUE indicates failure

ParametersParameters lpSearchFile — Points to directory or pathname. Wildcards

are OK (* and ?) lpffd — Points to a WIN32_FIND_DATA structure

Page 76: Input/Output With File and Directory Processing

– 76 –

FILE ATTRIBUTES ANDFILE SEARCHING (2/2)FILE ATTRIBUTES ANDFILE SEARCHING (2/2)

typedef struct _WIN32_FIND_DATA {typedef struct _WIN32_FIND_DATA {DWORD dwFileAttributes; /* see CreateFile */DWORD dwFileAttributes; /* see CreateFile */FILETIME ftCreationTime; /* 64-bit int */FILETIME ftCreationTime; /* 64-bit int */FILETIME ftLastAccessTime;FILETIME ftLastAccessTime;FILETIME ftLastWriteTime;FILETIME ftLastWriteTime;DWORD nFileSizeHigh;DWORD nFileSizeHigh;DWORD nFileSizeLow;DWORD nFileSizeLow;DWORD dwReserved0;DWORD dwReserved0;DWORD dwReserved1;DWORD dwReserved1;TCHAR cFileName [MAX_PATH]; /* file name */TCHAR cFileName [MAX_PATH]; /* file name */TCHAR cAlternateFileName [14]; /* 8.3 name */TCHAR cAlternateFileName [14]; /* 8.3 name */

} WIN32_FIND_DATA;} WIN32_FIND_DATA;

Page 77: Input/Output With File and Directory Processing

– 77 –

FILES AND DIRECTORIES (1/5)FILES AND DIRECTORIES (1/5)

BOOL FindNextFile (HANDLE hFindFile,BOOL FindNextFile (HANDLE hFindFile,LPWIN32_FIND_DATA lpffd)LPWIN32_FIND_DATA lpffd) FALSE when no more files satisfy the search pattern

BOOL FindClose (HANDLE hFindFile)BOOL FindClose (HANDLE hFindFile) Exception: This is an example of a HANDLE that is not closed

with CloseHandle.

Page 78: Input/Output With File and Directory Processing

– 78 –

FILES AND DIRECTORIES (2/5)FILES AND DIRECTORIES (2/5)

File attributes can be obtained individuallyFile attributes can be obtained individually Usually from the HANDLE rather than file name

DWORD GetFileSize (HANDLE hFile,DWORD GetFileSize (HANDLE hFile,

LPDWORD lpdwFileSizeHigh)LPDWORD lpdwFileSizeHigh)

Return: The low-order component of the file sizeReturn: The low-order component of the file size 0xFFFFFFFF indicates a possible error; check GetLastError

Page 79: Input/Output With File and Directory Processing

– 79 –

FILES AND DIRECTORIES (3/5)FILES AND DIRECTORIES (3/5)

DWORD GetFileInformationByHandle (HANDLE hFile)DWORD GetFileInformationByHandle (HANDLE hFile)

Use Use GetCompressedFileSizeGetCompressedFileSize to test for an empty file to test for an empty file if you have the file name but do not have an open handle

Page 80: Input/Output With File and Directory Processing

– 80 –

FILES AND DIRECTORIES (4/5)FILES AND DIRECTORIES (4/5)

BOOL GetFileTime (HANDLE hFile,BOOL GetFileTime (HANDLE hFile,LPFILETIME lpftCreation,LPFILETIME lpftCreation,LPFILETIME lpftLastAccess,LPFILETIME lpftLastAccess,LPFILETIME lpftLastWrite)LPFILETIME lpftLastWrite)

BOOL FileTimeToSystemTime(BOOL FileTimeToSystemTime(CONST FILETIME * CONST FILETIME * lpFileTimelpFileTime,,LPSYSTEMTIME LPSYSTEMTIME lpSystemTimelpSystemTime))

LPSYSTEMTIMELPSYSTEMTIME has has WORDWORD members for the time members for the time components:components:

wYear, wMonth, …, wMilliseconds

FILETIME (64 bits) is elapsed 100 ns units since Jan 1, 1601

More than 60,000 years can be represented

Page 81: Input/Output With File and Directory Processing

– 81 –

FILES AND DIRECTORIES (5/5)FILES AND DIRECTORIES (5/5)

DWORD GetFileAttributes (LPCTSTR lpFileName)DWORD GetFileAttributes (LPCTSTR lpFileName)

Return: The file attributes or Return: The file attributes or 0xFFFFFFFF0xFFFFFFFF in case of failure in case of failure The attributes can be tested for the following values:

FILE_ATTRIBUTE_DIRECTORYFILE_ATTRIBUTE_NORMALFILE_ATTRIBUTE_READONLYFILE_ATTRIBUTE_TEMPORARY

SetFileAttributesSetFileAttributes Allows you to change attributes in a named file

Page 82: Input/Output With File and Directory Processing

– 82 –

TEMPORARY FILE NAMES (1/2)TEMPORARY FILE NAMES (1/2)

UINT GetTempFileName (LPCTSTR lpPath,UINT GetTempFileName (LPCTSTR lpPath,LPCTSTR lpPrefix, UINT uUnique,LPCTSTR lpPrefix, UINT uUnique,LPTSTR lpTempFile)LPTSTR lpTempFile)

Return: A unique numeric value used to create the file Return: A unique numeric value used to create the file namename uUnique if uUnique is non-zero On failure, the return value is zero.

Page 83: Input/Output With File and Directory Processing

– 83 –

TEMPORARY FILE NAMES (2/2)TEMPORARY FILE NAMES (2/2)

lpPathlpPath Directory where you want the temporary file

lpPrefixlpPrefix Prefix of the temporary name

uUniqueuUnique Normally zero to generate a unique four-digit suffix

lpTempFilelpTempFile Points to buffer for the temporary file name