Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

fileutils.cc

Go to the documentation of this file.
00001 /*
00002  * Worldvisions Weaver Software:
00003  *   Copyright (C) 1997-2002 Net Integration Technologies, Inc.
00004  * 
00005  * Various useful file utilities.
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     // You're trying to make a nothing directory eh?
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     // You're probably creating the directory to write to it? Maybe this should
00036     // look for R_OK&X_OK instead of X_OK&W_OK...
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         /* This used to be a select(0), but really, if select() returns
00058          * false, it'll keep doing it until the end of time. If you're
00059          * going into an infinite loop, better save the CPU a bit, since
00060          * you can still find out about it with strace... */
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 // runs fnmatch against everything in patterns.  We also interpret 
00106 // CVS-style '!' patterns, which makes us very fancy.
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         // if we hit JUST a '!', reset any matches found so far.
00115         if (*i == "!") {
00116             match = false;
00117             continue;
00118         }
00119 
00120         // if we hit something that starts with '!', we unmatch anything
00121         // found so far.
00122         if (i->cstr()[0] == '!')
00123         {
00124             if (!match)
00125                 continue;   // nothing to unmatch, so why try?
00126             if (fnmatch(*i+1, name, flags) == 0)    // matches
00127                 match = false;                      // unmatch it.
00128         }
00129         else
00130         {
00131             // just a straightforward matching case.
00132             if (fnmatch(*i, name, flags) == 0)  // matches
00133                 match = true;
00134         }
00135     }
00136 
00137     return match;
00138 }

Generated on Wed Dec 15 15:08:10 2004 for WvStreams by  doxygen 1.3.9.1