]> git.lyx.org Git - lyx.git/commitdiff
* FileName:
authorAbdelrazak Younes <younes@lyx.org>
Wed, 28 Nov 2007 09:01:49 +0000 (09:01 +0000)
committerAbdelrazak Younes <younes@lyx.org>
Wed, 28 Nov 2007 09:01:49 +0000 (09:01 +0000)
- removeFile(): replace unlink
- copyTo(): add 'overwrite' parameter.

* getcwd.cpp: simplify.

git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@21843 a592a061-630c-0410-9148-cb99ea01b6c8

24 files changed:
src/Buffer.cpp
src/ConverterCache.cpp
src/LaTeX.cpp
src/LyXVC.h
src/Mover.cpp
src/Server.cpp
src/ServerSocket.cpp
src/TextClass.cpp
src/buffer_funcs.cpp
src/graphics/GraphicsCacheItem.cpp
src/graphics/GraphicsConverter.cpp
src/graphics/PreviewImage.cpp
src/graphics/PreviewLoader.cpp
src/insets/InsetExternal.cpp
src/mathed/MathExtern.cpp
src/support/FileName.cpp
src/support/FileName.h
src/support/Makefile.am
src/support/filetools.cpp
src/support/getcwd.cpp
src/support/lyxlib.h
src/support/rename.cpp
src/support/socktools.cpp
src/support/unlink.cpp [deleted file]

index af30fa6d265245c2af30a91164828b5216afe51c..82f5b8fe4068debc4e8f45b358eaf5eca1ccb5f2 100644 (file)
@@ -853,7 +853,7 @@ bool Buffer::save() const
                        backupName = FileName(addName(lyxrc.backupdir_path,
                                                      mangledName));
                }
