File.cc 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  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. #include <utime.h>
  39. #include <unistd.h>
  40. #include <vector>
  41. #include <cstring>
  42. #include <cstdio>
  43. #include "util.h"
  44. #include "A2STR.h"
  45. #include "array_fun.h"
  46. #include "Logger.h"
  47. #include "LogFactory.h"
  48. #include "fmt.h"
  49. namespace aria2 {
  50. #ifdef __MINGW32__
  51. # define WIN32_LEAN_AND_MEAN
  52. # include <windows.h>
  53. #endif // __MINGW32__
  54. File::File(const std::string& name) : name_(name) {}
  55. File::File(const File& c) : name_(c.name_) {}
  56. File::~File() {}
  57. File& File::operator=(const File& c)
  58. {
  59. if(this != &c) {
  60. name_ = c.name_;
  61. }
  62. return *this;
  63. }
  64. int File::fillStat(a2_struct_stat& fstat) {
  65. return a2stat(utf8ToWChar(name_).c_str(), &fstat);
  66. }
  67. bool File::exists() {
  68. a2_struct_stat fstat;
  69. return fillStat(fstat) == 0;
  70. }
  71. bool File::isFile() {
  72. a2_struct_stat fstat;
  73. if(fillStat(fstat) < 0) {
  74. return false;
  75. }
  76. return S_ISREG(fstat.st_mode) == 1;
  77. }
  78. bool File::isDir() {
  79. a2_struct_stat fstat;
  80. if(fillStat(fstat) < 0) {
  81. return false;
  82. }
  83. return S_ISDIR(fstat.st_mode) == 1;
  84. }
  85. bool File::remove() {
  86. if(isFile()) {
  87. return a2unlink(utf8ToWChar(name_).c_str()) == 0;
  88. } else if(isDir()) {
  89. return a2rmdir(utf8ToWChar(name_).c_str()) == 0;
  90. } else {
  91. return false;
  92. }
  93. }
  94. uint64_t File::size() {
  95. a2_struct_stat fstat;
  96. if(fillStat(fstat) < 0) {
  97. return 0;
  98. }
  99. return fstat.st_size;
  100. }
  101. bool File::mkdirs() {
  102. if(isDir()) {
  103. return false;
  104. }
  105. #ifdef __MINGW32__
  106. std::string path = name_;
  107. for(std::string::iterator i = path.begin(), eoi = path.end(); i != eoi; ++i) {
  108. if(*i == '\\') {
  109. *i = '/';
  110. }
  111. }
  112. std::string::iterator begin = path.begin();
  113. std::string::iterator end = path.end();
  114. #else // !__MINGW32__
  115. std::string::iterator begin = name_.begin();
  116. std::string::iterator end = name_.end();
  117. #endif // !__MINGW32__
  118. for(std::string::iterator i = begin; i != end;) {
  119. std::string::iterator j = std::find(i, end, '/');
  120. if(std::distance(i, j) == 0) {
  121. ++i;
  122. continue;
  123. }
  124. i = j;
  125. if(i != end) {
  126. ++i;
  127. }
  128. #ifdef __MINGW32__
  129. if(*(j-1) == ':') {
  130. // This is a drive letter, e.g. C:, so skip it.
  131. continue;
  132. }
  133. #endif // __MINGW32__
  134. std::string dir = std::string(begin, j);
  135. A2_LOG_DEBUG(fmt("Making directory %s", utf8ToNative(dir).c_str()));
  136. if(File(dir).isDir()) {
  137. A2_LOG_DEBUG(fmt("%s exists and is a directory.",
  138. utf8ToNative(dir).c_str()));
  139. continue;
  140. }
  141. if(a2mkdir(utf8ToWChar(dir).c_str(), DIR_OPEN_MODE) == -1) {
  142. A2_LOG_DEBUG(fmt("Failed to create %s", utf8ToNative(dir).c_str()));
  143. return false;
  144. }
  145. }
  146. return true;
  147. }
  148. mode_t File::mode()
  149. {
  150. a2_struct_stat fstat;
  151. if(fillStat(fstat) < 0) {
  152. return 0;
  153. }
  154. return fstat.st_mode;
  155. }
  156. std::string File::getBasename() const
  157. {
  158. std::string::size_type lastSlashIndex = name_.find_last_of(A2STR::SLASH_C);
  159. if(lastSlashIndex == std::string::npos) {
  160. return name_;
  161. } else {
  162. return name_.substr(lastSlashIndex+1);
  163. }
  164. }
  165. std::string File::getDirname() const
  166. {
  167. std::string::size_type lastSlashIndex = name_.find_last_of(A2STR::SLASH_C);
  168. if(lastSlashIndex == std::string::npos) {
  169. if(name_.empty()) {
  170. return A2STR::NIL;
  171. } else {
  172. return A2STR::DOT_C;
  173. }
  174. } else if(lastSlashIndex == 0) {
  175. return A2STR::SLASH_C;
  176. } else {
  177. return name_.substr(0, lastSlashIndex);
  178. }
  179. }
  180. bool File::isDir(const std::string& filename)
  181. {
  182. return File(filename).isDir();
  183. }
  184. bool File::renameTo(const std::string& dest)
  185. {
  186. #ifdef __MINGW32__
  187. /* MinGW's rename() doesn't delete an existing destination */
  188. if (_waccess(utf8ToWChar(dest).c_str(), 0) == 0) {
  189. if (a2unlink(utf8ToWChar(dest).c_str()) != 0) {
  190. return false;
  191. }
  192. }
  193. #endif // __MINGW32__
  194. if(a2rename(utf8ToWChar(name_).c_str(), utf8ToWChar(dest).c_str()) == 0) {
  195. name_ = dest;
  196. return true;
  197. } else {
  198. return false;
  199. }
  200. }
  201. bool File::utime(const Time& actime, const Time& modtime) const
  202. {
  203. a2utimbuf ub;
  204. ub.actime = actime.getTime();
  205. ub.modtime = modtime.getTime();
  206. return a2utime(utf8ToWChar(name_).c_str(), &ub) == 0;
  207. }
  208. Time File::getModifiedTime()
  209. {
  210. a2_struct_stat fstat;
  211. if(fillStat(fstat) < 0) {
  212. return 0;
  213. }
  214. return Time(fstat.st_mtime);
  215. }
  216. std::string File::getCurrentDir()
  217. {
  218. #ifdef __MINGW32__
  219. const size_t buflen = 2048;
  220. wchar_t buf[buflen];
  221. if(_wgetcwd(buf, buflen)) {
  222. return wCharToUtf8(buf);
  223. } else {
  224. return A2STR::DOT_C;
  225. }
  226. #else // !__MINGW32__
  227. const size_t buflen = 2048;
  228. char buf[buflen];
  229. if(getcwd(buf, buflen)) {
  230. return std::string(buf);
  231. } else {
  232. return A2STR::DOT_C;
  233. }
  234. #endif // !__MINGW32__
  235. }
  236. } // namespace aria2