File.cc 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  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(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 unlink(name_.c_str()) == 0;
  88. } else if(isDir()) {
  89. return rmdir(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", dir.c_str()));
  136. if(File(dir).isDir()) {
  137. A2_LOG_DEBUG(fmt("%s exists and is a directory.", dir.c_str()));
  138. continue;
  139. }
  140. if(a2mkdir(dir.c_str(), DIR_OPEN_MODE) == -1) {
  141. A2_LOG_DEBUG(fmt("Failed to create %s", dir.c_str()));
  142. return false;
  143. }
  144. }
  145. return true;
  146. }
  147. mode_t File::mode()
  148. {
  149. a2_struct_stat fstat;
  150. if(fillStat(fstat) < 0) {
  151. return 0;
  152. }
  153. return fstat.st_mode;
  154. }
  155. std::string File::getBasename() const
  156. {
  157. std::string::size_type lastSlashIndex = name_.find_last_of(A2STR::SLASH_C);
  158. if(lastSlashIndex == std::string::npos) {
  159. return name_;
  160. } else {
  161. return name_.substr(lastSlashIndex+1);
  162. }
  163. }
  164. std::string File::getDirname() const
  165. {
  166. std::string::size_type lastSlashIndex = name_.find_last_of(A2STR::SLASH_C);
  167. if(lastSlashIndex == std::string::npos) {
  168. if(name_.empty()) {
  169. return A2STR::NIL;
  170. } else {
  171. return A2STR::DOT_C;
  172. }
  173. } else if(lastSlashIndex == 0) {
  174. return A2STR::SLASH_C;
  175. } else {
  176. return name_.substr(0, lastSlashIndex);
  177. }
  178. }
  179. bool File::isDir(const std::string& filename)
  180. {
  181. return File(filename).isDir();
  182. }
  183. bool File::renameTo(const std::string& dest)
  184. {
  185. #ifdef __MINGW32__
  186. /* MinGW's rename() doesn't delete an existing destination */
  187. if (_access(dest.c_str(), 0) == 0) {
  188. if (_unlink(dest.c_str()) != 0) {
  189. return false;
  190. }
  191. }
  192. #endif // __MINGW32__
  193. if(rename(name_.c_str(), dest.c_str()) == 0) {
  194. name_ = dest;
  195. return true;
  196. } else {
  197. return false;
  198. }
  199. }
  200. bool File::utime(const Time& actime, const Time& modtime) const
  201. {
  202. struct utimbuf ub;
  203. ub.actime = actime.getTime();
  204. ub.modtime = modtime.getTime();
  205. return ::utime(name_.c_str(), &ub) == 0;
  206. }
  207. Time File::getModifiedTime()
  208. {
  209. a2_struct_stat fstat;
  210. if(fillStat(fstat) < 0) {
  211. return 0;
  212. }
  213. return Time(fstat.st_mtime);
  214. }
  215. std::string File::getCurrentDir()
  216. {
  217. const size_t buflen = 2048;
  218. char buf[buflen];
  219. if(getcwd(buf, buflen)) {
  220. return std::string(buf);
  221. } else {
  222. return A2STR::DOT_C;
  223. }
  224. }
  225. } // namespace aria2