/* */ #include "File.h" #include "Util.h" #include // use GNU version basename #undef basename #include #include namespace aria2 { #ifdef __MINGW32__ # define WIN32_LEAN_AND_MEAN # include #endif // __MINGW32__ File::File(const std::string& name):name(name) {} File::~File() {} int File::fillStat(struct stat& fstat) { return stat(name.c_str(), &fstat); } bool File::exists() { struct stat fstat; return fillStat(fstat) == 0; } bool File::isFile() { struct stat fstat; if(fillStat(fstat) < 0) { return false; } return S_ISREG(fstat.st_mode) == 1; } bool File::isDir() { struct stat fstat; if(fillStat(fstat) < 0) { return false; } return S_ISDIR(fstat.st_mode) == 1; } bool File::remove() { if(isFile()) { return unlink(name.c_str()) == 0; } else if(isDir()) { return rmdir(name.c_str()) == 0; } else { return false; } } uint64_t File::size() { struct stat fstat; if(fillStat(fstat) < 0) { return 0; } return fstat.st_size; } bool File::mkdirs() { if(isDir()) { return false; } std::deque dirs; Util::slice(dirs, name, '/'); if(!dirs.size()) { return true; } std::string accDir; if(Util::startsWith(name, "/")) { accDir = "/"; } for(std::deque::const_iterator itr = dirs.begin(); itr != dirs.end(); itr++, accDir += "/") { accDir += *itr; if(File(accDir).isDir()) { continue; } if(a2mkdir(accDir.c_str(), DIR_OPEN_MODE) == -1) { return false; } } return true; } mode_t File::mode() { struct stat fstat; if(fillStat(fstat) < 0) { return 0; } return fstat.st_mode; } std::string File::getBasename() const { char* s = strdup(name.c_str()); std::string bname = basename(s); free(s); return bname; } std::string File::getDirname() const { char* s = strdup(name.c_str()); std::string dname = dirname(s); free(s); return dname; } bool File::isDir(const std::string& filename) { return File(filename).isDir(); } bool File::renameTo(const std::string& dest) { #ifdef __MINGW32__ /* MinGW's rename() doesn't delete an existing destination */ if (_access(dest.c_str(), 0) == 0) { if (_unlink(dest.c_str()) != 0) { return false; } } #endif // __MINGW32__ if(rename(name.c_str(), dest.c_str()) == 0) { name = dest; return true; } else { return false; } } } // namespace aria2