2006-10-23 17:48:43 +02:00
|
|
|
//
|
|
|
|
// File_WIN32.cpp
|
|
|
|
//
|
2007-05-07 18:46:23 +02:00
|
|
|
// $Id: //poco/Main/Foundation/src/File_WIN32.cpp#20 $
|
2006-10-23 17:48:43 +02:00
|
|
|
//
|
|
|
|
// Library: Foundation
|
|
|
|
// Package: Filesystem
|
|
|
|
// Module: File
|
|
|
|
//
|
|
|
|
// Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH.
|
|
|
|
// and Contributors.
|
|
|
|
//
|
|
|
|
// Permission is hereby granted, free of charge, to any person or organization
|
|
|
|
// obtaining a copy of the software and accompanying documentation covered by
|
|
|
|
// this license (the "Software") to use, reproduce, display, distribute,
|
|
|
|
// execute, and transmit the Software, and to prepare derivative works of the
|
|
|
|
// Software, and to permit third-parties to whom the Software is furnished to
|
|
|
|
// do so, all subject to the following:
|
|
|
|
//
|
|
|
|
// The copyright notices in the Software and this entire statement, including
|
|
|
|
// the above license grant, this restriction and the following disclaimer,
|
|
|
|
// must be included in all copies of the Software, in whole or in part, and
|
|
|
|
// all derivative works of the Software, unless such copies or derivative
|
|
|
|
// works are solely in the form of machine-executable object code generated by
|
|
|
|
// a source language processor.
|
|
|
|
//
|
|
|
|
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
// FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
|
|
|
|
// SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
|
|
|
|
// FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
|
|
|
|
// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
|
|
|
// DEALINGS IN THE SOFTWARE.
|
|
|
|
//
|
|
|
|
|
|
|
|
|
|
|
|
#include "Poco/File_WIN32.h"
|
|
|
|
#include "Poco/Exception.h"
|
2007-04-18 18:22:57 +02:00
|
|
|
#include "Poco/String.h"
|
2007-05-05 15:15:55 +02:00
|
|
|
#include "Poco/UnWindows.h"
|
2006-10-23 17:48:43 +02:00
|
|
|
|
|
|
|
|
|
|
|
namespace Poco {
|
|
|
|
|
|
|
|
|
|
|
|
class FileHandle
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
FileHandle(const std::string& path, DWORD access, DWORD share, DWORD disp)
|
|
|
|
{
|
2007-05-07 18:46:23 +02:00
|
|
|
_h = CreateFileA(path.c_str(), access, share, 0, disp, 0, 0);
|
2007-04-18 18:22:57 +02:00
|
|
|
if (!_h) FileImpl::handleLastErrorImpl(path);
|
2006-10-23 17:48:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
~FileHandle()
|
|
|
|
{
|
|
|
|
if (_h) CloseHandle(_h);
|
|
|
|
}
|
|
|
|
|
|
|
|
HANDLE get() const
|
|
|
|
{
|
|
|
|
return _h;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
HANDLE _h;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
FileImpl::FileImpl()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
FileImpl::FileImpl(const std::string& path): _path(path)
|
|
|
|
{
|
|
|
|
std::string::size_type n = _path.size();
|
2006-12-22 10:31:08 +01:00
|
|
|
if (n > 1 && (_path[n - 1] == '\\' || _path[n - 1] == '/') && !((n == 3 && _path[1]==':')))
|
|
|
|
{
|
2006-10-23 17:48:43 +02:00
|
|
|
_path.resize(n - 1);
|
2006-12-22 10:31:08 +01:00
|
|
|
}
|
2006-10-23 17:48:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
FileImpl::~FileImpl()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void FileImpl::swapImpl(FileImpl& file)
|
|
|
|
{
|
|
|
|
std::swap(_path, file._path);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void FileImpl::setPathImpl(const std::string& path)
|
|
|
|
{
|
|
|
|
_path = path;
|
2006-12-22 10:31:08 +01:00
|
|
|
std::string::size_type n = _path.size();
|
|
|
|
if (n > 1 && (_path[n - 1] == '\\' || _path[n - 1] == '/') && !((n == 3 && _path[1]==':')))
|
|
|
|
{
|
|
|
|
_path.resize(n - 1);
|
|
|
|
}
|
2006-10-23 17:48:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool FileImpl::existsImpl() const
|
|
|
|
{
|
|
|
|
poco_assert (!_path.empty());
|
|
|
|
|
|
|
|
DWORD attr = GetFileAttributes(_path.c_str());
|
|
|
|
if (attr == 0xFFFFFFFF)
|
|
|
|
{
|
|
|
|
switch (GetLastError())
|
|
|
|
{
|
|
|
|
case ERROR_FILE_NOT_FOUND:
|
|
|
|
case ERROR_PATH_NOT_FOUND:
|
|
|
|
case ERROR_NOT_READY:
|
|
|
|
case ERROR_INVALID_DRIVE:
|
|
|
|
return false;
|
|
|
|
default:
|
2007-04-18 18:22:57 +02:00
|
|
|
handleLastErrorImpl(_path);
|
2006-10-23 17:48:43 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool FileImpl::canReadImpl() const
|
|
|
|
{
|
|
|
|
poco_assert (!_path.empty());
|
|
|
|
|
|
|
|
DWORD attr = GetFileAttributes(_path.c_str());
|
|
|
|
if (attr == 0xFFFFFFFF)
|
|
|
|
{
|
|
|
|
switch (GetLastError())
|
|
|
|
{
|
|
|
|
case ERROR_ACCESS_DENIED:
|
|
|
|
return false;
|
|
|
|
default:
|
2007-04-18 18:22:57 +02:00
|
|
|
handleLastErrorImpl(_path);
|
2006-10-23 17:48:43 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool FileImpl::canWriteImpl() const
|
|
|
|
{
|
|
|
|
poco_assert (!_path.empty());
|
|
|
|
|
|
|
|
DWORD attr = GetFileAttributes(_path.c_str());
|
|
|
|
if (attr == 0xFFFFFFFF)
|
2007-04-18 18:22:57 +02:00
|
|
|
handleLastErrorImpl(_path);
|
2006-10-23 17:48:43 +02:00
|
|
|
return (attr & FILE_ATTRIBUTE_READONLY) == 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-04-18 18:22:57 +02:00
|
|
|
bool FileImpl::canExecuteImpl() const
|
|
|
|
{
|
|
|
|
Path p(_path);
|
|
|
|
return icompare(p.getExtension(), "exe") == 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-10-23 17:48:43 +02:00
|
|
|
bool FileImpl::isFileImpl() const
|
|
|
|
{
|
|
|
|
poco_assert (!_path.empty());
|
|
|
|
|
|
|
|
DWORD attr = GetFileAttributes(_path.c_str());
|
|
|
|
if (attr == 0xFFFFFFFF)
|
2007-04-18 18:22:57 +02:00
|
|
|
handleLastErrorImpl(_path);
|
2006-10-23 17:48:43 +02:00
|
|
|
return (attr & FILE_ATTRIBUTE_DIRECTORY) == 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool FileImpl::isDirectoryImpl() const
|
|
|
|
{
|
|
|
|
poco_assert (!_path.empty());
|
|
|
|
|
|
|
|
DWORD attr = GetFileAttributes(_path.c_str());
|
|
|
|
if (attr == 0xFFFFFFFF)
|
2007-04-18 18:22:57 +02:00
|
|
|
handleLastErrorImpl(_path);
|
2006-10-23 17:48:43 +02:00
|
|
|
return (attr & FILE_ATTRIBUTE_DIRECTORY) != 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool FileImpl::isLinkImpl() const
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Timestamp FileImpl::createdImpl() const
|
|
|
|
{
|
|
|
|
poco_assert (!_path.empty());
|
|
|
|
|
|
|
|
WIN32_FILE_ATTRIBUTE_DATA fad;
|
|
|
|
if (GetFileAttributesEx(_path.c_str(), GetFileExInfoStandard, &fad) == 0)
|
2007-04-18 18:22:57 +02:00
|
|
|
handleLastErrorImpl(_path);
|
2006-10-23 17:48:43 +02:00
|
|
|
return Timestamp::fromFileTimeNP(fad.ftCreationTime.dwLowDateTime, fad.ftCreationTime.dwHighDateTime);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Timestamp FileImpl::getLastModifiedImpl() const
|
|
|
|
{
|
|
|
|
poco_assert (!_path.empty());
|
|
|
|
|
|
|
|
WIN32_FILE_ATTRIBUTE_DATA fad;
|
|
|
|
if (GetFileAttributesEx(_path.c_str(), GetFileExInfoStandard, &fad) == 0)
|
2007-04-18 18:22:57 +02:00
|
|
|
handleLastErrorImpl(_path);
|
2006-10-23 17:48:43 +02:00
|
|
|
return Timestamp::fromFileTimeNP(fad.ftLastWriteTime.dwLowDateTime, fad.ftLastWriteTime.dwHighDateTime);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void FileImpl::setLastModifiedImpl(const Timestamp& ts)
|
|
|
|
{
|
|
|
|
poco_assert (!_path.empty());
|
|
|
|
|
|
|
|
UInt32 low;
|
|
|
|
UInt32 high;
|
|
|
|
ts.toFileTimeNP(low, high);
|
|
|
|
FILETIME ft;
|
|
|
|
ft.dwLowDateTime = low;
|
|
|
|
ft.dwHighDateTime = high;
|
|
|
|
FileHandle fh(_path, FILE_ALL_ACCESS, FILE_SHARE_WRITE, OPEN_EXISTING);
|
|
|
|
if (SetFileTime(fh.get(), 0, &ft, &ft) == 0)
|
2007-04-18 18:22:57 +02:00
|
|
|
handleLastErrorImpl(_path);
|
2006-10-23 17:48:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
FileImpl::FileSizeImpl FileImpl::getSizeImpl() const
|
|
|
|
{
|
|
|
|
poco_assert (!_path.empty());
|
|
|
|
|
|
|
|
WIN32_FILE_ATTRIBUTE_DATA fad;
|
|
|
|
if (GetFileAttributesEx(_path.c_str(), GetFileExInfoStandard, &fad) == 0)
|
2007-04-18 18:22:57 +02:00
|
|
|
handleLastErrorImpl(_path);
|
2006-10-23 17:48:43 +02:00
|
|
|
LARGE_INTEGER li;
|
|
|
|
li.LowPart = fad.nFileSizeLow;
|
|
|
|
li.HighPart = fad.nFileSizeHigh;
|
|
|
|
return li.QuadPart;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void FileImpl::setSizeImpl(FileSizeImpl size)
|
|
|
|
{
|
|
|
|
poco_assert (!_path.empty());
|
|
|
|
|
|
|
|
FileHandle fh(_path, GENERIC_WRITE, FILE_SHARE_WRITE, OPEN_EXISTING);
|
|
|
|
LARGE_INTEGER li;
|
|
|
|
li.QuadPart = size;
|
|
|
|
if (SetFilePointer(fh.get(), li.LowPart, &li.HighPart, FILE_BEGIN) == -1)
|
2007-04-18 18:22:57 +02:00
|
|
|
handleLastErrorImpl(_path);
|
2006-10-23 17:48:43 +02:00
|
|
|
if (SetEndOfFile(fh.get()) == 0)
|
2007-04-18 18:22:57 +02:00
|
|
|
handleLastErrorImpl(_path);
|
2006-10-23 17:48:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-12-22 10:31:08 +01:00
|
|
|
void FileImpl::setWriteableImpl(bool flag)
|
2006-10-23 17:48:43 +02:00
|
|
|
{
|
|
|
|
poco_assert (!_path.empty());
|
|
|
|
|
|
|
|
DWORD attr = GetFileAttributes(_path.c_str());
|
|
|
|
if (attr == -1)
|
2007-04-18 18:22:57 +02:00
|
|
|
handleLastErrorImpl(_path);
|
2006-10-23 17:48:43 +02:00
|
|
|
if (flag)
|
|
|
|
attr &= ~FILE_ATTRIBUTE_READONLY;
|
|
|
|
else
|
|
|
|
attr |= FILE_ATTRIBUTE_READONLY;
|
|
|
|
if (SetFileAttributes(_path.c_str(), attr) == 0)
|
2007-04-18 18:22:57 +02:00
|
|
|
handleLastErrorImpl(_path);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void FileImpl::setExecutableImpl(bool flag)
|
|
|
|
{
|
|
|
|
// not supported
|
2006-10-23 17:48:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void FileImpl::copyToImpl(const std::string& path) const
|
|
|
|
{
|
|
|
|
poco_assert (!_path.empty());
|
|
|
|
|
2007-05-07 18:46:23 +02:00
|
|
|
if (CopyFileA(_path.c_str(), path.c_str(), FALSE) != 0)
|
|
|
|
{
|
|
|
|
FileImpl copy(path);
|
|
|
|
copy.setWriteableImpl(true);
|
|
|
|
}
|
|
|
|
else handleLastErrorImpl(_path);
|
2006-10-23 17:48:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void FileImpl::renameToImpl(const std::string& path)
|
|
|
|
{
|
|
|
|
poco_assert (!_path.empty());
|
|
|
|
|
2007-05-07 18:46:23 +02:00
|
|
|
if (MoveFileA(_path.c_str(), path.c_str()) == 0)
|
2007-04-18 18:22:57 +02:00
|
|
|
handleLastErrorImpl(_path);
|
2006-10-23 17:48:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void FileImpl::removeImpl()
|
|
|
|
{
|
|
|
|
poco_assert (!_path.empty());
|
|
|
|
|
|
|
|
if (isDirectoryImpl())
|
|
|
|
{
|
2007-05-07 18:46:23 +02:00
|
|
|
if (RemoveDirectoryA(_path.c_str()) == 0)
|
2007-04-18 18:22:57 +02:00
|
|
|
handleLastErrorImpl(_path);
|
2006-10-23 17:48:43 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2007-05-07 18:46:23 +02:00
|
|
|
if (DeleteFileA(_path.c_str()) == 0)
|
2007-04-18 18:22:57 +02:00
|
|
|
handleLastErrorImpl(_path);
|
2006-10-23 17:48:43 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool FileImpl::createFileImpl()
|
|
|
|
{
|
|
|
|
poco_assert (!_path.empty());
|
|
|
|
|
2007-05-07 18:46:23 +02:00
|
|
|
HANDLE hFile = CreateFileA(_path.c_str(), GENERIC_WRITE, 0, 0, CREATE_NEW, 0, 0);
|
2007-04-25 10:39:02 +02:00
|
|
|
if (hFile != INVALID_HANDLE_VALUE)
|
2006-10-23 17:48:43 +02:00
|
|
|
{
|
|
|
|
CloseHandle(hFile);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else if (GetLastError() == ERROR_ALREADY_EXISTS)
|
|
|
|
return false;
|
|
|
|
else
|
2007-04-18 18:22:57 +02:00
|
|
|
handleLastErrorImpl(_path);
|
2006-10-23 17:48:43 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool FileImpl::createDirectoryImpl()
|
|
|
|
{
|
|
|
|
poco_assert (!_path.empty());
|
|
|
|
|
|
|
|
if (existsImpl() && isDirectoryImpl())
|
|
|
|
return false;
|
2007-05-07 18:46:23 +02:00
|
|
|
if (CreateDirectoryA(_path.c_str(), 0) == 0)
|
2007-04-18 18:22:57 +02:00
|
|
|
handleLastErrorImpl(_path);
|
2006-10-23 17:48:43 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-04-18 18:22:57 +02:00
|
|
|
void FileImpl::handleLastErrorImpl(const std::string& path)
|
2006-10-23 17:48:43 +02:00
|
|
|
{
|
|
|
|
switch (GetLastError())
|
|
|
|
{
|
|
|
|
case ERROR_FILE_NOT_FOUND:
|
|
|
|
throw FileNotFoundException(path);
|
|
|
|
case ERROR_PATH_NOT_FOUND:
|
|
|
|
case ERROR_BAD_NETPATH:
|
|
|
|
case ERROR_CANT_RESOLVE_FILENAME:
|
|
|
|
case ERROR_INVALID_DRIVE:
|
|
|
|
throw PathNotFoundException(path);
|
|
|
|
case ERROR_ACCESS_DENIED:
|
|
|
|
throw FileAccessDeniedException(path);
|
|
|
|
case ERROR_ALREADY_EXISTS:
|
|
|
|
case ERROR_FILE_EXISTS:
|
|
|
|
throw FileExistsException(path);
|
|
|
|
case ERROR_INVALID_NAME:
|
|
|
|
case ERROR_DIRECTORY:
|
|
|
|
case ERROR_FILENAME_EXCED_RANGE:
|
|
|
|
case ERROR_BAD_PATHNAME:
|
|
|
|
throw PathSyntaxException(path);
|
|
|
|
case ERROR_FILE_READ_ONLY:
|
|
|
|
throw FileReadOnlyException(path);
|
|
|
|
case ERROR_CANNOT_MAKE:
|
|
|
|
throw CreateFileException(path);
|
|
|
|
case ERROR_DIR_NOT_EMPTY:
|
|
|
|
throw FileException("directory not empty", path);
|
2007-04-18 18:22:57 +02:00
|
|
|
case ERROR_WRITE_FAULT:
|
|
|
|
throw WriteFileException(path);
|
|
|
|
case ERROR_READ_FAULT:
|
|
|
|
throw ReadFileException(path);
|
|
|
|
case ERROR_SHARING_VIOLATION:
|
|
|
|
throw FileException("sharing violation", path);
|
|
|
|
case ERROR_LOCK_VIOLATION:
|
|
|
|
throw FileException("lock violation", path);
|
|
|
|
case ERROR_HANDLE_EOF:
|
|
|
|
throw ReadFileException("EOF reached", path);
|
|
|
|
case ERROR_HANDLE_DISK_FULL:
|
|
|
|
case ERROR_DISK_FULL:
|
|
|
|
throw WriteFileException("disk is full", path);
|
|
|
|
case ERROR_NEGATIVE_SEEK:
|
|
|
|
throw FileException("negative seek", path);
|
2006-10-23 17:48:43 +02:00
|
|
|
default:
|
|
|
|
throw FileException(path);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
} // namespace Poco
|