00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026 #include "io/File.h"
00027
00028
00029 #include <stdio.h>
00030 #include <errno.h>
00031 #include <string.h>
00032
00033 #include <fcntl.h>
00034 #include <sys/types.h>
00035 #include <sys/stat.h>
00036
00037 #ifdef _WIN32
00038 # include <direct.h>
00039 # include <io.h>
00040 # include <winsock2.h>
00041 # include "win32/timecnv.h"
00042
00043
00044 # define F_OK 0
00045 # define X_OK 1
00046 # define W_OK 2
00047 # define R_OK 4
00048
00049 #define mkdir(path, mode) _mkdir(path)
00050
00051 #else
00052 # include <dirent.h>
00053 # include "io/pstream.h"
00054 #endif
00055
00056 namespace IXE {
00057 namespace io {
00058
00059 bool File::exists()
00060 {
00061 struct stat stat_buf;
00062 return ::stat(pathname, &stat_buf) != -1;
00063 }
00064
00065 bool File::canRead()
00066 {
00067 return ::access(pathname, R_OK) == 0;
00068 }
00069
00070 bool File::canWrite()
00071 {
00072 return ::access(pathname, W_OK) == 0;
00073 }
00074
00075 bool File::isDirectory()
00076 {
00077 struct stat stat_buf;
00078 return ::stat(pathname, &stat_buf) != -1 && S_ISDIR(stat_buf.st_mode);
00079 }
00080
00081 bool File::isEmpty()
00082 {
00083 return length() == 0;
00084 }
00085
00086 bool File::isFile()
00087 {
00088 struct stat stat_buf;
00089 return ::stat(pathname, &stat_buf) != -1 && S_ISREG(stat_buf.st_mode);
00090 }
00091
00092 bool File::isIndexable()
00093 {
00094 # ifdef _WIN32
00095 DWORD dwAttrs = GetFileAttributes(pathname);
00096 return !(dwAttrs & (FILE_ATTRIBUTE_SYSTEM |
00097 FILE_ATTRIBUTE_HIDDEN |
00098 FILE_ATTRIBUTE_NOT_CONTENT_INDEXED));
00099 # else
00100 return isFile();
00101 # endif
00102 }
00103
00104 bool File::isLink()
00105 {
00106 # ifdef _WIN32
00107 return false;
00108 # else
00109 struct stat lstat_buf;
00110 return ::lstat(pathname, &lstat_buf) != -1
00111 && S_ISLNK(lstat_buf.st_mode & S_IFLNK);
00112 # endif
00113 }
00114
00115 bool File::isHidden()
00116 {
00117 return false;
00118 }
00119
00120 off64_t File::length()
00121 {
00122 struct stat sbuf;
00123 if (::stat(pathname, &sbuf) != 0)
00124 return 0;
00125 return sbuf.st_size;
00126 }
00127
00128 bool File::create()
00129 {
00130 if (exists())
00131 return false;
00132 int fd = ::open(pathname, O_CREAT | O_EXCL, S_IREAD | S_IWRITE);
00133 if (fd == -1)
00134 return false;
00135 ::close(fd);
00136 return true;
00137 }
00138
00139 bool File::remove()
00140 {
00141 if (!exists())
00142 return false;
00143 if (isDirectory())
00144 return ::rmdir(pathname) == 0;
00145 return ::unlink(pathname) == 0;
00146 }
00147
00148 time_t File::lastAccessed()
00149 {
00150 # ifdef _WIN32
00151 FILETIME creationTime;
00152 FILETIME lastAccessTime;
00153 FILETIME lastWriteTime;
00154
00155 HANDLE hf = CreateFile(pathname, GENERIC_READ, 0, NULL, OPEN_EXISTING, 0, NULL);
00156 if (hf == INVALID_HANDLE_VALUE)
00157 return 0;
00158 GetFileTime(hf, &creationTime, &lastAccessTime, &lastWriteTime);
00159 CloseHandle(hf);
00160 return ::FileTimeToNetworkTime(lastWriteTime);
00161 #else
00162 struct stat sbuf;
00163 if (::stat(pathname, &sbuf) != 0)
00164 return 0;
00165 return time_t(sbuf.st_atime);
00166 #endif
00167 }
00168
00169 bool File::lastAccessed(time_t accesstime)
00170 {
00171 # ifdef _WIN32
00172 HANDLE hf = CreateFile(pathname, GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL);
00173 if (hf == INVALID_HANDLE_VALUE)
00174 return false;
00175 FILETIME cft = ::NetworkTimeToFileTime(accesstime);
00176 BOOL bret = SetFileTime(hf, NULL, &cft, NULL);
00177 CloseHandle(hf);
00178 return bret == TRUE;
00179 # else
00180 utimbuf buf;
00181 buf.actime = (long)(accesstime / 1000);
00182 buf.modtime = (long)lastModified() / 1000;
00183 return ::utime(pathname, &buf) == 0;
00184 # endif
00185 }
00186
00187 time_t File::lastModified()
00188 {
00189 # ifdef _WIN32
00190 FILETIME creationTime;
00191 FILETIME lastAccessTime;
00192 FILETIME lastWriteTime;
00193
00194 HANDLE hf = CreateFile(pathname, GENERIC_READ, 0, NULL, OPEN_EXISTING, 0, NULL);
00195 if (hf == INVALID_HANDLE_VALUE)
00196 return 0;
00197 GetFileTime(hf, &creationTime, &lastAccessTime, &lastWriteTime);
00198 CloseHandle(hf);
00199 return ::FileTimeToNetworkTime(lastWriteTime);
00200 # else
00201 struct stat sbuf;
00202 if (::stat(pathname, &sbuf) != 0)
00203 return 0;
00204 return time_t(sbuf.st_atime);
00205 # endif
00206 }
00207
00208 bool File::lastModified(time_t modtime)
00209 {
00210 # ifdef _WIN32
00211 HANDLE hf = CreateFile(pathname, GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL);
00212 if (hf == INVALID_HANDLE_VALUE)
00213 return false;
00214 FILETIME cft = ::NetworkTimeToFileTime(modtime);
00215 BOOL bret = SetFileTime(hf, NULL, NULL, &cft);
00216 CloseHandle(hf);
00217 return bret == TRUE;
00218 # else
00219 utimbuf buf;
00220 buf.actime = (long)(lastAccessed() / 1000);
00221 buf.modtime = (long)(modtime / 1000);
00222 return ::utime(pathname, &buf) == 0;
00223 # endif
00224 }
00225
00226 time_t File::fileCreated()
00227 {
00228 # ifdef _WIN32
00229 FILETIME creationTime;
00230 FILETIME lastAccessTime;
00231 FILETIME lastWriteTime;
00232 HANDLE hf = CreateFile(pathname, GENERIC_READ, 0, NULL, OPEN_EXISTING, 0, NULL);
00233 if (hf == INVALID_HANDLE_VALUE)
00234 return 0;
00235 GetFileTime(hf, &creationTime, &lastAccessTime, &lastWriteTime);
00236 CloseHandle(hf);
00237 return ::FileTimeToNetworkTime(creationTime);
00238 #else
00239 struct stat sbuf;
00240 if (::stat(pathname, &sbuf) != 0)
00241 return 0;
00242 return time_t(sbuf.st_ctime);
00243 #endif
00244 }
00245
00246 bool File::makedir(int mode)
00247 {
00248 if (exists())
00249 return false;
00250 return ::mkdir(pathname, mode) == 0;
00251 }
00252
00253 bool File::makedirs(int mode)
00254 {
00255 char dir[PATH_MAX];
00256 char const* fn = pathname;
00257 while (fn && fn[0]) {
00258 char const* slash = ::strchr(fn, '/');
00259 if (!slash && !(slash = ::strchr(fn, PATH_SEPARATOR)))
00260 break;
00261 int dirlen = slash - pathname;
00262 ::strncpy(dir, pathname, dirlen);
00263 dir[dirlen] = '\0';
00264 fn = slash + 1;
00265 if (File(dir).isDirectory())
00266 continue;
00267 if (::mkdir(dir, mode))
00268 return false;
00269 }
00270 return true;
00271 }
00272
00273 bool File::rename(char const* dest)
00274 {
00275 if (!exists())
00276 return false;
00277 if (File(dest).exists())
00278 return false;
00279 return ::rename(pathname, dest) == 0;
00280 }
00281
00282 std::string File::mimeType()
00283 {
00284 # ifndef _WIN32
00285 char checkMime[PATH_MAX + 20] = "file -i ";
00286 ::strcat(checkMime, pathname);
00287 ipstream output(checkMime);
00288 std::string line;
00289 std::getline(output, line);
00290
00291
00292 int ms = line.find(' ') + 1;
00293 return std::string(line, ms);
00294 # else
00295 return std::string("text/html");
00296 # endif
00297 }
00298
00299 }
00300 }