File.cc 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. /* <!-- copyright */
  2. /*
  3. * aria2 - The high speed download utility
  4. *
  5. * Copyright (C) 2006 Tatsuhiro Tsujikawa
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. *
  21. * In addition, as a special exception, the copyright holders give
  22. * permission to link the code of portions of this program with the
  23. * OpenSSL library under certain conditions as described in each
  24. * individual source file, and distribute linked combinations
  25. * including the two.
  26. * You must obey the GNU General Public License in all respects
  27. * for all of the code used other than OpenSSL. If you modify
  28. * file(s) with this exception, you may extend this exception to your
  29. * version of the file(s), but you are not obligated to do so. If you
  30. * do not wish to do so, delete this exception statement from your
  31. * version. If you delete this exception statement from all source
  32. * files in the program, then also delete it here.
  33. */
  34. /* copyright --> */
  35. #include "File.h"
  36. #include <stdlib.h>
  37. #include <sys/types.h>
  38. #ifdef HAVE_UTIME_H
  39. # include <utime.h>
  40. #endif // HAVE_UTIME_H
  41. #include <unistd.h>
  42. #include <vector>
  43. #include <cstring>
  44. #include <cstdio>
  45. #include "util.h"
  46. #include "A2STR.h"
  47. #include "array_fun.h"
  48. #include "Logger.h"
  49. #include "LogFactory.h"
  50. #include "fmt.h"
  51. namespace aria2 {
  52. File::File(const std::string& name) : name_(name) {}
  53. File::File(const File& c) : name_(c.name_) {}
  54. File::~File() {}
  55. File& File::operator=(const File& c)
  56. {
  57. if(this != &c) {
  58. name_ = c.name_;
  59. }
  60. return *this;
  61. }
  62. int File::fillStat(a2_struct_stat& fstat) {
  63. return a2stat(utf8ToWChar(name_).c_str(), &fstat);
  64. }
  65. bool File::exists() {
  66. a2_struct_stat fstat;
  67. return fillStat(fstat) == 0;
  68. }
  69. bool File::isFile() {
  70. a2_struct_stat fstat;
  71. if(fillStat(fstat) < 0) {
  72. return false;
  73. }
  74. return S_ISREG(fstat.st_mode) == 1;
  75. }
  76. bool File::isDir() {
  77. a2_struct_stat fstat;
  78. if(fillStat(fstat) < 0) {
  79. return false;
  80. }
  81. return S_ISDIR(fstat.st_mode) == 1;
  82. }
  83. bool File::remove() {
  84. if(isFile()) {
  85. return a2unlink(utf8ToWChar(name_).c_str()) == 0;
  86. } else if(isDir()) {
  87. return a2rmdir(utf8ToWChar(name_).c_str()) == 0;
  88. } else {
  89. return false;
  90. }
  91. }
  92. int64_t File::size() {
  93. a2_struct_stat fstat;
  94. if(fillStat(fstat) < 0) {
  95. return 0;
  96. }
  97. return fstat.st_size;
  98. }
  99. bool File::mkdirs() {
  100. if(isDir()) {
  101. return false;
  102. }
  103. #ifdef __MINGW32__
  104. std::string path = name_;
  105. for(std::string::iterator i = path.begin(), eoi = path.end(); i != eoi; ++i) {
  106. if(*i == '\\') {
  107. *i = '/';
  108. }
  109. }
  110. std::string::iterator dbegin;
  111. if(util::startsWith(path, "//")) {
  112. // UNC path
  113. std::string::size_type hostEnd = path.find('/', 2);
  114. if(hostEnd == std::string::npos) {
  115. // UNC path with only hostname considered as an error.
  116. return false;
  117. } else if(hostEnd == 2) {
  118. // If path starts with "///", it is not considered as UNC.
  119. dbegin = path.begin();
  120. } else {
  121. std::string::iterator i = path.begin()+hostEnd;
  122. std::string::iterator eoi = path.end();
  123. // //host/mount/dir/...
  124. // | |
  125. // i (at this point)
  126. // |
  127. // dbegin (will be)
  128. // Skip to after first directory part. This is because
  129. // //host/dir appears to be non-directory and mkdir it fails.
  130. for(; i != eoi && *i == '/'; ++i);
  131. for(; i != eoi && *i != '/'; ++i);
  132. dbegin = i;
  133. A2_LOG_DEBUG(fmt("UNC Prefix %s",
  134. std::string(path.begin(), dbegin).c_str()));
  135. }
  136. } else {
  137. dbegin = path.begin();
  138. }
  139. std::string::iterator begin = path.begin();
  140. std::string::iterator end = path.end();
  141. for(std::string::iterator i = dbegin; i != end;) {
  142. #else // !__MINGW32__
  143. std::string::iterator begin = name_.begin();
  144. std::string::iterator end = name_.end();
  145. for(std::string::iterator i = begin; i != end;) {
  146. #endif // !__MINGW32__
  147. std::string::iterator j = std::find(i, end, '/');
  148. if(std::distance(i, j) == 0) {
  149. ++i;
  150. continue;
  151. }
  152. i = j;
  153. if(i != end) {
  154. ++i;
  155. }
  156. #ifdef __MINGW32__
  157. if(*(j-1) == ':') {
  158. // This is a drive letter, e.g. C:, so skip it.
  159. continue;
  160. }
  161. #endif // __MINGW32__
  162. std::string dir(begin, j);
  163. A2_LOG_DEBUG(fmt("Making directory %s", dir.c_str()));
  164. if(File(dir).isDir()) {
  165. A2_LOG_DEBUG(fmt("%s exists and is a directory.", dir.c_str()));
  166. continue;
  167. }
  168. if(a2mkdir(utf8ToWChar(dir).c_str(), DIR_OPEN_MODE) == -1) {
  169. A2_LOG_DEBUG(fmt("Failed to create %s", dir.c_str()));
  170. return false;
  171. }
  172. }
  173. return true;
  174. }
  175. mode_t File::mode()
  176. {
  177. a2_struct_stat fstat;
  178. if(fillStat(fstat) < 0) {
  179. return 0;
  180. }
  181. return fstat.st_mode;
  182. }
  183. std::string File::getBasename() const
  184. {
  185. std::string::size_type lastSlashIndex =
  186. name_.find_last_of(getPathSeparators());
  187. if(lastSlashIndex == std::string::npos) {
  188. return name_;
  189. } else {
  190. return name_.substr(lastSlashIndex+1);
  191. }
  192. }
  193. std::string File::getDirname() const
  194. {
  195. std::string::size_type lastSlashIndex =
  196. name_.find_last_of(getPathSeparators());
  197. if(lastSlashIndex == std::string::npos) {
  198. if(name_.empty()) {
  199. return A2STR::NIL;
  200. } else {
  201. return ".";
  202. }
  203. } else if(lastSlashIndex == 0) {
  204. return "/";
  205. } else {
  206. return name_.substr(0, lastSlashIndex);
  207. }
  208. }
  209. bool File::isDir(const std::string& filename)
  210. {
  211. return File(filename).isDir();
  212. }
  213. bool File::renameTo(const std::string& dest)
  214. {
  215. #ifdef __MINGW32__
  216. /* MinGW's rename() doesn't delete an existing destination */
  217. if (_waccess(utf8ToWChar(dest).c_str(), 0) == 0) {
  218. if (a2unlink(utf8ToWChar(dest).c_str()) != 0) {
  219. return false;
  220. }
  221. }
  222. #endif // __MINGW32__
  223. if(a2rename(utf8ToWChar(name_).c_str(), utf8ToWChar(dest).c_str()) == 0) {
  224. name_ = dest;
  225. return true;
  226. } else {
  227. return false;
  228. }
  229. }
  230. bool File::utime(const Time& actime, const Time& modtime) const
  231. {
  232. #if defined HAVE_UTIMES && !(defined __MINGW32__)
  233. struct timeval times[2] = {
  234. { actime.getTime(), 0 },
  235. { modtime.getTime(), 0 }
  236. };
  237. return utimes(name_.c_str(), times) == 0;
  238. #else // !HAVE_UTIMES
  239. a2utimbuf ub;
  240. ub.actime = actime.getTime();
  241. ub.modtime = modtime.getTime();
  242. return a2utime(utf8ToWChar(name_).c_str(), &ub) == 0;
  243. #endif // !HAVE_UTIMES
  244. }
  245. Time File::getModifiedTime()
  246. {
  247. a2_struct_stat fstat;
  248. if(fillStat(fstat) < 0) {
  249. return 0;
  250. }
  251. return Time(fstat.st_mtime);
  252. }
  253. std::string File::getCurrentDir()
  254. {
  255. #ifdef __MINGW32__
  256. const size_t buflen = 2048;
  257. wchar_t buf[buflen];
  258. if(_wgetcwd(buf, buflen)) {
  259. return wCharToUtf8(buf);
  260. } else {
  261. return ".";
  262. }
  263. #else // !__MINGW32__
  264. const size_t buflen = 2048;
  265. char buf[buflen];
  266. if(getcwd(buf, buflen)) {
  267. return std::string(buf);
  268. } else {
  269. return ".";
  270. }
  271. #endif // !__MINGW32__
  272. }
  273. const char* File::getPathSeparators()
  274. {
  275. #ifdef __MINGW32__
  276. return "/\\";
  277. #else // !__MINGW32__
  278. return "/";
  279. #endif // !__MINGW32__
  280. }
  281. } // namespace aria2