Skip to content

Commit 4e6fad8

Browse files
committed
Merge pull request pocoproject#664 from cryptoknight/WriteOnCopy
Don't automatically make copied files writable on Windows
2 parents 71fb76b + e3c5cd3 commit 4e6fad8

4 files changed

Lines changed: 68 additions & 78 deletions

File tree

Foundation/src/File_WIN32.cpp

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -34,17 +34,17 @@ class FileHandle
3434
FileImpl::handleLastErrorImpl(path);
3535
}
3636
}
37-
37+
3838
~FileHandle()
3939
{
4040
if (_h != INVALID_HANDLE_VALUE) CloseHandle(_h);
4141
}
42-
42+
4343
HANDLE get() const
4444
{
4545
return _h;
4646
}
47-
47+
4848
private:
4949
HANDLE _h;
5050
};
@@ -112,7 +112,7 @@ bool FileImpl::existsImpl() const
112112
bool FileImpl::canReadImpl() const
113113
{
114114
poco_assert (!_path.empty());
115-
115+
116116
DWORD attr = GetFileAttributes(_path.c_str());
117117
if (attr == INVALID_FILE_ATTRIBUTES)
118118
{
@@ -131,7 +131,7 @@ bool FileImpl::canReadImpl() const
131131
bool FileImpl::canWriteImpl() const
132132
{
133133
poco_assert (!_path.empty());
134-
134+
135135
DWORD attr = GetFileAttributes(_path.c_str());
136136
if (attr == INVALID_FILE_ATTRIBUTES)
137137
handleLastErrorImpl(_path);
@@ -201,7 +201,7 @@ Timestamp FileImpl::createdImpl() const
201201
poco_assert (!_path.empty());
202202

203203
WIN32_FILE_ATTRIBUTE_DATA fad;
204-
if (GetFileAttributesEx(_path.c_str(), GetFileExInfoStandard, &fad) == 0)
204+
if (GetFileAttributesEx(_path.c_str(), GetFileExInfoStandard, &fad) == 0)
205205
handleLastErrorImpl(_path);
206206
return Timestamp::fromFileTimeNP(fad.ftCreationTime.dwLowDateTime, fad.ftCreationTime.dwHighDateTime);
207207
}
@@ -212,7 +212,7 @@ Timestamp FileImpl::getLastModifiedImpl() const
212212
poco_assert (!_path.empty());
213213

214214
WIN32_FILE_ATTRIBUTE_DATA fad;
215-
if (GetFileAttributesEx(_path.c_str(), GetFileExInfoStandard, &fad) == 0)
215+
if (GetFileAttributesEx(_path.c_str(), GetFileExInfoStandard, &fad) == 0)
216216
handleLastErrorImpl(_path);
217217
return Timestamp::fromFileTimeNP(fad.ftLastWriteTime.dwLowDateTime, fad.ftLastWriteTime.dwHighDateTime);
218218
}
@@ -239,7 +239,7 @@ FileImpl::FileSizeImpl FileImpl::getSizeImpl() const
239239
poco_assert (!_path.empty());
240240

241241
WIN32_FILE_ATTRIBUTE_DATA fad;
242-
if (GetFileAttributesEx(_path.c_str(), GetFileExInfoStandard, &fad) == 0)
242+
if (GetFileAttributesEx(_path.c_str(), GetFileExInfoStandard, &fad) == 0)
243243
handleLastErrorImpl(_path);
244244
LARGE_INTEGER li;
245245
li.LowPart = fad.nFileSizeLow;
@@ -257,7 +257,7 @@ void FileImpl::setSizeImpl(FileSizeImpl size)
257257
li.QuadPart = size;
258258
if (SetFilePointer(fh.get(), li.LowPart, &li.HighPart, FILE_BEGIN) == INVALID_SET_FILE_POINTER)
259259
handleLastErrorImpl(_path);
260-
if (SetEndOfFile(fh.get()) == 0)
260+
if (SetEndOfFile(fh.get()) == 0)
261261
handleLastErrorImpl(_path);
262262
}
263263

