]> git.lyx.org Git - lyx.git/blobdiff - src/support/filetools.C
some new (not extensive) changes, some fixes, will probably reverto to .la libs later...
[lyx.git] / src / support / filetools.C
index d511683bc5c32bc213f40a3a5b06856fd9b9182a..5f3e939d2045c38d9c371d5ba3a8dcaad0a59151 100644 (file)
 #include <config.h>
 
 #include <cctype>
+#include <fstream>
+using std::fstream;
+using std::ios;
+
 #include <utility>
 using std::make_pair;
 using std::pair;
@@ -153,15 +157,13 @@ bool IsFileReadable (string const & path)
 //      -1 error (doesn't exist, no access, anything else) 
 int IsFileWriteable (string const & path)
 {
-       FilePtr fp(path, FilePtr::update);
-       if (!fp()) {
-               if ((errno == EACCES) || (errno == EROFS)) {
-                       fp.reopen(path, FilePtr::read);
-                       if (fp()) {
-                               return 0;
-                       }
-               }
-               return -1;
+       fstream fs(path.c_str(), ios::out|ios::ate);
+       if (!fs) {
+               fs.open(path.c_str(), ios::in|ios::ate);
+               if (fs)
+                       return 0;
+               else
+                       return -1;
        }
        return 1;
 }
@@ -179,23 +181,10 @@ int IsDirWriteable (string const & path)
                             _("Could not test if directory is writeable"));
                return -1;
        } else {
-       FilePtr fp(tmpfl, FilePtr::truncate);
-       if (!fp()) {
-               if (errno == EACCES) {
-                       return 0;
-               } else { 
-                       WriteFSAlert(_("LyX Internal Error!"), 
-                                    _("Cannot open directory test file"));
-                       return -1;
-               }
-               }
-       }
-               if (remove(tmpfl.c_str())) {
-                       WriteFSAlert(_("LyX Internal Error!"), 
-                                   _("Created test file but cannot remove it?"));
-                       return -1;
+               FileInfo fi(path);
+               if (fi.writable()) return 1;
+               return 0;
        }
-       return 1;
 }
 
 
@@ -334,7 +323,22 @@ bool PutEnv(string const & envstr)
         // this leaks, but what can we do about it?
         //   Is doing a getenv() and a free() of the older value 
         //   a good idea? (JMarc)
-        int retval = putenv(const_cast<PUTENV_TYPE_ARG>((new string(envstr))->c_str()));
+       // Actually we don't have to leak...calling putenv like this
+       // should be enough: ... and this is obviously not enough if putenv
+       // does not make a copy of the string. It is also not very wise to
+       // put a string on the free store. If we have to leak we should do it
+       // like this:
+       /*
+       char * leaker = new char[envstr.length() + 1];
+       envstr.copy(leaker, envstr.length());
+       leaker[envstr.length()] = '\0';
+       int retval = putenv(const_cast<PUTENV_TYPE_ARG>(leaker));
+       */
+
+       // If putenv does not make a copy of the char const * this
+       // is very dangerous. OTOH if it does take a copy this is the
+       // best solution.
+       int retval = putenv(const_cast<PUTENV_TYPE_ARG>(envstr.c_str()));
 #else
 #ifdef HAVE_SETENV 
         string varname;
@@ -979,7 +983,7 @@ static cmdret do_popen(string const & cmd)
        // create our own popen based on fork, exec, pipe
        // of course the best would be to have a
        // pstream (process stream), with the
-       // variants ipstream, opstream and
+       // variants ipstream, opstream
        FILE * inf = popen(cmd.c_str(), "r");
        string ret;
        int c = fgetc(inf);