File.cc 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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 "Util.h"
  37. #include <cstring>
  38. #include <stdlib.h>
  39. #include <deque>
  40. namespace aria2 {
  41. #ifdef __MINGW32__
  42. # define WIN32_LEAN_AND_MEAN
  43. # include <windows.h>
  44. #endif // __MINGW32__
  45. File::File(const std::string& name):name(name) {}
  46. File::~File() {}
  47. int File::fillStat(struct stat& fstat) {
  48. return stat(name.c_str(), &fstat);
  49. }
  50. bool File::exists() {
  51. struct stat fstat;
  52. return fillStat(fstat) == 0;
  53. }
  54. bool File::isFile() {
  55. struct stat fstat;
  56. if(fillStat(fstat) < 0) {
  57. return false;
  58. }
  59. return S_ISREG(fstat.st_mode) == 1;
  60. }
  61. bool File::isDir() {
  62. struct stat fstat;
  63. if(fillStat(fstat) < 0) {
  64. return false;
  65. }
  66. return S_ISDIR(fstat.st_mode) == 1;
  67. }
  68. bool File::remove() {
  69. if(isFile()) {
  70. return unlink(name.c_str()) == 0;
  71. } else if(isDir()) {
  72. return rmdir(name.c_str()) == 0;
  73. } else {
  74. return false;
  75. }
  76. }
  77. uint64_t File::size() {
  78. struct stat fstat;
  79. if(fillStat(fstat) < 0) {
  80. return 0;
  81. }
  82. return fstat.st_size;
  83. }
  84. bool File::mkdirs() {
  85. if(isDir()) {
  86. return false;
  87. }
  88. std::deque<std::string> dirs;
  89. Util::slice(dirs, name, '/');
  90. if(!dirs.size()) {
  91. return true;
  92. }
  93. std::string accDir;
  94. if(Util::startsWith(name, "/")) {
  95. accDir = "/";
  96. }
  97. for(std::deque<std::string>::const_iterator itr = dirs.begin(); itr != dirs.end();
  98. itr++, accDir += "/") {
  99. accDir += *itr;
  100. if(File(accDir).isDir()) {
  101. continue;
  102. }
  103. if(a2mkdir(accDir.c_str(), DIR_OPEN_MODE) == -1) {
  104. return false;
  105. }
  106. }
  107. return true;
  108. }
  109. mode_t File::mode()
  110. {
  111. struct stat fstat;
  112. if(fillStat(fstat) < 0) {
  113. return 0;
  114. }
  115. return fstat.st_mode;
  116. }
  117. std::string File::getBasename() const
  118. {
  119. std::string::size_type lastSlashIndex = name.find_last_of("/");
  120. if(lastSlashIndex == std::string::npos) {
  121. return name;
  122. } else {
  123. return name.substr(lastSlashIndex+1);
  124. }
  125. }
  126. std::string File::getDirname() const
  127. {
  128. std::string::size_type lastSlashIndex = name.find_last_of("/");
  129. if(lastSlashIndex == std::string::npos) {
  130. if(name == "") {
  131. return "";
  132. } else {
  133. return ".";
  134. }
  135. } else if(lastSlashIndex == 0) {
  136. return "/";
  137. } else {
  138. return name.substr(0, lastSlashIndex);
  139. }
  140. }
  141. bool File::isDir(const std::string& filename)
  142. {
  143. return File(filename).isDir();
  144. }
  145. bool File::renameTo(const std::string& dest)
  146. {
  147. #ifdef __MINGW32__
  148. /* MinGW's rename() doesn't delete an existing destination */
  149. if (_access(dest.c_str(), 0) == 0) {
  150. if (_unlink(dest.c_str()) != 0) {
  151. return false;
  152. }
  153. }
  154. #endif // __MINGW32__
  155. if(rename(name.c_str(), dest.c_str()) == 0) {
  156. name = dest;
  157. return true;
  158. } else {
  159. return false;
  160. }
  161. }
  162. } // namespace aria2