Create new file on disk and make sure that there is enough space on disk for it.
| 109 | // Create new file on disk and make sure that there is enough |
| 110 | // space on disk for it. |
| 111 | bool DiskFile::Create(std::string _filename, u64 _filesize) |
| 112 | { |
| 113 | assert(hFile == INVALID_HANDLE_VALUE); |
| 114 | |
| 115 | filename = _filename; |
| 116 | filesize = _filesize; |
| 117 | |
| 118 | if (!DiskFile::CreateParentDirectory(filename)) |
| 119 | return false; |
| 120 | |
| 121 | // Create the file |
| 122 | std::wstring wfilename = utf8::Utf8ToWide(_filename); |
| 123 | hFile = ::CreateFileW(wfilename.c_str(), GENERIC_READ | GENERIC_WRITE, 0, NULL, CREATE_NEW, 0, NULL); |
| 124 | if (hFile == INVALID_HANDLE_VALUE) |
| 125 | { |
| 126 | DWORD error = ::GetLastError(); |
| 127 | |
| 128 | #pragma omp critical |
| 129 | *serr << "Could not create \"" << _filename << "\": " << ErrorMessage(error) << std::endl; |
| 130 | |
| 131 | return false; |
| 132 | } |
| 133 | |
| 134 | if (filesize > 0) |
| 135 | { |
| 136 | // Seek to the end of the file |
| 137 | LONG* ptrfilesize = (LONG*)&filesize; |
| 138 | LONG lowoffset = ptrfilesize[0]; |
| 139 | LONG highoffset = ptrfilesize[1]; |
| 140 | |
| 141 | if (INVALID_SET_FILE_POINTER == SetFilePointer(hFile, lowoffset, &highoffset, FILE_BEGIN)) |
| 142 | { |
| 143 | DWORD error = ::GetLastError(); |
| 144 | |
| 145 | #pragma omp critical |
| 146 | *serr << "Could not set size of \"" << _filename << "\": " << ErrorMessage(error) << std::endl; |
| 147 | |
| 148 | ::CloseHandle(hFile); |
| 149 | hFile = INVALID_HANDLE_VALUE; |
| 150 | ::DeleteFileW(wfilename.c_str()); |
| 151 | |
| 152 | return false; |
| 153 | } |
| 154 | |
| 155 | // Set the end of the file |
| 156 | if (!::SetEndOfFile(hFile)) |
| 157 | { |
| 158 | DWORD error = ::GetLastError(); |
| 159 | |
| 160 | #pragma omp critical |
| 161 | *serr << "Could not set size of \"" << _filename << "\": " << ErrorMessage(error) << std::endl; |
| 162 | |
| 163 | ::CloseHandle(hFile); |
| 164 | hFile = INVALID_HANDLE_VALUE; |
| 165 | ::DeleteFileW(wfilename.c_str()); |
| 166 | |
| 167 | return false; |
| 168 | } |