uLib
User mode C/C++ extended API library for Win32 programmers.
|
I/O related functions... (IoUtil.cpp)
Groups | |
Memory Mapped Files | |
Macros | |
#define | OPEN_FILE(name, access) CreateFile( name, access, FILE_SHARE_READ, &DefSec, OPEN_EXISTING, 0,0 ) |
#define | LONG_PATH_PREFIX_A "\\\\?\\" |
#define | LONG_UNC_PREFIX_A LONG_PATH_PREFIX_A "UNC\\" |
#define | DEVICE_NAME_PREFIX_A "\\\\.\\" |
#define | UNPARSED_NAME_PREFIX_A "\\??\\" |
#define | LONG_PATH_PREFIX_W L"\\\\?\\" |
#define | LONG_UNC_PREFIX_W LONG_PATH_PREFIX_W L"UNC\\" |
#define | DEVICE_NAME_PREFIX_W L"\\\\.\\" |
#define | UNPARSED_NAME_PREFIX_W L"\\??\\" |
#define | LONG_PATH_PREFIX LONG_PATH_PREFIX_A |
#define | LONG_UNC_PREFIX LONG_UNC_PREFIX_A |
#define | DEVICE_NAME_PREFIX DEVICE_NAME_PREFIX_A |
#define | UNPARSED_NAME_PREFIX UNPARSED_NAME_PREFIX_A |
#define | SkipPathPrefix SkipPathPrefixA |
#define | GetFilePointer(hFile, pHiPart) SetFilePointer( hFile, 0, (PLONG)pHiPart, FILE_CURRENT ) |
#define | WriteStr WriteStrA |
#define | ReadLine ReadLineA |
Typedefs | |
typedef bool(__stdcall * | PFnEnumHardLinks) (CSTR PathName, PVOID usrData) |
typedef bool(__stdcall * | PFnEnumStreams) (CSTR PathName, UINT64 Size, PVOID usrData) |
Functions | |
HANDLE | OpenDirectory (CSTR DirPath, DWORD Flags) |
ACSTR | SkipPathPrefixA (ACSTR PathName) |
WCSTR | SkipPathPrefixW (WCSTR PathName) |
bool | FileExist (CSTR PathName) |
bool | DirExist (CSTR PathName) |
bool | IsDir (LPWIN32_FIND_DATA pFd) |
bool | IsSubdirName (CSTR Dir) |
bool | IsSubdir (LPWIN32_FIND_DATA pFd) |
bool | IsJunctionDir (CSTR PathName) |
bool | IsSymLink (CSTR PathName) |
bool | IsSymLinkDir (CSTR PathName) |
bool | IsVolumeMountPoint (CSTR PathName, TSTR VolNameBuf=NULL, UINT ccBuf=0) |
bool | GetWin32FileData (CSTR PathName, LPWIN32_FIND_DATA pFd) |
UINT64 | SetFilePos (HANDLE hFile, INT64 Offset, UINT How) |
UINT64 | GetFilePos (HANDLE hFile) |
bool | Seek (HANDLE hFile, LONG Offs, DWORD How) |
UINT | Tell (HANDLE hFile) |
bool | IsEndOfFile (HANDLE hFile) |
UINT64 | GetFileSize64 (HANDLE hFile) |
UINT64 | GetFileSize64Ex (CSTR FName) |
UINT64 | GetFileSizeByName (CSTR FName) |
UINT | PeekFile (HANDLE hFile, PVOID Buf, UINT nBytes) |
UINT | BlockRead (HANDLE hFile, PVOID Buf, UINT nBytes) |
UINT | BlockWrite (HANDLE hFile, PVOID Buf, UINT nBytes) |
BOOL | WriteLine (HANDLE hFile, CSTR Buf, UINT ccBuf=0) |
UINT __cdecl | WriteStrA (HANDLE hFile, const char *Fmt,...) |
UINT __cdecl | WriteStrW (HANDLE hFile, const wchar_t *Fmt,...) |
UINT | ReadLineA (HANDLE hFile, char *Buffer, UINT BufLength, bool TrimCrLf) |
UINT | ReadLineW (HANDLE hFile, wchar_t *Buffer, UINT BufLength, bool TrimCrLf) |
TSTR | GetPathFileName (TSTR PathName) |
UINT | GetFileNameFromHandle (HANDLE hFile, OUT TSTR Buffer, UINT BufLen) |
UINT | DevicePathToDosPath (IN CSTR DevPath, OUT TSTR PathBuf, UINT ccPathBuf) |
CSTR | ChangeDirectory (CSTR Dir) |
CSTR | DoneDirectory (CSTR prevDir) |
bool | FindMoreFiles (HANDLE hFind, WIN32_FIND_DATA *Found, OPTIN BYTE nTries=1, OPTIN BYTE msWait=50) |
HANDLE | FindCloseEx (HANDLE hFind) |
CSTR | GetTempPathName (CSTR Prefix, CSTR dotExt) |
ULARGE_INTEGER | GetAttributeAndReparseTag (CSTR PathName) |
bool | GetFileInformationByName (CSTR FileName, LPBY_HANDLE_FILE_INFORMATION pInfo) |
bool | GetVolumeGuidOfShare (IN CSTR ShareName, OUT TSTR VolGuid, WORD ccGuid, OPTOUT TSTR DosDrv, WORD ccDrv) |
bool | GetVolumeGuidOfDevice (IN CSTR DevName, OUT TSTR VolGuid, WORD ccGuid, OPTOUT TSTR DosDrv, WORD ccDrv) |
bool | CreateJunctionDir (CSTR LinkName, CSTR TargetPath, CSTR PrintName) |
bool | DeleteJunctionDir (CSTR LinkName) |
bool | CreateSymLink (CSTR LinkName, CSTR TargetPath, CSTR PrintName) |
bool | RemoveReparseData (CSTR PathName) |
INT | GetReparseTarget (IN CSTR LinkName, OUT TSTR SubstPath, UINT cchSubst, OPTOUT TSTR PrintName, UINT cchPrint, OPTOUT PULARGE_INTEGER AttrTag) |
UINT | GetHardLinkCount (CSTR FileName) |
bool | _CreateHardLink (CSTR LinkName, CSTR TargetName) |
UINT | EnumerateHardLinks (CSTR FileName, PFnEnumHardLinks Action, PVOID UserData) |
UINT | EnumerateFileStreams (CSTR FileName, PFnEnumStreams Action, PVOID usrData) |
#define OPEN_FILE | ( | name, | |
access | |||
) | CreateFile( name, access, FILE_SHARE_READ, &DefSec, OPEN_EXISTING, 0,0 ) |
Open existing file with shared reading (Most common file usage I can concieve).
Definition at line 2333 of file UtilFunc.h.
#define LONG_PATH_PREFIX_A "\\\\?\\" |
("\\?\") Disable Win32 name parsing and override the MAX_PATH limit.
Definition at line 2345 of file UtilFunc.h.
#define LONG_UNC_PREFIX_A LONG_PATH_PREFIX_A "UNC\\" |
("\\?\UNC\") Same as "\\?\" but applies to UNC paths.
Definition at line 2346 of file UtilFunc.h.
#define DEVICE_NAME_PREFIX_A "\\\\.\\" |
("\\.\") A device in the Win32 device name space.
Definition at line 2347 of file UtilFunc.h.
#define UNPARSED_NAME_PREFIX_A "\\??\\" |
("\??\") AFAIK: The pathname should not be parsed. Used e.g for symlink abs targets.
Definition at line 2348 of file UtilFunc.h.
#define LONG_PATH_PREFIX_W L"\\\\?\\" |
Definition at line 2350 of file UtilFunc.h.
#define LONG_UNC_PREFIX_W LONG_PATH_PREFIX_W L"UNC\\" |
Definition at line 2351 of file UtilFunc.h.
#define DEVICE_NAME_PREFIX_W L"\\\\.\\" |
Definition at line 2352 of file UtilFunc.h.
#define UNPARSED_NAME_PREFIX_W L"\\??\\" |
Definition at line 2353 of file UtilFunc.h.
#define LONG_PATH_PREFIX LONG_PATH_PREFIX_A |
Definition at line 2361 of file UtilFunc.h.
#define LONG_UNC_PREFIX LONG_UNC_PREFIX_A |
Definition at line 2362 of file UtilFunc.h.
#define DEVICE_NAME_PREFIX DEVICE_NAME_PREFIX_A |
Definition at line 2363 of file UtilFunc.h.
#define UNPARSED_NAME_PREFIX UNPARSED_NAME_PREFIX_A |
Definition at line 2364 of file UtilFunc.h.
#define SkipPathPrefix SkipPathPrefixA |
Definition at line 2375 of file UtilFunc.h.
#define GetFilePointer | ( | hFile, | |
pHiPart | |||
) | SetFilePointer( hFile, 0, (PLONG)pHiPart, FILE_CURRENT ) |
Get the current file position in hFile
.\nSee also GetFilePos().
Definition at line 2409 of file UtilFunc.h.
#define WriteStr WriteStrA |
Definition at line 2459 of file UtilFunc.h.
#define ReadLine ReadLineA |
Definition at line 2460 of file UtilFunc.h.
typedef bool(__stdcall * PFnEnumHardLinks) (CSTR PathName, PVOID usrData) |
Callback for EnumerateHardLinks(). Return true to continue.
PathName specifies the absolute path name of the hardlink, including the drive spec.
Definition at line 2652 of file UtilFunc.h.
PFnEnumStreams is the callback for EnumerateFileStreams(). Return true to continue.
PathName specifies the absolute path name of the stream, including the drive spec.
Definition at line 2667 of file UtilFunc.h.
OpenDirectory opens a disk directory for direct access.
Flags may be f.ex. FILE_LIST_DIRECTORY. FILE_FLAG_BACKUP_SEMANTICS is added by default.
The returned handle can be used with f.ex. ReadDirectoryChangesW() or DeviceIoControl().
Requires SE_BACKUP_NAME / SE_RESTORE_NAME privilege. On failure the function returns NULL.
Definition at line 385 of file IoUtil.cpp.
Return a pointer to the first char past any known namespace prefix in PathName.
Definition at line 108 of file IoUtil.cpp.
See SkipPathPrefixA().
Definition at line 133 of file IoUtil.cpp.
bool FileExist | ( | CSTR | PathName | ) |
True if file exist. VERY fast.
Definition at line 24 of file IoUtil.cpp.
bool DirExist | ( | CSTR | PathName | ) |
True if directory exist. VERY fast.
Definition at line 30 of file IoUtil.cpp.
bool IsDir | ( | LPWIN32_FIND_DATA | pFd | ) |
True if pFd refers to a directory.
Definition at line 38 of file IoUtil.cpp.
bool IsSubdirName | ( | CSTR | Dir | ) |
True if Dir is a subdir name (not "." or "..").
Definition at line 43 of file IoUtil.cpp.
bool IsSubdir | ( | LPWIN32_FIND_DATA | pFd | ) |
True if pFd refers to a subdirectory.
Definition at line 55 of file IoUtil.cpp.
bool IsJunctionDir | ( | CSTR | PathName | ) |
NTFS junction to another directory? (IO_REPARSE_TAG_MOUNT_POINT)
Definition at line 640 of file ReparsePnt.cpp.
bool IsSymLink | ( | CSTR | PathName | ) |
NTFS symbolic link? (IO_REPARSE_TAG_SYMLINK)
Definition at line 624 of file ReparsePnt.cpp.
bool IsSymLinkDir | ( | CSTR | PathName | ) |
NTFS symbolic link to a directory? (IO_REPARSE_TAG_SYMLINK and FILE_ATTRIBUTE_DIRECTORY)
Definition at line 632 of file ReparsePnt.cpp.
Determine if directory is a junction mounted volume.
Note: IsVolumeMountPoint fails for mapped network directories (they're not mount points).
Definition at line 652 of file ReparsePnt.cpp.
bool GetWin32FileData | ( | CSTR | PathName, |
LPWIN32_FIND_DATA | pFd | ||
) |
GetWin32FileData gets the FindFile() data for a file or subdirectory.
Note: For directories, PathName should not have a terminal backslash.
For root directories pFd->cFilename recieves esotheric OS info.
Definition at line 84 of file IoUtil.cpp.
Set current (64 bit) file position in hFile
.
See [MSDN] SetFilePointer() for parameter details.
Definition at line 306 of file IoUtil.cpp.
UINT64 GetFilePos | ( | HANDLE | hFile | ) |
Get current (64 bit) file position in hFile.
Definition at line 299 of file IoUtil.cpp.
bool Seek | ( | HANDLE | hFile, |
LONG | Offs, | ||
DWORD | How | ||
) |
Set current (32 bit) file position in hFile
.
Definition at line 316 of file IoUtil.cpp.
UINT Tell | ( | HANDLE | hFile | ) |
Get current (32 bit) file position in hFile
.
Definition at line 321 of file IoUtil.cpp.
bool IsEndOfFile | ( | HANDLE | hFile | ) |
Amend strange omission in Win32 APIs. True if hFile
is at end.
Definition at line 395 of file IoUtil.cpp.
UINT64 GetFileSize64 | ( | HANDLE | hFile | ) |
Get the size of an open file. Note: Symlink behavior is: Size of the Target.
Definition at line 328 of file IoUtil.cpp.
Get the size of a named file. Note: Symlink behavior is: Size of the Target.
Definition at line 336 of file IoUtil.cpp.
Get the size of an unopened file. Note: Symlink behavior is: Size of the Link (0).
Definition at line 354 of file IoUtil.cpp.
UINT PeekFile | ( | HANDLE | hFile, |
PVOID | Buf, | ||
UINT | nBytes | ||
) |
Read from file without moving the file pointer.
Definition at line 404 of file IoUtil.cpp.
UINT BlockRead | ( | HANDLE | hFile, |
PVOID | Buf, | ||
UINT | nBytes | ||
) |
Read a data block from hFile. Return nr of bytes read.
Definition at line 417 of file IoUtil.cpp.
UINT BlockWrite | ( | HANDLE | hFile, |
PVOID | Buf, | ||
UINT | nBytes | ||
) |
Write a data block to hFile. Return nr of bytes written.
Definition at line 423 of file IoUtil.cpp.
WriteLine writes text to hFile, and appends CRLF.
If ccBuf is zero, the whole string is written, else that nr of TCHARs.
Definition at line 432 of file IoUtil.cpp.
UINT __cdecl WriteStrA | ( | HANDLE | hFile, |
const char * | Fmt, | ||
... | |||
) |
WriteStr formats a string into an internal buffer of 512 chars, and writes it.
It returns the nr of characters, not bytes, written to the file.
On error, the return value is zero, and GetLastError() returns the reason.
UINT __cdecl WriteStrW | ( | HANDLE | hFile, |
const wchar_t * | Fmt, | ||
... | |||
) |
Definition at line 492 of file IoUtil.cpp.
UINT ReadLineA | ( | HANDLE | hFile, |
char * | Buffer, | ||
UINT | BufLength, | ||
bool | TrimCrLf | ||
) |
ReadLine reads a line of text, up to the next [CR]LF found.
Definition at line 508 of file IoUtil.cpp.
UINT ReadLineW | ( | HANDLE | hFile, |
wchar_t * | Buffer, | ||
UINT | BufLength, | ||
bool | TrimCrLf | ||
) |
See ReadLineA()
Definition at line 565 of file IoUtil.cpp.
GetPathFileName gets a pointer to the "filname.ext" part of PathName.
If there is no '\' in PathName, you'll get a pointer to PathName itself.
Definition at line 620 of file IoUtil.cpp.
UINT GetFileNameFromHandle | ( | HANDLE | hFile, |
OUT TSTR | Buffer, | ||
UINT | BufLen | ||
) |
Get the file name of the specified handle.
Return nr of chars copied to Buffer, or zero on failure.
See GetFinalPathNameByHandle:VOLUME_NAME_DOS and NtQueryObject:ObjectNameInformation.
Translate path with device name to a DOS path (that is, with drive letter).
Return length of resulting pathname, in chars, excluding NUL terminator.
Note: You may translate in-place by passing the same buffer as DevPath and PathBuf.
Definition at line 1007 of file IoUtil.cpp.
ChangeDirectory combines GetCurrentDirectory() and SetCurrentDirectory().
The function returns a newStr() duplicate of previous dir, or NULL on error.
Note: Use DoneDirectory() or deleteStr() when done with the returned string.
Definition at line 1026 of file IoUtil.cpp.
Restore previous working directory and free prevDir.
See ChangeDirectory().
Definition at line 1040 of file IoUtil.cpp.
bool FindMoreFiles | ( | HANDLE | hFind, |
WIN32_FIND_DATA * | Found, | ||
OPTIN BYTE | nTries = 1 , |
||
OPTIN BYTE | msWait = 50 |
||
) |
FindMoreFiles combines FindNextFile() and FindClose().
If FindNextFile fails, it calls FindClose and returns false.
hFind | Handle from FindFirstFile. |
Found | Recieves item data for the file. |
nTries | [optional] Max tries for FindNextFile (in case of error). |
msWait | [optional] Millisec wait between retries for FindNextFile. |
HANDLE FindCloseEx | ( | HANDLE | hFind | ) |
FindCloseEx closes a find-handle, and returns NULL on success.
On failure, the handle is returned as-is.
Definition at line 1072 of file IoUtil.cpp.
GetTempPathName returns a full tempfile pathname, with specific ext.
Note: The function uses a static buffer for the generated path name.
Prefix | Prefix for the file name. Only the first 3 chars are used. |
dotExt | Extension, with leading dot. Use NULL or a blank string for extensionless name. |
See also GetTempFileName() and GetTempPath().
Definition at line 62 of file IoUtil.cpp.
ULARGE_INTEGER GetAttributeAndReparseTag | ( | CSTR | PathName | ) |
GetAttributeAndReparseTag gets the file attributes and reparse tag for a file.
Return file attributes in LowPart, and reparse tag, if any, in HighPart.
On failure, the result is zero, not INVALID_FILE_ATTRIBUTES.
Definition at line 554 of file ReparsePnt.cpp.
bool GetFileInformationByName | ( | CSTR | FileName, |
LPBY_HANDLE_FILE_INFORMATION | pInfo | ||
) |
See [MSDN] GetFileInformationByHandle().
Definition at line 163 of file IoUtil.cpp.
bool GetVolumeGuidOfShare | ( | IN CSTR | ShareName, |
OUT TSTR | VolGuid, | ||
WORD | ccGuid, | ||
OPTOUT TSTR | DosDrv, | ||
WORD | ccDrv | ||
) |
Return the volume GUID of an UNC share ("\\Server\Share").
Definition at line 180 of file IoUtil.cpp.
bool GetVolumeGuidOfDevice | ( | IN CSTR | DevName, |
OUT TSTR | VolGuid, | ||
WORD | ccGuid, | ||
OPTOUT TSTR | DosDrv, | ||
WORD | ccDrv | ||
) |
Return the volume GUID of a disk device, e.g ("\Device\HarddiskVolumeX").
Definition at line 273 of file IoUtil.cpp.
CreateJunctionDir() creates a directory junction (if the volume supports it).
This function works with both directory junctions and volume mount points.
The link directory is automatically created before mounting the directory or volume.
You can also set the "friendly name".
LinkName | Pathname of the junction directory to create. |
TargetPath | Absolute path of an existing target to link to. This can be an absolute DOS directory path, or a volume GUID. |
PrintName | A "friendly" print name (ATM not shown by the shell). If this is NULL, TargetPath is used as a base for the print name. |
Definition at line 766 of file ReparsePnt.cpp.
bool DeleteJunctionDir | ( | CSTR | LinkName | ) |
DeleteJunctionDir() deletes a directory junction (without touching the link target).
See also CreateJunctionDir(), GetReparseTarget(), RemoveReparseData().
Definition at line 796 of file ReparsePnt.cpp.
CreateSymLink() creates a symbolic link to a file or directory.
This function sets the full surrogate name, including the "friendly" print name.
(The Win32 API CreateSymbolicLink() doesn't provide access to the print name.)
LinkName | Pathname of the symbolic link to create. |
TargetPath | Absolute or relative path of an existing target. Note: Directory targets must be absolute, not relative. |
PrintName | A "friendly" print name (ATM not shown by the shell). If this is NULL, TargetPath is used as a base for the print name. |
Definition at line 822 of file ReparsePnt.cpp.
bool RemoveReparseData | ( | CSTR | PathName | ) |
RemoveReparseData() removes the reparse data from a file or directory.
See also CreateSymLink(), CreateJunctionDir().
Definition at line 687 of file ReparsePnt.cpp.
INT GetReparseTarget | ( | IN CSTR | LinkName, |
OUT TSTR | SubstPath, | ||
UINT | cchSubst, | ||
OPTOUT TSTR | PrintName, | ||
UINT | cchPrint, | ||
OPTOUT PULARGE_INTEGER | AttrTag | ||
) |
GetReparseTarget() gets the target of a junction or symlink.
[in] | LinkName | Pathname of the link. |
[out] | SubstPath | Buffer to recieve the target pathname. This buffer should normally be at least MAX_PATH long. |
[in] | cchSubst | Length, in TCHARs, of the target buffer. |
[out] | PrintName | [optional] Buffer that recieves the "friendly" name. |
[in] | cchPrint | Length, in TCHARs, of the PrintName buffer (if supplied). |
[out] | AttrTag | [optional] If provided, recieves the GetAttributeAndReparseTag() value. |
UINT GetHardLinkCount | ( | CSTR | FileName | ) |
Return the nr of NTFS hard links (that is, MFT entries) for FileName.
Definition at line 7 of file Hardlink.cpp.
_CreateHardLink() creates an hard link to a file (if the volume supports it).
Note that the system has had this function since Win2k, so this is a legacy version.
Also note that this one omits the security parameter of the Win32 call, since it
doesn't apply to hard links (MFT entries), only the files they link to, and
I don't think a link should be allowed to change the security on the original file.
Definition at line 122 of file Hardlink.cpp.
UINT EnumerateHardLinks | ( | CSTR | FileName, |
PFnEnumHardLinks | Action, | ||
PVOID | UserData | ||
) |
[PRELIMINARY] Enumerate the hardlinks for FileName. Return nr of links processed.
Note: Not all file systems support hardlinks, and hence return zero.
This function requires Vista++.
FileName | Pathname of the file. |
Action | Callback function. |
UserData | Arbitrary data for the callback. |
Definition at line 236 of file Hardlink.cpp.
UINT EnumerateFileStreams | ( | CSTR | FileName, |
PFnEnumStreams | Action, | ||
PVOID | usrData | ||
) |
[PRELIMINARY] Enumerate $DATA streams contained in a file. Return nr of streams processed.
Note: Not all file systems support file streams, and hence return zero.
This function requires Vista++.
Data stream. The default data stream has no name.
Data streams can be enumerated using the FindFirstStreamW and FindNextStreamW functions.
These streams can be enumerated with EnumerateFileStreams().
Similar to ::$DATA but operations are logged to the NTFS change journal.
Used by EFS and Transactional NTFS (TxF). The :StreamName:$StreamType pair for EFS is :$EFS:$LOGGED_UTILITY_STREAM and for TxF is :$TXF_DATA:$LOGGED_UTILITY_STREAM.
Contains a list of all attributes that make up the file and identifies where each attribute is located.
A bitmap used by indexes to manage the B-tree free space for a directory.
The B-tree is managed in 4 KB chunks (regardless of cluster size) and this is used to manage the allocation of these chunks. This stream type is present on every directory.
Contains Extended Attributes data.
Contains support information about the Extended Attributes.
The name of the file, in Unicode characters.
This includes the short name of the file as well as any hard links.
The stream type of a directory. Used to implement filename allocation for large directories.
This stream represents the directory itself and contains all of the data of the directory. Changes to streams of this type are logged to the NTFS change journal. The default stream name of an $INDEX_ALLOCATION stream type is $I30 so "DirName", "DirName::$INDEX_ALLOCATION", and "DirName:$I30:$INDEX_ALLOCATION" are all equivalent.
This stream represents root of the b-tree of an index.
This stream type is present on every directory.
An 16-byte ID used to identify the file for the link-tracking service.
Definition at line 1084 of file IoUtil.cpp.