@@ -288,20 +288,16 @@ void FileImpl::copyToImpl(const std::string& path) const
288288
{
289289
poco_assert (!_path.empty());
290290

291-
if (CopyFileA(_path.c_str(), path.c_str(), FALSE) != 0)
292-
{
293-
FileImpl copy(path);
294-
copy.setWriteableImpl(true);
295-
}
296-
else handleLastErrorImpl(_path);
291+
if (CopyFileA(_path.c_str(), path.c_str(), FALSE) == 0)
292+
handleLastErrorImpl(_path);
297293
}
298294

299295

300296
void FileImpl::renameToImpl(const std::string& path)
301297
{
302298
poco_assert (!_path.empty());
303299

304-
if (MoveFileA(_path.c_str(), path.c_str()) == 0)
300+
if (MoveFileA(_path.c_str(), path.c_str()) == 0)
305301
handleLastErrorImpl(_path);
306302
}
307303

@@ -312,7 +308,7 @@ void FileImpl::removeImpl()
312308

313309
if (isDirectoryImpl())
314310
{
315-
if (RemoveDirectoryA(_path.c_str()) == 0)
311+
if (RemoveDirectoryA(_path.c_str()) == 0)
316312
handleLastErrorImpl(_path);
317313
}
318314
else
@@ -344,7 +340,7 @@ bool FileImpl::createFileImpl()
344340
bool FileImpl::createDirectoryImpl()
345341
{
346342
poco_assert (!_path.empty());
347-
343+
348344
if (existsImpl() && isDirectoryImpl())
349345
return false;
350346
if (CreateDirectoryA(_path.c_str(), 0) == 0)

Foundation/src/File_WIN32U.cpp

Lines changed: 15 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -35,17 +35,17 @@ class FileHandle
3535
FileImpl::handleLastErrorImpl(path);
3636
}
3737
}
38-
38+
3939
~FileHandle()
4040
{
4141
if (_h != INVALID_HANDLE_VALUE) CloseHandle(_h);
4242
}
43-
43+
4444
HANDLE get() const
4545
{
4646
return _h;
4747
}
48-
48+
4949
private:
5050
HANDLE _h;
5151
};
@@ -116,7 +116,7 @@ bool FileImpl::existsImpl() const
116116
bool FileImpl::canReadImpl() const
117117
{
118118
poco_assert (!_path.empty());
119-
119+
120120
DWORD attr = GetFileAttributesW(_upath.c_str());
121121
if (attr == INVALID_FILE_ATTRIBUTES)
122122
{
@@ -135,7 +135,7 @@ bool FileImpl::canReadImpl() const
135135
bool FileImpl::canWriteImpl() const
136136
{
137137
poco_assert (!_path.empty());
138-
138+
139139
DWORD attr = GetFileAttributesW(_upath.c_str());
140140
if (attr == INVALID_FILE_ATTRIBUTES)
141141
handleLastErrorImpl(_path);
@@ -205,7 +205,7 @@ Timestamp FileImpl::createdImpl() const
205205
poco_assert (!_path.empty());
206206

207207
WIN32_FILE_ATTRIBUTE_DATA fad;
208-
if (GetFileAttributesExW(_upath.c_str(), GetFileExInfoStandard, &fad) == 0)
208+
if (GetFileAttributesExW(_upath.c_str(), GetFileExInfoStandard, &fad) == 0)
209209
handleLastErrorImpl(_path);
210210
return Timestamp::fromFileTimeNP(fad.ftCreationTime.dwLowDateTime, fad.ftCreationTime.dwHighDateTime);
211211
}
@@ -216,7 +216,7 @@ Timestamp FileImpl::getLastModifiedImpl() const
216216
poco_assert (!_path.empty());
217217

218218
WIN32_FILE_ATTRIBUTE_DATA fad;
219-
if (GetFileAttributesExW(_upath.c_str(), GetFileExInfoStandard, &fad) == 0)
219+
if (GetFileAttributesExW(_upath.c_str(), GetFileExInfoStandard, &fad) == 0)
220220
handleLastErrorImpl(_path);
221221
return Timestamp::fromFileTimeNP(fad.ftLastWriteTime.dwLowDateTime, fad.ftLastWriteTime.dwHighDateTime);
222222
}
@@ -243,7 +243,7 @@ FileImpl::FileSizeImpl FileImpl::getSizeImpl() const
243243
poco_assert (!_path.empty());
244244

245245
WIN32_FILE_ATTRIBUTE_DATA fad;
246-
if (GetFileAttributesExW(_upath.c_str(), GetFileExInfoStandard, &fad) == 0)
246+
if (GetFileAttributesExW(_upath.c_str(), GetFileExInfoStandard, &fad) == 0)
247247
handleLastErrorImpl(_path);
248248
LARGE_INTEGER li;
249249
li.LowPart = fad.nFileSizeLow;
@@ -261,12 +261,12 @@ void FileImpl::setSizeImpl(FileSizeImpl size)
261261
li.QuadPart = size;
262262
if (SetFilePointer(fh.get(), li.LowPart, &li.HighPart, FILE_BEGIN) == INVALID_SET_FILE_POINTER)
263263
handleLastErrorImpl(_path);
264-
if (SetEndOfFile(fh.get()) == 0)
264+
if (SetEndOfFile(fh.get()) == 0)
265265
handleLastErrorImpl(_path);
266266
}
267267