-               if (fileName().copyTo(backupName)) {
+               if (fileName().copyTo(backupName, true)) {
                        madeBackup = true;
                } else {
                        Alert::error(_("Backup failure"),
@@ -2071,7 +2071,7 @@ int AutoSaveBuffer::generateChild()
                                // filesystems unless write of tmp_ret
                                // failed so remove tmp file (if it
                                // exists)
-                               unlink(tmp_ret);
+                               tmp_ret.removeFile();
                        }
                } else {
                        failed = true;
@@ -2492,7 +2492,7 @@ bool Buffer::readFileHelper(FileName const & s)
                        return readFile(a);
                case 1:
                        // Here we delete the autosave
-                       unlink(a);
+                       a.removeFile();
                        break;
                default:
                        return false;
index a7b086a2cfa49d86ac98766a59bf2d4dd85ffee9..c61c8554e4a2b066e8576d53461719ed09faf06c 100644 (file)
@@ -135,7 +135,7 @@ void ConverterCache::Impl::readIndex()
                if (!orig_from_name.exists()) {
                        LYXERR(Debug::FILES, "Not caching file `"
                                << orig_from << "' (does not exist anymore).");
-                       support::unlink(item.cache_name);
+                       item.cache_name.removeFile();
                        continue;
                }
 
@@ -153,7 +153,7 @@ void ConverterCache::Impl::readIndex()
                                > lyxrc.converter_cache_maxage) {
                        LYXERR(Debug::FILES, "Not caching file `"
                                << orig_from << "' (too old).");
-                       support::unlink(item.cache_name);
+                       item.cache_name.removeFile();
                        continue;
                }
 
@@ -352,7 +352,7 @@ void ConverterCache::remove_all(string const & from_format,
                        if (it2->first == to_format) {
                                LYXERR(Debug::FILES, "Removing file cache item "
                                        << it1->first << ' ' << to_format);
-                               support::unlink(it2->second.cache_name);
+                               it2->second.cache_name.removeFile();
                                format_cache.erase(it2);
                                // Have to start over again since items in a
                                // map are not ordered
index 16e504ad2c742c244d3888a1ab75dd838a39b4c3..351f6071d30fe7472fea1f7fc7846042d02a2301 100644 (file)
@@ -68,7 +68,6 @@ using support::split;
 using support::subst;
 using support::suffixIs;
 using support::Systemcall;
-using support::unlink;
 using support::trim;
 
 namespace os = support::os;
@@ -148,29 +147,29 @@ void LaTeX::deleteFilesOnError() const
        // What files do we have to delete?
 
        // This will at least make latex do all the runs
-       unlink(depfile);
+       depfile.removeFile();
 
        // but the reason for the error might be in a generated file...
 
        // bibtex file
        FileName const bbl(changeExtension(file.absFilename(), ".bbl"));
-       unlink(bbl);
+       bbl.removeFile();
 
        // makeindex file
        FileName const ind(changeExtension(file.absFilename(), ".ind"));
-       unlink(ind);
+       ind.removeFile();
 
        // nomencl file
        FileName const nls(changeExtension(file.absFilename(), ".nls"));
-       unlink(nls);
+       nls.removeFile();
 
        // nomencl file (old version of the package)
        FileName const gls(changeExtension(file.absFilename(), ".gls"));
-       unlink(gls);
+       gls.removeFile();
 
        // Also remove the aux file
        FileName const aux(changeExtension(file.absFilename(), ".aux"));
-       unlink(aux);
+       aux.removeFile();
 }
 
 
index 9e4b7c44317314edf741e6d53e50f22cc70c53a0..59413e2153eaadb82d2ad2918dd70e16cb53f925 100644 (file)
@@ -82,7 +82,7 @@ public:
 
        /**
         * Generate a log file and return the filename.
-        * It is the caller's responsibility to unlink the
+        * It is the caller's responsibility to remove the
         * file after use.
         */
        const std::string getLogFile() const;
index cfe203461779474672e4b8893b43c6717496f27e..112b857f4a3d39a50b2372f79d0a194fbf3826dc 100644 (file)
@@ -88,7 +88,7 @@ bool SpecialisedMover::do_rename(support::FileName const & from, support::FileNa
 
        if (!do_copy(from, to, latex, (unsigned long int)-1))
                return false;
-       return support::unlink(from) == 0;
+       return from.removeFile();
 }
 
 
index 26cc2033f90f0e3db13ae0f85e9a31a793945e78..2ff2fc9434527877e84488a970e43b65ac0b6613 100644 (file)
@@ -65,7 +65,6 @@ using support::compare;
 using support::FileName;
 using support::rtrim;
 using support::split;
-using support::unlink;
 
 using std::endl;
 using std::string;
@@ -206,7 +205,7 @@ int LyXComm::startPipe(string const & file, bool write)
        if (fd < 0) {
                lyxerr << "LyXComm: Could not open pipe " << filename << '\n'
                       << strerror(errno) << endl;
-               unlink(filename);
+               filename.removeFile();
                return -1;
        }
 
@@ -232,7 +231,7 @@ void LyXComm::endPipe(int & fd, string const & filename, bool write)
                       << '\n' << strerror(errno) << endl;
        }
 
-       if (unlink(FileName(filename)) < 0) {
+       if (FileName(filename).removeFile() < 0) {
                lyxerr << "LyXComm: Could not remove pipe " << filename
                       << '\n' << strerror(errno) << endl;
        }
index 829ff3e17bb9d0de54b8bec27b636cf0c72c5826..3eed16cae45f47ca1934619aa23aecd21a6d8e1b 100644 (file)
@@ -84,7 +84,7 @@ ServerSocket::~ServerSocket()
                        lyxerr << "lyx: Server socket " << fd_
                               << " IO error on closing: " << strerror(errno);
        }
-       support::unlink(address_);
+       address_.removeFile();
        LYXERR(Debug::LYXSERVER, "lyx: Server socket quitting");
 }
 
