]> git.lyx.org Git - lyx.git/blobdiff - src/support/copy.C
Detect mode_t for safe use of chmod, and for scons/msvc
[lyx.git] / src / support / copy.C
index b39c771b63003e0b6d834a5083b5d851ac9a02b9..3b6d328bd7e7a491ed7be39404a5c45bc6a8a2a4 100644 (file)
 
 #include "support/lyxlib.h"
 
+#ifdef HAVE_SYS_STAT_H
+# include <sys/stat.h>
+#endif
+#ifdef HAVE_SYS_TYPES_H
+# include <sys/types.h>
+#endif
+
+
+namespace lyx {
+
 
 using std::ifstream;
 using std::ofstream;
@@ -21,17 +31,42 @@ using std::ios;
 using std::string;
 
 
-bool lyx::support::copy(string const & from, string const & to)
+bool lyx::support::chmod(string const & file, unsigned long int mode)
 {
-       ifstream ifs(from.c_str(), ios::binary);
+#if defined (HAVE_CHMOD) && defined (HAVE_MODE_T)
+       if (::chmod(file.c_str(), mode_t(mode)) != 0)
+               return false;
+#else
+# ifdef WITH_WARNINGS
+#  warning "File permissions are ignored on this system."
+# endif
+#endif
+       return true;
+}
+
+
+bool lyx::support::copy(string const & from, string const & to, unsigned long int mode)
+{
+       ifstream ifs(from.c_str(), ios::binary | ios::in);
        if (!ifs)
                return false;
-       ofstream ofs(to.c_str(),
-                    ios::binary | ios::out | ios::trunc);
+
+       if (mode != (unsigned long int)-1) {
+               ofstream ofs(to.c_str(), ios::binary | ios::out | ios::trunc);
+               if (!ofs)
+                       return false;
+               ofs.close();
+               if (!support::chmod(to, mode))
+                       return false;
+       }
+
+       ofstream ofs(to.c_str(), ios::binary | ios::out | ios::trunc);
        if (!ofs)
                return false;
+
        ofs << ifs.rdbuf();
-       if (ofs.good())
-               return true;
-       return false;
+       return ofs.good();
 }
+
+
+} // namespace lyx