268268

269-
void FileImpl::setWriteableImpl(bool flag)
269+
void FileImpl::setWriteableImpl(bool flag)
270270
{
271271
poco_assert (!_path.empty());
272272

@@ -294,12 +294,8 @@ void FileImpl::copyToImpl(const std::string& path) const
294294

295295
std::wstring upath;
296296
UnicodeConverter::toUTF16(path, upath);
297-
if (CopyFileW(_upath.c_str(), upath.c_str(), FALSE) != 0)
298-
{
299-
FileImpl copy(path);
300-
copy.setWriteableImpl(true);
301-
}
302-
else handleLastErrorImpl(_path);
297+
if (CopyFileW(_upath.c_str(), upath.c_str(), FALSE) == 0)
298+
handleLastErrorImpl(_path);
303299
}
304300

305301

@@ -309,7 +305,7 @@ void FileImpl::renameToImpl(const std::string& path)
309305

310306
std::wstring upath;
311307
UnicodeConverter::toUTF16(path, upath);
312-
if (MoveFileW(_upath.c_str(), upath.c_str()) == 0)
308+
if (MoveFileW(_upath.c_str(), upath.c_str()) == 0)
313309
handleLastErrorImpl(_path);
314310
}
315311

@@ -320,7 +316,7 @@ void FileImpl::removeImpl()
320316

