1 // Windows/FileIO.h
2 
3 #ifndef __WINDOWS_FILEIO_H
4 #define __WINDOWS_FILEIO_H
5 
6 #include "../Common/Types.h"
7 
8 #include "Defs.h"
9 
10 namespace NWindows {
11 namespace NFile {
12 namespace NIO {
13 
14 struct CByHandleFileInfo
15 {
16   DWORD Attrib;
17   FILETIME CTime;
18   FILETIME ATime;
19   FILETIME MTime;
20   DWORD VolumeSerialNumber;
21   UInt64 Size;
22   DWORD NumberOfLinks;
23   UInt64 FileIndex;
24 };
25 
26 class CFileBase
27 {
28 protected:
29   HANDLE _handle;
30 
31   bool Create(LPCTSTR fileName, DWORD desiredAccess,
32       DWORD shareMode, DWORD creationDisposition, DWORD flagsAndAttributes);
33   #ifndef _UNICODE
34   bool Create(LPCWSTR fileName, DWORD desiredAccess,
35       DWORD shareMode, DWORD creationDisposition, DWORD flagsAndAttributes);
36   #endif
37 
38 public:
39   #ifdef SUPPORT_DEVICE_FILE
40   bool IsDeviceFile;
41   bool LengthDefined;
42   UInt64 Length;
43   #endif
44 
CFileBase()45   CFileBase(): _handle(INVALID_HANDLE_VALUE) {};
46   ~CFileBase();
47 
48   bool Close();
49 
50   bool GetPosition(UInt64 &position) const;
51   bool GetLength(UInt64 &length) const;
52 
53   bool Seek(Int64 distanceToMove, DWORD moveMethod, UInt64 &newPosition) const;
54   bool Seek(UInt64 position, UInt64 &newPosition);
55   bool SeekToBegin();
56   bool SeekToEnd(UInt64 &newPosition);
57 
58   bool GetFileInformation(CByHandleFileInfo &fileInfo) const;
59 };
60 
61 #define IOCTL_CDROM_BASE  FILE_DEVICE_CD_ROM
62 #define IOCTL_CDROM_GET_DRIVE_GEOMETRY  CTL_CODE(IOCTL_CDROM_BASE, 0x0013, METHOD_BUFFERED, FILE_READ_ACCESS)
63 #define IOCTL_CDROM_MEDIA_REMOVAL  CTL_CODE(IOCTL_CDROM_BASE, 0x0201, METHOD_BUFFERED, FILE_READ_ACCESS)
64 
65 class CInFile: public CFileBase
66 {
67   #ifdef SUPPORT_DEVICE_FILE
DeviceIoControl(DWORD controlCode,LPVOID inBuffer,DWORD inSize,LPVOID outBuffer,DWORD outSize,LPDWORD bytesReturned,LPOVERLAPPED overlapped)68   bool DeviceIoControl(DWORD controlCode, LPVOID inBuffer, DWORD inSize,
69       LPVOID outBuffer, DWORD outSize, LPDWORD bytesReturned, LPOVERLAPPED overlapped) const
70   {
71     return BOOLToBool(::DeviceIoControl(_handle, controlCode, inBuffer, inSize,
72         outBuffer, outSize, bytesReturned, overlapped));
73   }
74 
DeviceIoControl(DWORD controlCode,LPVOID inBuffer,DWORD inSize,LPVOID outBuffer,DWORD outSize)75   bool DeviceIoControl(DWORD controlCode, LPVOID inBuffer,
76       DWORD inSize, LPVOID outBuffer, DWORD outSize) const
77   {
78     DWORD ret;
79     return DeviceIoControl(controlCode, inBuffer, inSize, outBuffer, outSize, &ret, 0);
80   }
81 
DeviceIoControlOut(DWORD controlCode,LPVOID outBuffer,DWORD outSize)82   bool DeviceIoControlOut(DWORD controlCode, LPVOID outBuffer, DWORD outSize) const
83     { return DeviceIoControl(controlCode, NULL, 0, outBuffer, outSize); }
84 
85   #ifndef UNDER_CE
GetGeometry(DISK_GEOMETRY * res)86   bool GetGeometry(DISK_GEOMETRY *res) const
87     { return DeviceIoControlOut(IOCTL_DISK_GET_DRIVE_GEOMETRY, res, sizeof(*res)); }
88 
GetCdRomGeometry(DISK_GEOMETRY * res)89   bool GetCdRomGeometry(DISK_GEOMETRY *res) const
90     { return DeviceIoControlOut(IOCTL_CDROM_GET_DRIVE_GEOMETRY, res, sizeof(*res)); }
91 
GetPartitionInfo(PARTITION_INFORMATION * res)92   bool GetPartitionInfo(PARTITION_INFORMATION *res)
93     { return DeviceIoControlOut(IOCTL_DISK_GET_PARTITION_INFO, LPVOID(res), sizeof(*res)); }
94   #endif
95 
96   void GetDeviceLength();
97   #endif
98 
99 public:
100   bool Open(LPCTSTR fileName, DWORD shareMode, DWORD creationDisposition, DWORD flagsAndAttributes);
101   bool OpenShared(LPCTSTR fileName, bool shareForWrite);
102   bool Open(LPCTSTR fileName);
103   #ifndef _UNICODE
104   bool Open(LPCWSTR fileName, DWORD shareMode, DWORD creationDisposition, DWORD flagsAndAttributes);
105   bool OpenShared(LPCWSTR fileName, bool shareForWrite);
106   bool Open(LPCWSTR fileName);
107   #endif
108   bool Read1(void *data, UInt32 size, UInt32 &processedSize);
109   bool ReadPart(void *data, UInt32 size, UInt32 &processedSize);
110   bool Read(void *data, UInt32 size, UInt32 &processedSize);
111 };
112 
113 class COutFile: public CFileBase
114 {
115 public:
116   bool Open(LPCTSTR fileName, DWORD shareMode, DWORD creationDisposition, DWORD flagsAndAttributes);
117   bool Open(LPCTSTR fileName, DWORD creationDisposition);
118   bool Create(LPCTSTR fileName, bool createAlways);
119 
120   #ifndef _UNICODE
121   bool Open(LPCWSTR fileName, DWORD shareMode, DWORD creationDisposition, DWORD flagsAndAttributes);
122   bool Open(LPCWSTR fileName, DWORD creationDisposition);
123   bool Create(LPCWSTR fileName, bool createAlways);
124   #endif
125 
126   bool SetTime(const FILETIME *cTime, const FILETIME *aTime, const FILETIME *mTime);
127   bool SetMTime(const FILETIME *mTime);
128   bool WritePart(const void *data, UInt32 size, UInt32 &processedSize);
129   bool Write(const void *data, UInt32 size, UInt32 &processedSize);
130   bool SetEndOfFile();
131   bool SetLength(UInt64 length);
132 };
133 
134 }}}
135 
136 #endif
137