index fc73622f6ee5278515ce99305b5a576640ade009..1294cc8f9e3fc2989247c3f23529f8f9954b2f6c 100644 (file)
@@ -450,7 +450,7 @@ bool TextClass::read(FileName const & filename, ReadType rt)
                error = !layout2layout(filename, tempfile);
                if (!error)
                        error = read(tempfile, rt);
-               support::unlink(tempfile);
+               tempfile.removeFile();
                return error;
        }
 
index 749e2cbe8a1c498e876d1bbd01f53258b2bcd7e6..a01eb78b24f97e14ee379c1eb48daa1be9e7a169 100644 (file)
@@ -62,7 +62,6 @@ using support::makeAbsPath;
 using support::makeDisplayPath;
 using support::onlyFilename;
 using support::onlyPath;
-using support::unlink;
 
 namespace Alert = frontend::Alert;
 
index 4f776033d788c14887d04f259d4ed4e4f6a096e7..882c44b46b8e3a5f5be5db4250c7fd5b85c80d2d 100644 (file)
@@ -34,7 +34,6 @@ using support::FileName;
 using support::makeDisplayPath;
 using support::onlyFilename;
 using support::tempName;
-using support::unlink;
 using support::unzipFile;
 
 using std::endl;
@@ -224,11 +223,11 @@ void CacheItem::Impl::reset()
 {
        zipped_ = false;
        if (!unzipped_filename_.empty())
-               unlink(unzipped_filename_);
+               unzipped_filename_.removeFile();
        unzipped_filename_.erase();
 
        if (remove_loaded_file_ && !file_to_load_.empty())
-               unlink(file_to_load_);
+               file_to_load_.removeFile();
        remove_loaded_file_ = false;
        file_to_load_.erase();
        to_.erase();
@@ -276,7 +275,7 @@ void CacheItem::Impl::imageConverted(bool success)
                setStatus(ErrorConverting);
 
                if (zipped_)
-                       unlink(unzipped_filename_);
+                       unzipped_filename_.removeFile();
 
                return;
        }
@@ -311,10 +310,10 @@ void CacheItem::Impl::imageLoaded(bool success)
 
        // Clean up after loading.
        if (zipped_)
-               unlink(unzipped_filename_);
+               unzipped_filename_.removeFile();
 
        if (remove_loaded_file_ && unzipped_filename_ != file_to_load_)
-               unlink(file_to_load_);
+               file_to_load_.removeFile();
 
        cl_.disconnect();
 
@@ -430,7 +429,7 @@ void CacheItem::Impl::convertToDisplayFormat()
 
        // Remove the temp file, we only want the name...
        // FIXME: This is unsafe!
-       unlink(to_file_base);
+       to_file_base.removeFile();
 
        // Connect a signal to this->imageConverted and pass this signal to
        // the graphics converter so that we can load the modified file
index 3ecc643168aec5bb54ec4bfe6c81b0729df2cee4..7dd829a82c4dd20c7174f107467d3248f2451da7 100644 (file)
@@ -43,7 +43,6 @@ using support::quoteName;
 using support::quote_python;
 using support::subst;
 using support::tempName;
-using support::unlink;
 
 using std::endl;
 using std::ostream;
@@ -209,10 +208,10 @@ void Converter::Impl::converted(pid_t /* pid */, int retval)
 
        finished_ = true;
        // Clean-up behind ourselves
