/* */ #include "File.h" #include "Util.h" #include "a2io.h" #include File::File(const string& name):name(name) {} File::~File() {} int32_t 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; } } int64_t File::size() { struct stat fstat; if(fillStat(fstat) < 0) { return 0; } return fstat.st_size; } bool File::mkdirs() { if(isDir()) { return false; } Strings dirs; Util::slice(dirs, name, '/'); if(!dirs.size()) { return true; } string accDir; if(Util::startsWith(name, "/")) { accDir = "/"; } for(Strings::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; } string File::getBasename() const { char* s = strdup(name.c_str()); string bname = basename(s); free(s); return bname; } string File::getDirname() const { char* s = strdup(name.c_str()); string dname = dirname(s); free(s); return dname; } bool File::isDir(const string& filename) { return File(filename).isDir(); }