00001
00002
00003
00004
00005
00006
00007
00008
00009 static const char rcsid[] __attribute__((used)) = "$Id: File.cpp 1230 2014-11-16 02:32:01Z sella $";
00010
00011 #include "File.h"
00012 #include "CommonMacro.h"
00013 #include "String.h"
00014
00015 using namespace sella::util;
00016
00017 std::string File::getBaseName(const std::string &path) throw () {
00018 if (path.empty()) {
00019 return path;
00020 }
00021
00022 return path.substr(path.rfind('/') + 1);
00023 }
00024
00025 std::string File::getDirName(const std::string &path) throw () {
00026 if (path.empty()) {
00027 return path;
00028 }
00029
00030 return path.substr(0, path.rfind('/'));
00031 }
00032
00033 std::string File::getRealPath(const std::string &path) throw (FileException) {
00034 char realPath[PATH_MAX];
00035
00036 if (path.empty()) {
00037 return path;
00038 }
00039
00040 if (::realpath(path.c_str(), realPath) != NULL) {
00041 return std::string(realPath);
00042 }
00043
00044 THROW2(FileException, "realpath()");
00045 }
00046
00047 bool File::exists(const std::string &fullpath, bool followSymlink) throw () {
00048 struct stat sb;
00049
00050 if (followSymlink) {
00051 return (stat(fullpath.c_str(), &sb) == 0);
00052 } else {
00053 return (lstat(fullpath.c_str(), &sb) == 0);
00054 }
00055 }
00056
00057 bool File::isRegularFile(const std::string &fullpath, bool followSymlink) throw (FileException) {
00058 return S_ISREG(File::getMode(fullpath, followSymlink));
00059 }
00060
00061 bool File::isDirectory(const std::string &fullpath, bool followSymlink) throw (FileException) {
00062 return S_ISDIR(File::getMode(fullpath, followSymlink));
00063 }
00064
00065 bool File::isCharacterDevice(const std::string &fullpath, bool followSymlink) throw (FileException) {
00066 return S_ISCHR(File::getMode(fullpath, followSymlink));
00067 }
00068
00069 bool File::isBlockDevice(const std::string &fullpath, bool followSymlink) throw (FileException) {
00070 return S_ISBLK(File::getMode(fullpath, followSymlink));
00071 }
00072
00073 bool File::isFIFO(const std::string &fullpath, bool followSymlink) throw (FileException) {
00074 return S_ISFIFO(File::getMode(fullpath, followSymlink));
00075 }
00076
00077 bool File::isSymLink(const std::string &fullpath) throw (FileException) {
00078 return S_ISLNK(File::getMode(fullpath, false));
00079 }
00080
00081 bool File::isSocket(const std::string &fullpath, bool followSymlink) throw (FileException) {
00082 return S_ISSOCK(File::getMode(fullpath, followSymlink));
00083 }
00084
00085 uid_t File::getUID(const std::string &fullpath, bool followSymlink) throw (FileException) {
00086 return File::getStat(fullpath, followSymlink).st_uid;
00087 }
00088
00089 gid_t File::getGID(const std::string &fullpath, bool followSymlink) throw (FileException) {
00090 return File::getStat(fullpath, followSymlink).st_gid;
00091 }
00092
00093 int64_t File::getSize(const std::string &fullpath, bool followSymlink) throw (FileException) {
00094 return (int64_t) File::getStat(fullpath, followSymlink).st_size;
00095 }
00096
00097 int64_t File::getBlocks(const std::string &fullpath, bool followSymlink) throw (FileException) {
00098 return (int64_t) File::getStat(fullpath, followSymlink).st_blocks;
00099 }
00100
00101 int64_t File::getBlockSize(const std::string &fullpath, bool followSymlink) throw (FileException) {
00102 return (int64_t) File::getStat(fullpath, followSymlink).st_blksize;
00103 }
00104
00105 time_t File::getATime(const std::string &fullpath, bool followSymlink) throw (FileException) {
00106 return File::getStat(fullpath, followSymlink).st_atime;
00107 }
00108
00109 time_t File::getMTime(const std::string &fullpath, bool followSymlink) throw (FileException) {
00110 return File::getStat(fullpath, followSymlink).st_mtime;
00111 }
00112
00113 time_t File::getCTime(const std::string &fullpath, bool followSymlink) throw (FileException) {
00114 return File::getStat(fullpath, followSymlink).st_ctime;
00115 }
00116
00117 struct stat File::getStat(const std::string &fullpath, bool followSymlink) throw (FileException) {
00118 struct stat sb;
00119
00120 if (followSymlink) {
00121 if (stat(fullpath.c_str(), &sb) < 0) {
00122 THROW2(FileException, "stat()");
00123 }
00124 } else {
00125 if (lstat(fullpath.c_str(), &sb)) {
00126 THROW2(FileException, "lstat()");
00127 }
00128 }
00129
00130 return sb;
00131 }
00132
00133 int File::getMode(const std::string &fullpath, bool followSymlink) throw (FileException) {
00134 return File::getStat(fullpath, followSymlink).st_mode;
00135 }
00136
00137
00138
00139