-       unlink(script_file_);
+       script_file_.removeFile();
 
        if (retval > 0) {
-               unlink(to_file_);
+               to_file_.removeFile();
                to_file_.erase();
                finishedConversion(false);
        } else {
@@ -314,7 +313,7 @@ static void build_script(FileName const & from_file,
        static int counter = 0;
        string const tmp = "gconvert" + convert<string>(counter++);
        FileName const to_base(tempName(FileName(), tmp));
-       unlink(to_base);
+       to_base.removeFile();
 
        // Create a copy of the file in case the original name contains
        // problematic characters like ' or ". We can work around that problem
index d99f0ee51f992787281b177dd49a15ea05c6a577..ba51f85ce4b41386ff4f15a47f4c7e7784dd146c 100644 (file)
@@ -107,7 +107,7 @@ PreviewImage::Impl::Impl(PreviewImage & p, PreviewLoader & l,
 
 PreviewImage::Impl::~Impl()
 {
-       support::unlink(iloader_.filename());
+       iloader_.filename().removeFile();
 }
 
 
@@ -135,12 +135,12 @@ void PreviewImage::Impl::statusChanged()
        case ErrorLoading:
        case ErrorGeneratingPixmap:
        case ErrorUnknown:
-               //lyx::unlink(iloader_.filename());
+               //iloader_.filename().removeFile();
                ploader_.remove(snippet_);
                break;
 
        case Ready:
-               support::unlink(iloader_.filename());
+               iloader_.filename().removeFile();
                break;
        }
        ploader_.emitSignal(parent_);
index 5cd738f5d0da9ebd6cf0ebc7f352e1d14f4f91cf..4586db8fca4c0a8a04c074f98e4d32f8ce488e15 100644 (file)
@@ -393,13 +393,13 @@ void InProgress::stop() const
                lyx::support::ForkedcallsController::get().kill(pid, 0);
 
        if (!metrics_file.empty())
-               lyx::support::unlink(metrics_file);
+               metrics_file.removeFile();
 
        BitmapFile::const_iterator vit  = snippets.begin();
        BitmapFile::const_iterator vend = snippets.end();
        for (; vit != vend; ++vit) {
                if (!vit->second.empty())
-                       lyx::support::unlink(vit->second);
+                       vit->second.removeFile();
        }
 }
 
index 92092afeb89ca338975580cbf9503a44f4c06889..3f7ff2a06f427540691ae2f0d001bd8809b10a41 100644 (file)
@@ -72,7 +72,7 @@ TempName::TempName()
 {
        support::FileName const tempname(support::tempName(support::FileName(), "lyxext"));
        // FIXME: This is unsafe
-       support::unlink(tempname);
+       tempname.removeFile();
        // must have an extension for the converter code to work correctly.
        tempname_ = support::FileName(tempname.absFilename() + ".tmp");
 }
@@ -86,7 +86,7 @@ TempName::TempName(TempName const &)
 
 TempName::~TempName()
 {
-       support::unlink(tempname_);
+       tempname_.removeFile();
 }
 
 
index 13fd640ce95b160b063fd4a02c867ebfcda1c401..2d7fd9d39d890ab4055dc114b0465c8dc4a00e57 100644 (file)
@@ -54,7 +54,6 @@ using support::runCommand;
 using support::FileName;
 using support::quoteName;
 using support::tempName;
-using support::unlink;
 using support::subst;
 
 using std::auto_ptr;
@@ -1051,7 +1050,7 @@ namespace {
                lyxerr << "calling: " << cmd
                       << "\ninput: '" << data << "'" << endl;
                cmd_ret const ret = runCommand(command);
-               unlink(cas_tmpfile);
+               cas_tmpfile.removeFile();
                return ret.second;
        }
 
index 8203da462b7dc80d91cf5fd66623040e530dd1a4..9439d85abb219b7d7220bb875e049f81eac1de38 100644 (file)
@@ -130,9 +130,15 @@ void FileName::erase()
 }
 
 
-bool FileName::copyTo(FileName const & name) const
+bool FileName::copyTo(FileName const & name, bool overwrite) const
 {
-       return QFile::copy(d->fi.absoluteFilePath(), name.d->fi.absoluteFilePath());
+       if (overwrite)
+               QFile::remove(name.d->fi.absoluteFilePath());
+       bool success = QFile::copy(d->fi.absoluteFilePath(), name.d->fi.absoluteFilePath());
+       if (!success)
+               lyxerr << "FileName::copyTo(): Could not copy file "
+                       << *this << " to " << name << endl;
+       return success;
 }
 
 
