WIN32 File Management Flashcards

1
Q

Character system

A
  • Coding strategies:
    • 8-bit only
      • char
    • Unicode only
      • Use 16-bit chars by defining UNICODE and _UNICODE
    • 8-bit and unicode with generic code
      • Use generic functions that are mapped to the deisred output
    • 8-bit and unicode with runtime selection
      • Programmer specified
  • Generic type char definition: TCHAR
    • mapped either to ANSI C ASCII (char)
    • or Unicode UTF-16 (wchar)
    • _T / TEXT macro
  • MACROS
    • UNICODE
      • to get wchar in all source modules
      • # undef to get char
      • must be defined before #include <windows.h></windows.h>
    • _UNICODE
      • to make available a wide class of string processing functions
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Generic main definition

A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Opening a file

A
  • There is an OpenFile function
    • but it’s been deprecated
  • Use CreateFile:
    • parameters:
      • dwAccess:
        • GENERIC_READ or GENERIC_WRITE
        • can be combined: GENERIC_READ | GENERIC_WRITE
      • dwShareMode (filesharing mode):
        • 0: even the same process can’t open an handle of the same file
        • FILE_SHARED_READ: other proc. can read concurrently
        • FILE_SHARED_WRITE: other proc. can write concurrently
      • lpsa:
        • Points to a security attribute
        • a newly created file can be fully controlled by anynore
        • usually NULL
      • dwCreate (file read mode):
        • NB: there is no append mode
        • CREATE_NEW: create new or file if one already exists
        • CREATE_ALWAYS: create new or overwrite
        • OPEN_EXISTING: open a file only if it exists
        • OPEN_ALWAYS: either open a new file or an already existing one.
      • dwAttrAndFlags:
        • File attributes
        • FILE_ATTRIBUTE_NORMAL: only this field is set
        • FILE_ATTRIBUTE_READONLY: can only read
        • FILE_FLAG_OVERLAPPED: async I/O option
        • FILE_FLAG_SEQUENTIAL_SCAN: perf. hint
        • FILE_FLAG_RANDOM_ACCESS: perf. hint
      • hTemplateFile:
        • usually NULL
        • handle of a file from which to copy CreateFile attributes associated with the handle
        • TRUNCATE_EXISTING: set file lenght to 0
  • Every handle, even if referring to the same file, could have different sets of properties.
  • Closing a file is done through the CloseHandle(Handle Object) function, which is general for all kinds of handles.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

{Read|Write}File

A
  • Returns true on success.
  • Parameters of the two functions are complementary:
    • hFile: file handle with GENERIC_READ access
    • *lpBuffer: buffer to receive the input data
    • nNumberOfBytesToRead: number of bytex you expect to read
    • *lpNumberOfBytesRead: actual number of bytes transferred, 0 indicates end of file
    • *lpOverlapped: points to overlapped structure if present
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

CopyFile

A
  • Very useful shortcut.
  • Better performance and also file attributes are copied (like timestamps)
  • Parameter:
    • *lpExisitingFile:
    • *lpNewFile
    • *fFailIfExists: if false, overwrites content of newFile if already existing
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

SetFilePointer

A
  • The file pointer is different in each handle, initially at 0.
  • SetFilePointer:
    • hFile
    • lDistanceToMove: 32-LSBs
    • lpDistanceToMoveHigh: 32-MSBs if (>4GB)
    • dwModeMethod:
      • FILE_BEGIN
      • FILE_CURRENT
      • FILE_END
    • returns:
      • 32-LSBs of the new file pointer, the other 32bits are set in lpDistanceToMoveHigh if not null
      • 0xffffffff in case of error
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

SetFilePointerEx

A

SetFilePointerEx:

  • uses LARGE_INTEGER (64bits) for file positions
  • params:
    • hFile
    • liDistanceToMove:
    • lpNewFilePointer:
    • dwMoveMethod:
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Overlapped Datastrcture for synch i/o

A
  • Internal and InternalHigh are reserved.
  • Offset 32-LSBs
  • OffsetHigh: 32-MSBs
  • hEvent is used with async I/O, must be NULL for synchronous ops
  • offset is always using FILE_BEGIN equivalent of SetFilePointer
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

File Size and appending to a file

A
  • To append a new record to the end of an existing file:
    • set Offset and OffsetHigh to 0xFFFFFFFF, before a write op.
  • To get file size:
    • using SetFilePointerEx:
      • set position 0 bytes from the end
      • get lpNewFilePointer returned
    • use GetFileSizeEx
      • hFile
      • lpFileSize (pointer to file size on success
      • returns false on failure
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

FileLocking

A
  • In Windows it is possible to lock file regions so that no other process or thread can access it.
  • A lock belongs to a process.
  • It is possible to Lock beyond the file end.
  • A lock fails if a portion of the record is locked.
  • Locks:
    • entire file
    • part of file
  • Locking mode:
    • shared
    • exclusive
  • Conflicting cannot be created, locks can’t be overlapped
    • For each thread that requires a lock on a file must do so from a different handle.

LockFileEx:

  • hFile: must have generic read or write access
  • dwFlags: describes how to lock and how to wait
    • LOCKFILE_EXCLUSIVE_LOCK: if includes this bit the lock is exclusive, otherwise shared
    • LOCKFILE_FAIL_IMMEDIATELY: returns if section is locked
  • dwRserved: reserved, must be 0
  • nNumberOfBytesToLockLow
  • nNumberOfBytesToLockHigh
  • lpOverlapped: contains overlapped ds with pointer to start of section to be locked.

UnlockFileEx:

  • must use exactly the same range as before lock
  • same parameters as LockFileEx, but dwFlags
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Overlapped I/O with waiting

A

Thread continues execution after issuing I/O request.

When the thread requires the result it await until it is ready.

Overlapped I/O with waiting uses the overlapped data structure to implement asyncrhonous functions.

We need to specify FILE_FLAG_OVERLAPPED flag inside CreateFile in fdwAttrsAndFlags,

then we use the file handle or the overlapped event to wait for completion.

ReadFile and WriteFile may block the process if not in overlapped mode.

In overlapped mode, neither the return value from ReadFile/WriteFile nor the returned number of bytes is relevant as to wheter it was successful or not .

How well did you know this?
1
Not at all
2
3
4
5
Perfectly