00001
00002
00003
00004
00005
00006
00007
00008 #include "fileutils.h"
00009 #include "wvfile.h"
00010 #include <string.h>
00011 #include <unistd.h>
00012 #include <sys/stat.h>
00013 #include <utime.h>
00014 #include <fnmatch.h>
00015
00016 bool mkdirp(WvStringParm _dir, int create_mode)
00017 {
00018 if (!access(_dir, X_OK))
00019 return true;
00020
00021
00022 assert(!!_dir);
00023
00024 WvString dir(_dir);
00025 char *p = dir.edit();
00026
00027 while ((p = strchr(++p, '/')))
00028 {
00029 *p = '\0';
00030 if (access(dir.cstr(), X_OK) && mkdir(dir.cstr(), create_mode))
00031 return false;
00032 *p = '/';
00033 }
00034
00035
00036
00037 return !(access(dir.cstr(), X_OK&W_OK) && mkdir(dir.cstr(), create_mode));
00038 }
00039
00040
00041 bool fcopy(WvStringParm src, WvStringParm dst)
00042 {
00043 struct stat buf;
00044 if (stat(src, &buf))
00045 return false;
00046
00047 WvFile in(src, O_RDONLY);
00048 unlink(dst);
00049
00050 int oldmode = umask(0);
00051 WvFile out(dst, O_CREAT|O_WRONLY, buf.st_mode & 007777);
00052 umask(oldmode);
00053
00054 in.autoforward(out);
00055 while (in.isok() && out.isok())
00056 {
00057
00058
00059
00060
00061 if (in.select(-1, true, false))
00062 in.callback();
00063 }
00064 if (!out.isok())
00065 return false;
00066
00067 struct utimbuf utim;
00068 utim.actime = utim.modtime = buf.st_mtime;
00069 if (utime(dst, &utim))
00070 return false;
00071
00072 return true;
00073 }
00074
00075
00076 bool fcopy(WvStringParm srcdir, WvStringParm dstdir, WvStringParm relname)
00077 {
00078 return fcopy(WvString("%s/%s", srcdir, relname),
00079 WvString("%s/%s", dstdir, relname));
00080 }
00081
00082
00083 bool samedate(WvStringParm file1, WvStringParm file2)
00084 {
00085 struct stat buf;
00086 struct stat buf2;
00087
00088 if (stat(file1, &buf) || stat(file2, &buf2))
00089 return false;
00090
00091 if (buf.st_mtime == buf2.st_mtime || buf.st_ctime == buf2.st_ctime)
00092 return true;
00093
00094 return false;
00095 }
00096
00097
00098 bool samedate(WvStringParm dir1, WvStringParm dir2, WvStringParm relname)
00099 {
00100 return samedate(WvString("%s/%s", dir1, relname),
00101 WvString("%s/%s", dir2, relname));
00102 }
00103
00104
00105
00106
00107 bool wvfnmatch(WvStringList& patterns, WvStringParm name, int flags)
00108 {
00109 WvStringList::Iter i(patterns);
00110 bool match = false;
00111
00112 for (i.rewind(); i.next(); )
00113 {
00114
00115 if (*i == "!") {
00116 match = false;
00117 continue;
00118 }
00119
00120
00121
00122 if (i->cstr()[0] == '!')
00123 {
00124 if (!match)
00125 continue;
00126 if (fnmatch(*i+1, name, flags) == 0)
00127 match = false;
00128 }
00129 else
00130 {
00131
00132 if (fnmatch(*i, name, flags) == 0)
00133 match = true;
00134 }
00135 }
00136
00137 return match;
00138 }