321317
if (isDirectoryImpl())
322318
{
323-
if (RemoveDirectoryW(_upath.c_str()) == 0)
319+
if (RemoveDirectoryW(_upath.c_str()) == 0)
324320
handleLastErrorImpl(_path);
325321
}
326322
else
@@ -352,7 +348,7 @@ bool FileImpl::createFileImpl()
352348
bool FileImpl::createDirectoryImpl()
353349
{
354350
poco_assert (!_path.empty());
355-
351+
356352
if (existsImpl() && isDirectoryImpl())
357353
return false;
358354
if (CreateDirectoryW(_upath.c_str(), 0) == 0)

Foundation/src/File_WINCE.cpp

Lines changed: 15 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -36,17 +36,17 @@ class FileHandle
3636
FileImpl::handleLastErrorImpl(path);
3737
}
3838
}
39-
39+
4040
~FileHandle()
4141
{
4242
if (_h != INVALID_HANDLE_VALUE) CloseHandle(_h);
4343
}
44-
44+
4545
HANDLE get() const
4646
{
4747
return _h;
4848
}
49-
49+
5050
private:
5151
HANDLE _h;
5252
};
@@ -117,7 +117,7 @@ bool FileImpl::existsImpl() const
117117
bool FileImpl::canReadImpl() const
118118
{
119119
poco_assert (!_path.empty());
120-
120+
121121
DWORD attr = GetFileAttributesW(_upath.c_str());
122122
if (attr == INVALID_FILE_ATTRIBUTES)
123123
{
@@ -136,7 +136,7 @@ bool FileImpl::canReadImpl() const
136136
bool FileImpl::canWriteImpl() const
137137
{
138138
poco_assert (!_path.empty());
139-
139+
140140
DWORD attr = GetFileAttributesW(_upath.c_str());
141141
if (attr == INVALID_FILE_ATTRIBUTES)
142142
handleLastErrorImpl(_path);
@@ -196,7 +196,7 @@ Timestamp FileImpl::createdImpl() const
196196
poco_assert (!_path.empty());
197197

198198
WIN32_FILE_ATTRIBUTE_DATA fad;
199-
if (GetFileAttributesExW(_upath.c_str(), GetFileExInfoStandard, &fad) == 0)
199+
if (GetFileAttributesExW(_upath.c_str(), GetFileExInfoStandard, &fad) == 0)
200200
handleLastErrorImpl(_path);
201201
return Timestamp::fromFileTimeNP(fad.ftCreationTime.dwLowDateTime, fad.ftCreationTime.dwHighDateTime);
202202
}
@@ -207,7 +207,7 @@ Timestamp FileImpl::getLastModifiedImpl() const
207207
poco_assert (!_path.empty());
208208

209209
WIN32_FILE_ATTRIBUTE_DATA fad;
210-
if (GetFileAttributesExW(_upath.c_str(), GetFileExInfoStandard, &fad) == 0)
210+
if (GetFileAttributesExW(_upath.c_str(), GetFileExInfoStandard, &fad) == 0)
211211
handleLastErrorImpl(_path);
212212
return Timestamp::fromFileTimeNP(fad.ftLastWriteTime.dwLowDateTime, fad.ftLastWriteTime.dwHighDateTime);
213213
}
@@ -234,7 +234,7 @@ FileImpl::FileSizeImpl FileImpl::getSizeImpl() const
234234
poco_assert (!_path.empty());
235235

236236
WIN32_FILE_ATTRIBUTE_DATA fad;
237-
if (GetFileAttributesExW(_upath.c_str(), GetFileExInfoStandard, &fad) == 0)
237+
if (GetFileAttributesExW(_upath.c_str(), GetFileExInfoStandard, &fad) == 0)
238238
handleLastErrorImpl(_path);
239239
LARGE_INTEGER li;
240240
li.LowPart = fad.nFileSizeLow;
@@ -252,12 +252,12 @@ void FileImpl::setSizeImpl(FileSizeImpl size)
252252
li.QuadPart = size;
253253
if (SetFilePointer(fh.get(), li.LowPart, &li.HighPart, FILE_BEGIN) == INVALID_SET_FILE_POINTER)
254254
handleLastErrorImpl(_path);
255-
if (SetEndOfFile(fh.get()) == 0)
255+
if (SetEndOfFile(fh.get()) == 0)
256256
handleLastErrorImpl(_path);
257257
}
258258

259259

260-
void FileImpl::setWriteableImpl(bool flag)
260+
void FileImpl::setWriteableImpl(bool flag)
261261
{
262262
poco_assert (!_path.empty());
263263

@@ -285,12 +285,8 @@ void FileImpl::copyToImpl(const std::string& path) const
285285

286286
std::wstring upath;
287287
UnicodeConverter::toUTF16(path, upath);
288-
if (CopyFileW(_upath.c_str(), upath.c_str(), FALSE) != 0)
289-
{
290-
FileImpl copy(path);
291-
copy.setWriteableImpl(true);
292-
}
293-
else handleLastErrorImpl(_path);
288+
if (CopyFileW(_upath.c_str(), upath.c_str(), FALSE) == 0)
289+
handleLastErrorImpl(_path);
294290
}
295291

296292

@@ -300,7 +296,7 @@ void FileImpl::renameToImpl(const std::string& path)
300296

301297
std::wstring upath;
302298
UnicodeConverter::toUTF16(path, upath);
303-
if (MoveFileW(_upath.c_str(), upath.c_str()) == 0)
299+
if (MoveFileW(_upath.c_str(), upath.c_str()) == 0)
304300
handleLastErrorImpl(_path);
305301
}
306302

@@ -311,7 +307,7 @@ void FileImpl::removeImpl()
311307

312308
if (isDirectoryImpl())
313309
{
314-
if (RemoveDirectoryW(_upath.c_str()) == 0)
310+
if (RemoveDirectoryW(_upath.c_str()) == 0)
315311
handleLastErrorImpl(_path);
316312
}
317313
else
@@ -343,7 +339,7 @@ bool FileImpl::createFileImpl()
343339
bool FileImpl::createDirectoryImpl()
344340
{
345341
poco_assert (!_path.empty());
346-
342+
347343
if (existsImpl() && isDirectoryImpl())
348344
return false;
349345
if (CreateDirectoryW(_upath.c_str(), 0) == 0)

0 commit comments

Comments
 (0)