File.cc 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. /* <!-- copyright */
  2. /*
  3. * aria2 - a simple utility for downloading files faster
  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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  20. */
  21. /* copyright --> */
  22. #include "File.h"
  23. #include <sys/types.h>
  24. #include <sys/stat.h>
  25. #include <unistd.h>
  26. File::File(const string& name):name(name) {}
  27. File::~File() {}
  28. int File::fillStat(struct stat& fstat) {
  29. return stat(name.c_str(), &fstat);
  30. }
  31. bool File::exists() {
  32. struct stat fstat;
  33. return fillStat(fstat) == 0;
  34. }
  35. bool File::isFile() {
  36. struct stat fstat;
  37. if(fillStat(fstat) < 0) {
  38. return false;
  39. }
  40. return S_ISREG(fstat.st_mode) == 1;
  41. }
  42. bool File::isDir() {
  43. struct stat fstat;
  44. if(fillStat(fstat) < 0) {
  45. return false;
  46. }
  47. return S_ISDIR(fstat.st_mode) == 1;
  48. }
  49. bool File::remove() {
  50. if(isFile()) {
  51. return unlink(name.c_str()) == 0;
  52. } else if(isDir()) {
  53. return rmdir(name.c_str()) == 0;
  54. } else {
  55. return false;
  56. }
  57. }
  58. long long int File::size() {
  59. struct stat fstat;
  60. if(fillStat(fstat) < 0) {
  61. return 0;
  62. }
  63. return fstat.st_size;
  64. }