File_WINCE.cpp 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403
  1. //
  2. // File_WIN32U.cpp
  3. //
  4. // $Id: //poco/1.4/Foundation/src/File_WINCE.cpp#1 $
  5. //
  6. // Library: Foundation
  7. // Package: Filesystem
  8. // Module: File
  9. //
  10. // Copyright (c) 2006-2010, Applied Informatics Software Engineering GmbH.
  11. // and Contributors.
  12. //
  13. // SPDX-License-Identifier: BSL-1.0
  14. //
  15. #include "Poco/File_WINCE.h"
  16. #include "Poco/Exception.h"
  17. #include "Poco/String.h"
  18. #include "Poco/UnicodeConverter.h"
  19. #include "Poco/Path.h"
  20. #include "Poco/UnWindows.h"
  21. namespace Poco {
  22. class FileHandle
  23. {
  24. public:
  25. FileHandle(const std::string& path, const std::wstring& upath, DWORD access, DWORD share, DWORD disp)
  26. {
  27. _h = CreateFileW(upath.c_str(), access, share, 0, disp, 0, 0);
  28. if (_h == INVALID_HANDLE_VALUE)
  29. {
  30. FileImpl::handleLastErrorImpl(path);
  31. }
  32. }
  33. ~FileHandle()
  34. {
  35. if (_h != INVALID_HANDLE_VALUE) CloseHandle(_h);
  36. }
  37. HANDLE get() const
  38. {
  39. return _h;
  40. }
  41. private:
  42. HANDLE _h;
  43. };
  44. FileImpl::FileImpl()
  45. {
  46. }
  47. FileImpl::FileImpl(const std::string& path): _path(path)
  48. {
  49. std::string::size_type n = _path.size();
  50. if (n > 1 && (_path[n - 1] == '\\' || _path[n - 1] == '/') && !((n == 3 && _path[1]==':')))
  51. {
  52. _path.resize(n - 1);
  53. }
  54. UnicodeConverter::toUTF16(_path, _upath);
  55. }
  56. FileImpl::~FileImpl()
  57. {
  58. }
  59. void FileImpl::swapImpl(FileImpl& file)
  60. {
  61. std::swap(_path, file._path);
  62. std::swap(_upath, file._upath);
  63. }
  64. void FileImpl::setPathImpl(const std::string& path)
  65. {
  66. _path = path;
  67. std::string::size_type n = _path.size();
  68. if (n > 1 && (_path[n - 1] == '\\' || _path[n - 1] == '/') && !((n == 3 && _path[1]==':')))
  69. {
  70. _path.resize(n - 1);
  71. }
  72. UnicodeConverter::toUTF16(_path, _upath);
  73. }
  74. bool FileImpl::existsImpl() const
  75. {
  76. poco_assert (!_path.empty());
  77. DWORD attr = GetFileAttributesW(_upath.c_str());
  78. if (attr == 0xFFFFFFFF)
  79. {
  80. switch (GetLastError())
  81. {
  82. case ERROR_FILE_NOT_FOUND:
  83. case ERROR_PATH_NOT_FOUND:
  84. case ERROR_NOT_READY:
  85. case ERROR_INVALID_DRIVE:
  86. return false;
  87. default:
  88. handleLastErrorImpl(_path);
  89. }
  90. }
  91. return true;
  92. }
  93. bool FileImpl::canReadImpl() const
  94. {
  95. poco_assert (!_path.empty());
  96. DWORD attr = GetFileAttributesW(_upath.c_str());
  97. if (attr == 0xFFFFFFFF)
  98. {
  99. switch (GetLastError())
  100. {
  101. case ERROR_ACCESS_DENIED:
  102. return false;
  103. default:
  104. handleLastErrorImpl(_path);
  105. }
  106. }
  107. return true;
  108. }
  109. bool FileImpl::canWriteImpl() const
  110. {
  111. poco_assert (!_path.empty());
  112. DWORD attr = GetFileAttributesW(_upath.c_str());
  113. if (attr == 0xFFFFFFFF)
  114. handleLastErrorImpl(_path);
  115. return (attr & FILE_ATTRIBUTE_READONLY) == 0;
  116. }
  117. bool FileImpl::canExecuteImpl() const
  118. {
  119. Path p(_path);
  120. return icompare(p.getExtension(), "exe") == 0;
  121. }
  122. bool FileImpl::isFileImpl() const
  123. {
  124. return !isDirectoryImpl() && !isDeviceImpl();
  125. }
  126. bool FileImpl::isDirectoryImpl() const
  127. {
  128. poco_assert (!_path.empty());
  129. DWORD attr = GetFileAttributesW(_upath.c_str());
  130. if (attr == 0xFFFFFFFF)
  131. handleLastErrorImpl(_path);
  132. return (attr & FILE_ATTRIBUTE_DIRECTORY) != 0;
  133. }
  134. bool FileImpl::isLinkImpl() const
  135. {
  136. return false;
  137. }
  138. bool FileImpl::isDeviceImpl() const
  139. {
  140. return false;
  141. }
  142. bool FileImpl::isHiddenImpl() const
  143. {
  144. poco_assert (!_path.empty());
  145. DWORD attr = GetFileAttributesW(_upath.c_str());
  146. if (attr == 0xFFFFFFFF)
  147. handleLastErrorImpl(_path);
  148. return (attr & FILE_ATTRIBUTE_HIDDEN) != 0;
  149. }
  150. Timestamp FileImpl::createdImpl() const
  151. {
  152. poco_assert (!_path.empty());
  153. WIN32_FILE_ATTRIBUTE_DATA fad;
  154. if (GetFileAttributesExW(_upath.c_str(), GetFileExInfoStandard, &fad) == 0)
  155. handleLastErrorImpl(_path);
  156. return Timestamp::fromFileTimeNP(fad.ftCreationTime.dwLowDateTime, fad.ftCreationTime.dwHighDateTime);
  157. }
  158. Timestamp FileImpl::getLastModifiedImpl() const
  159. {
  160. poco_assert (!_path.empty());
  161. WIN32_FILE_ATTRIBUTE_DATA fad;
  162. if (GetFileAttributesExW(_upath.c_str(), GetFileExInfoStandard, &fad) == 0)
  163. handleLastErrorImpl(_path);
  164. return Timestamp::fromFileTimeNP(fad.ftLastWriteTime.dwLowDateTime, fad.ftLastWriteTime.dwHighDateTime);
  165. }
  166. void FileImpl::setLastModifiedImpl(const Timestamp& ts)
  167. {
  168. poco_assert (!_path.empty());
  169. UInt32 low;
  170. UInt32 high;
  171. ts.toFileTimeNP(low, high);
  172. FILETIME ft;
  173. ft.dwLowDateTime = low;
  174. ft.dwHighDateTime = high;
  175. FileHandle fh(_path, _upath, GENERIC_WRITE, FILE_SHARE_WRITE, OPEN_EXISTING);
  176. if (SetFileTime(fh.get(), 0, &ft, &ft) == 0)
  177. handleLastErrorImpl(_path);
  178. }
  179. FileImpl::FileSizeImpl FileImpl::getSizeImpl() const
  180. {
  181. poco_assert (!_path.empty());
  182. WIN32_FILE_ATTRIBUTE_DATA fad;
  183. if (GetFileAttributesExW(_upath.c_str(), GetFileExInfoStandard, &fad) == 0)
  184. handleLastErrorImpl(_path);
  185. LARGE_INTEGER li;
  186. li.LowPart = fad.nFileSizeLow;
  187. li.HighPart = fad.nFileSizeHigh;
  188. return li.QuadPart;
  189. }
  190. void FileImpl::setSizeImpl(FileSizeImpl size)
  191. {
  192. poco_assert (!_path.empty());
  193. FileHandle fh(_path, _upath, GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, OPEN_EXISTING);
  194. LARGE_INTEGER li;
  195. li.QuadPart = size;
  196. if (SetFilePointer(fh.get(), li.LowPart, &li.HighPart, FILE_BEGIN) == -1)
  197. handleLastErrorImpl(_path);
  198. if (SetEndOfFile(fh.get()) == 0)
  199. handleLastErrorImpl(_path);
  200. }
  201. void FileImpl::setWriteableImpl(bool flag)
  202. {
  203. poco_assert (!_path.empty());
  204. DWORD attr = GetFileAttributesW(_upath.c_str());
  205. if (attr == -1)
  206. handleLastErrorImpl(_path);
  207. if (flag)
  208. attr &= ~FILE_ATTRIBUTE_READONLY;
  209. else
  210. attr |= FILE_ATTRIBUTE_READONLY;
  211. if (SetFileAttributesW(_upath.c_str(), attr) == 0)
  212. handleLastErrorImpl(_path);
  213. }
  214. void FileImpl::setExecutableImpl(bool flag)
  215. {
  216. // not supported
  217. }
  218. void FileImpl::copyToImpl(const std::string& path) const
  219. {
  220. poco_assert (!_path.empty());
  221. std::wstring upath;
  222. UnicodeConverter::toUTF16(path, upath);
  223. if (CopyFileW(_upath.c_str(), upath.c_str(), FALSE) != 0)
  224. {
  225. FileImpl copy(path);
  226. copy.setWriteableImpl(true);
  227. }
  228. else handleLastErrorImpl(_path);
  229. }
  230. void FileImpl::renameToImpl(const std::string& path)
  231. {
  232. poco_assert (!_path.empty());
  233. std::wstring upath;
  234. UnicodeConverter::toUTF16(path, upath);
  235. if (MoveFileW(_upath.c_str(), upath.c_str()) == 0)
  236. handleLastErrorImpl(_path);
  237. }
  238. void FileImpl::removeImpl()
  239. {
  240. poco_assert (!_path.empty());
  241. if (isDirectoryImpl())
  242. {
  243. if (RemoveDirectoryW(_upath.c_str()) == 0)
  244. handleLastErrorImpl(_path);
  245. }
  246. else
  247. {
  248. if (DeleteFileW(_upath.c_str()) == 0)
  249. handleLastErrorImpl(_path);
  250. }
  251. }
  252. bool FileImpl::createFileImpl()
  253. {
  254. poco_assert (!_path.empty());
  255. HANDLE hFile = CreateFileW(_upath.c_str(), GENERIC_WRITE, 0, 0, CREATE_NEW, 0, 0);
  256. if (hFile != INVALID_HANDLE_VALUE)
  257. {
  258. CloseHandle(hFile);
  259. return true;
  260. }
  261. else if (GetLastError() == ERROR_FILE_EXISTS)
  262. return false;
  263. else
  264. handleLastErrorImpl(_path);
  265. return false;
  266. }
  267. bool FileImpl::createDirectoryImpl()
  268. {
  269. poco_assert (!_path.empty());
  270. if (existsImpl() && isDirectoryImpl())
  271. return false;
  272. if (CreateDirectoryW(_upath.c_str(), 0) == 0)
  273. handleLastErrorImpl(_path);
  274. return true;
  275. }
  276. void FileImpl::handleLastErrorImpl(const std::string& path)
  277. {
  278. switch (GetLastError())
  279. {
  280. case ERROR_FILE_NOT_FOUND:
  281. throw FileNotFoundException(path);
  282. case ERROR_PATH_NOT_FOUND:
  283. case ERROR_BAD_NETPATH:
  284. case ERROR_CANT_RESOLVE_FILENAME:
  285. case ERROR_INVALID_DRIVE:
  286. throw PathNotFoundException(path);
  287. case ERROR_ACCESS_DENIED:
  288. throw FileAccessDeniedException(path);
  289. case ERROR_ALREADY_EXISTS:
  290. case ERROR_FILE_EXISTS:
  291. throw FileExistsException(path);
  292. case ERROR_INVALID_NAME:
  293. case ERROR_DIRECTORY:
  294. case ERROR_FILENAME_EXCED_RANGE:
  295. case ERROR_BAD_PATHNAME:
  296. throw PathSyntaxException(path);
  297. case ERROR_FILE_READ_ONLY:
  298. throw FileReadOnlyException(path);
  299. case ERROR_CANNOT_MAKE:
  300. throw CreateFileException(path);
  301. case ERROR_DIR_NOT_EMPTY:
  302. throw FileException("directory not empty", path);
  303. case ERROR_WRITE_FAULT:
  304. throw WriteFileException(path);
  305. case ERROR_READ_FAULT:
  306. throw ReadFileException(path);
  307. case ERROR_SHARING_VIOLATION:
  308. throw FileException("sharing violation", path);
  309. case ERROR_LOCK_VIOLATION:
  310. throw FileException("lock violation", path);
  311. case ERROR_HANDLE_EOF:
  312. throw ReadFileException("EOF reached", path);
  313. case ERROR_HANDLE_DISK_FULL:
  314. case ERROR_DISK_FULL:
  315. throw WriteFileException("disk is full", path);
  316. case ERROR_NEGATIVE_SEEK:
  317. throw FileException("negative seek", path);
  318. default:
  319. throw FileException(path);
  320. }
  321. }
  322. } // namespace Poco