/* */ #include "File.h" #include #include #include #include #include #include #include #include "util.h" #include "A2STR.h" #include "array_fun.h" 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(a2_struct_stat& fstat) { return a2stat(name_.c_str(), &fstat); } bool File::exists() { a2_struct_stat fstat; return fillStat(fstat) == 0; } bool File::isFile() { a2_struct_stat fstat; if(fillStat(fstat) < 0) { return false; } return S_ISREG(fstat.st_mode) == 1; } bool File::isDir() { a2_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() { a2_struct_stat fstat; if(fillStat(fstat) < 0) { return 0; } return fstat.st_size; } bool File::mkdirs() { if(isDir()) { return false; } std::vector dirs; util::split(name_, std::back_inserter(dirs), "/"); if(!dirs.size()) { return true; } std::string accDir; if(util::startsWith(name_, A2STR::SLASH_C)) { accDir = A2STR::SLASH_C; } for(std::vector::const_iterator itr = dirs.begin(), eoi = dirs.end(); itr != eoi; ++itr, accDir += A2STR::SLASH_C) { accDir += *itr; if(File(accDir).isDir()) { continue; } if(a2mkdir(accDir.c_str(), DIR_OPEN_MODE) == -1) { return false; } } return true; } mode_t File::mode() { a2_struct_stat fstat; if(fillStat(fstat) < 0) { return 0; } return fstat.st_mode; } std::string File::getBasename() const { std::string::size_type lastSlashIndex = name_.find_last_of(A2STR::SLASH_C); if(lastSlashIndex == std::string::npos) { return name_; } else { return name_.substr(lastSlashIndex+1); } } std::string File::getDirname() const { std::string::size_type lastSlashIndex = name_.find_last_of(A2STR::SLASH_C); if(lastSlashIndex == std::string::npos) { if(name_.empty()) { return A2STR::NIL; } else { return A2STR::DOT_C; } } else if(lastSlashIndex == 0) { return A2STR::SLASH_C; } else { return name_.substr(0, lastSlashIndex); } } 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; } } bool File::utime(const Time& actime, const Time& modtime) const { struct utimbuf ub; ub.actime = actime.getTime(); ub.modtime = modtime.getTime(); return ::utime(name_.c_str(), &ub) == 0; } Time File::getModifiedTime() { a2_struct_stat fstat; if(fillStat(fstat) < 0) { return 0; } return Time(fstat.st_mtime); } std::string File::getCurrentDir() { const size_t buflen = 2048; char buf[buflen]; if(getcwd(buf, buflen)) { return std::string(buf); } else { return A2STR::DOT_C; } } } // namespace aria2