@@ -219,7 +225,7 @@ bool FileName::isDirWritable() const
        if (tmpfl.empty())
                return false;
 
-       unlink(tmpfl);
+       tmpfl.removeFile();
        return true;
 }
 
@@ -236,6 +242,16 @@ std::time_t FileName::lastModified() const
 }
 
 
+bool FileName::removeFile() const
+{
+       bool const success = QFile::remove(d->fi.absoluteFilePath());
+       if (!success)
+               lyxerr << "FileName::removeFile(): Could not delete file "
+                       << *this << "." << endl;
+       return success;
+}
+
+
 static bool rmdir(QFileInfo const & fi)
 {
        QDir dir(fi.absoluteFilePath());
index d447f5abf35c066123a282e8269da6dffad261b6..251cfdbebb74cc340f1fe7d1078233a7ebc1c6ad 100644 (file)
@@ -84,8 +84,15 @@ public:
        /// return true when file/directory is writable (write test file)
        bool isDirWritable() const;
        
-       /// return true when file/directory is writable (write test file)
-       bool copyTo(FileName const & target) const;
+       /// copy a file
+       /// \return true when file/directory is writable (write test file)
+       /// \param overwrite: set to true if we should erase the \c target 
+       /// file if it exists,
+       bool copyTo(FileName const & target, bool overwrite = false) const;
+
+       /// remove pointed file.
+       /// \retrun true on success.
+       bool removeFile() const;
 
        /// remove directory and all contents, returns true on success
        bool destroyDirectory() const;
index 85f1959df02acc30777d0409384a362843d0f871..d3e7ac9a0fd37dc9135b1ce63bf4906c3f08f9a7 100644 (file)
@@ -89,7 +89,6 @@ liblyxsupport_la_SOURCES = \
        userinfo.h \
        unicode.cpp \
        unicode.h \
-       unlink.cpp \
        minizip/crypt.h \
        minizip/ioapi.c \
        minizip/ioapi.h \
index 2357f3848229210f039f36982336bd1c42175132..e009fcc7b6d106c586bedc3847fc62581ceda3dc 100644 (file)
@@ -328,7 +328,7 @@ static FileName createTmpDir(FileName const & tempdir, string const & mask)
        // stays unique. So we have to delete it before we can create
        // a dir with the same name. Note also that we are not thread
        // safe because of the gap between unlink and mkdir. (Lgb)
-       unlink(tmpfl);
+       tmpfl.removeFile();
 
        if (tmpfl.empty() || mkdir(tmpfl, 0700)) {
                lyxerr << "LyX could not create the temporary directory '"
@@ -855,7 +855,7 @@ void removeAutosaveFile(string const & filename)
        a += '#';
        FileName const autosave(a);
        if (autosave.exists())
-               unlink(autosave);
+               autosave.removeFile();
 }
 
 
@@ -865,7 +865,7 @@ void readBB_lyxerrMessage(FileName const & file, bool & zipped,
        LYXERR(Debug::GRAPHICS, "[readBB_from_PSFile] " << message);
        // FIXME: Why is this func deleting a file? (Lgb)
        if (zipped)
-               unlink(file);
+               file.removeFile();
 }
 
 
index c3ab5f3229ef8f82a50e605254564e352bc79dad..7c98a28a671d8796b0dfc10f02075820417e8234 100644 (file)
 #include <config.h>
 
 #include "support/lyxlib.h"
-#include "support/docstring.h"
-#include "support/os.h"
-
-#include <boost/scoped_array.hpp>
-
-#include <cerrno>
-
-#ifdef HAVE_UNISTD_H
-# include <unistd.h>
-#endif
-
-#ifdef _WIN32
-# include <windows.h>
-#endif
-
-using boost::scoped_array;
-
-using std::string;
-
 
 namespace lyx {
 namespace support {
 
-namespace {
-
-inline
-char * l_getcwd(char * buffer, size_t size)
-{
-#ifdef _WIN32
-       GetCurrentDirectory(size, buffer);
-       return buffer;
-#else
-       return ::getcwd(buffer, size);
-#endif
-}
-
-} // namespace anon
-
-
 // Returns current working directory
 FileName const getcwd()
 {
-       int n = 256;    // Assume path is less than 256 chars
-       char * err;
-       scoped_array<char> tbuf(new char[n]);
-
-       // Safe. Hopefully all getcwds behave this way!
-       while (((err = l_getcwd(tbuf.get(), n)) == 0) && (errno == ERANGE)) {
-               // Buffer too small, double the buffersize and try again
-               n *= 2;
-               tbuf.reset(new char[n]);
-       }
-
-       string result;
-       if (err)
-               result = tbuf.get();
-       return FileName(os::internal_path(to_utf8(from_filesystem8bit(result))));
+       return FileName(".");
 }
 
 } // namespace support
index 7312c294a6bed9232f004ba9e406f5193065e609..f2b73eb8bb798c6353cd890a2979b17237fa46b2 100644 (file)
@@ -50,8 +50,6 @@ int mkdir(FileName const & pathname, unsigned long int mode);
 /// intermediate directories if necessary
 /// \ret return 0 if the directory is successfully created
 int makedir(char * pathname, unsigned long int mode=0755);
-/// unlink the given file
-int unlink(FileName const & file);
 /// (securely) create a temporary file in the given dir with the given mask
 /// \p mask must be in filesystem encoding
 FileName const tempName(FileName const & dir = FileName(),
index ec6fca53898e38e4c00c490450417cee3cd398a5..4ad0c76b45f980d027dccfe3fd6eea6d1dc4d585 100644 (file)
@@ -27,7 +27,7 @@ bool rename(FileName const & from, FileName const & to)
 {
        if (::rename(from.toFilesystemEncoding().c_str(), to.toFilesystemEncoding().c_str()) == -1) {
                if (copy(from, to)) {
-                       unlink(from);
+                       from.removeFile();
                        return true;
                } else
                        return false;
index b6c6e5ac07824ccb2b6eede373dc5737bdf557da..d66bb0e8a2598d0f301a73304bddd8cd0ca3d75d 100644 (file)
@@ -114,7 +114,7 @@ int listen(FileName const & name, int queue)
                LYXERR(Debug::ANY, "lyx: Could not bind address '" << name.absFilename()
                       << "' to socket descriptor: " << strerror(errno));
                ::close(fd);
-               unlink(name);
+               name.removeFile();
                return -1;
        }
 
@@ -127,7 +127,7 @@ int listen(FileName const & name, int queue)
                LYXERR(Debug::ANY, "lyx: Could not put socket in 'listen' state: "
                       << strerror(errno));
                ::close(fd);
-               unlink(name);
+               name.removeFile();
                return -1;
        }
 
diff --git a/src/support/unlink.cpp b/src/support/unlink.cpp
deleted file mode 100644 (file)
index e67e2b1..0000000
+++ /dev/null
@@ -1,30 +0,0 @@
-/**
- * \file unlink.cpp
- * This file is part of LyX, the document processor.
- * Licence details can be found in the file COPYING.
- *
- * \author Lars Gullik Bjønnes
- *
- * Full author contact details are available in file CREDITS.
- */
-
-#include <config.h>
-
-#include "support/lyxlib.h"
-#include "support/FileName.h"
-
-#ifdef HAVE_UNISTD_H
-# include <unistd.h>
-#endif
-
-namespace lyx {
-namespace support {
-
-int unlink(FileName const & pathname)
-{
-       return ::unlink(pathname.toFilesystemEncoding().c_str());
-}
-
-
-} // namespace support
-} // namespace lyx