File.cc 4.9 KB

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