File.cc 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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. namespace aria2 {
  47. #ifdef __MINGW32__
  48. # define WIN32_LEAN_AND_MEAN
  49. # include <windows.h>
  50. #endif // __MINGW32__
  51. File::File(const std::string& name) : name_(name) {}
  52. File::File(const File& c) : name_(c.name_) {}
  53. File::~File() {}
  54. File& File::operator=(const File& c)
  55. {
  56. if(this != &c) {
  57. name_ = c.name_;
  58. }
  59. return *this;
  60. }
  61. int File::fillStat(a2_struct_stat& fstat) {
  62. return a2stat(name_.c_str(), &fstat);
  63. }
  64. bool File::exists() {
  65. a2_struct_stat fstat;
  66. return fillStat(fstat) == 0;
  67. }
  68. bool File::isFile() {
  69. a2_struct_stat fstat;
  70. if(fillStat(fstat) < 0) {
  71. return false;
  72. }
  73. return S_ISREG(fstat.st_mode) == 1;
  74. }
  75. bool File::isDir() {
  76. a2_struct_stat fstat;
  77. if(fillStat(fstat) < 0) {
  78. return false;
  79. }
  80. return S_ISDIR(fstat.st_mode) == 1;
  81. }
  82. bool File::remove() {
  83. if(isFile()) {
  84. return unlink(name_.c_str()) == 0;
  85. } else if(isDir()) {
  86. return rmdir(name_.c_str()) == 0;
  87. } else {
  88. return false;
  89. }
  90. }
  91. uint64_t File::size() {
  92. a2_struct_stat fstat;
  93. if(fillStat(fstat) < 0) {
  94. return 0;
  95. }
  96. return fstat.st_size;
  97. }
  98. bool File::mkdirs() {
  99. if(isDir()) {
  100. return false;
  101. }
  102. for(std::string::iterator i = name_.begin(), eoi = name_.end();
  103. i != eoi;) {
  104. std::string::iterator j = std::find(i, eoi, '/');
  105. if(std::distance(i, j) == 0) {
  106. ++i;
  107. continue;
  108. }
  109. i = j;
  110. if(j != eoi) {
  111. ++i;
  112. }
  113. std::string dir = std::string(name_.begin(), j);
  114. if(File(dir).isDir()) {
  115. continue;
  116. }
  117. if(a2mkdir(dir.c_str(), DIR_OPEN_MODE) == -1) {
  118. return false;
  119. }
  120. }
  121. return true;
  122. }
  123. mode_t File::mode()
  124. {
  125. a2_struct_stat fstat;
  126. if(fillStat(fstat) < 0) {
  127. return 0;
  128. }
  129. return fstat.st_mode;
  130. }
  131. std::string File::getBasename() const
  132. {
  133. std::string::size_type lastSlashIndex = name_.find_last_of(A2STR::SLASH_C);
  134. if(lastSlashIndex == std::string::npos) {
  135. return name_;
  136. } else {
  137. return name_.substr(lastSlashIndex+1);
  138. }
  139. }
  140. std::string File::getDirname() const
  141. {
  142. std::string::size_type lastSlashIndex = name_.find_last_of(A2STR::SLASH_C);
  143. if(lastSlashIndex == std::string::npos) {
  144. if(name_.empty()) {
  145. return A2STR::NIL;
  146. } else {
  147. return A2STR::DOT_C;
  148. }
  149. } else if(lastSlashIndex == 0) {
  150. return A2STR::SLASH_C;
  151. } else {
  152. return name_.substr(0, lastSlashIndex);
  153. }
  154. }
  155. bool File::isDir(const std::string& filename)
  156. {
  157. return File(filename).isDir();
  158. }
  159. bool File::renameTo(const std::string& dest)
  160. {
  161. #ifdef __MINGW32__
  162. /* MinGW's rename() doesn't delete an existing destination */
  163. if (_access(dest.c_str(), 0) == 0) {
  164. if (_unlink(dest.c_str()) != 0) {
  165. return false;
  166. }
  167. }
  168. #endif // __MINGW32__
  169. if(rename(name_.c_str(), dest.c_str()) == 0) {
  170. name_ = dest;
  171. return true;
  172. } else {
  173. return false;
  174. }
  175. }
  176. bool File::utime(const Time& actime, const Time& modtime) const
  177. {
  178. struct utimbuf ub;
  179. ub.actime = actime.getTime();
  180. ub.modtime = modtime.getTime();
  181. return ::utime(name_.c_str(), &ub) == 0;
  182. }
  183. Time File::getModifiedTime()
  184. {
  185. a2_struct_stat fstat;
  186. if(fillStat(fstat) < 0) {
  187. return 0;
  188. }
  189. return Time(fstat.st_mtime);
  190. }
  191. std::string File::getCurrentDir()
  192. {
  193. const size_t buflen = 2048;
  194. char buf[buflen];
  195. if(getcwd(buf, buflen)) {
  196. return std::string(buf);
  197. } else {
  198. return A2STR::DOT_C;
  199. }
  200. }
  201. } // namespace aria2