diff --git a/src/Buffer.cpp b/src/Buffer.cpp index af30fa6d26..82f5b8fe40 100644 --- a/src/Buffer.cpp +++ b/src/Buffer.cpp @@ -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; diff --git a/src/ConverterCache.cpp b/src/ConverterCache.cpp index a7b086a2cf..c61c8554e4 100644 --- a/src/ConverterCache.cpp +++ b/src/ConverterCache.cpp @@ -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 diff --git a/src/LaTeX.cpp b/src/LaTeX.cpp index 16e504ad2c..351f6071d3 100644 --- a/src/LaTeX.cpp +++ b/src/LaTeX.cpp @@ -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(); } diff --git a/src/LyXVC.h b/src/LyXVC.h index 9e4b7c4431..59413e2153 100644 --- a/src/LyXVC.h +++ b/src/LyXVC.h @@ -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; diff --git a/src/Mover.cpp b/src/Mover.cpp index cfe2034617..112b857f4a 100644 --- a/src/Mover.cpp +++ b/src/Mover.cpp @@ -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(); } diff --git a/src/Server.cpp b/src/Server.cpp index 26cc2033f9..2ff2fc9434 100644 --- a/src/Server.cpp +++ b/src/Server.cpp @@ -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; } diff --git a/src/ServerSocket.cpp b/src/ServerSocket.cpp index 829ff3e17b..3eed16cae4 100644 --- a/src/ServerSocket.cpp +++ b/src/ServerSocket.cpp @@ -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"); } diff --git a/src/TextClass.cpp b/src/TextClass.cpp index fc73622f6e..1294cc8f9e 100644 --- a/src/TextClass.cpp +++ b/src/TextClass.cpp @@ -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; } diff --git a/src/buffer_funcs.cpp b/src/buffer_funcs.cpp index 749e2cbe8a..a01eb78b24 100644 --- a/src/buffer_funcs.cpp +++ b/src/buffer_funcs.cpp @@ -62,7 +62,6 @@ using support::makeAbsPath; using support::makeDisplayPath; using support::onlyFilename; using support::onlyPath; -using support::unlink; namespace Alert = frontend::Alert; diff --git a/src/graphics/GraphicsCacheItem.cpp b/src/graphics/GraphicsCacheItem.cpp index 4f776033d7..882c44b46b 100644 --- a/src/graphics/GraphicsCacheItem.cpp +++ b/src/graphics/GraphicsCacheItem.cpp @@ -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 diff --git a/src/graphics/GraphicsConverter.cpp b/src/graphics/GraphicsConverter.cpp index 3ecc643168..7dd829a82c 100644 --- a/src/graphics/GraphicsConverter.cpp +++ b/src/graphics/GraphicsConverter.cpp @@ -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(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 diff --git a/src/graphics/PreviewImage.cpp b/src/graphics/PreviewImage.cpp index d99f0ee51f..ba51f85ce4 100644 --- a/src/graphics/PreviewImage.cpp +++ b/src/graphics/PreviewImage.cpp @@ -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_); diff --git a/src/graphics/PreviewLoader.cpp b/src/graphics/PreviewLoader.cpp index 5cd738f5d0..4586db8fca 100644 --- a/src/graphics/PreviewLoader.cpp +++ b/src/graphics/PreviewLoader.cpp @@ -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(); } } diff --git a/src/insets/InsetExternal.cpp b/src/insets/InsetExternal.cpp index 92092afeb8..3f7ff2a06f 100644 --- a/src/insets/InsetExternal.cpp +++ b/src/insets/InsetExternal.cpp @@ -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(); } diff --git a/src/mathed/MathExtern.cpp b/src/mathed/MathExtern.cpp index 13fd640ce9..2d7fd9d39d 100644 --- a/src/mathed/MathExtern.cpp +++ b/src/mathed/MathExtern.cpp @@ -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; } diff --git a/src/support/FileName.cpp b/src/support/FileName.cpp index 8203da462b..9439d85abb 100644 --- a/src/support/FileName.cpp +++ b/src/support/FileName.cpp @@ -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()); diff --git a/src/support/FileName.h b/src/support/FileName.h index d447f5abf3..251cfdbebb 100644 --- a/src/support/FileName.h +++ b/src/support/FileName.h @@ -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; diff --git a/src/support/Makefile.am b/src/support/Makefile.am index 85f1959df0..d3e7ac9a0f 100644 --- a/src/support/Makefile.am +++ b/src/support/Makefile.am @@ -89,7 +89,6 @@ liblyxsupport_la_SOURCES = \ userinfo.h \ unicode.cpp \ unicode.h \ - unlink.cpp \ minizip/crypt.h \ minizip/ioapi.c \ minizip/ioapi.h \ diff --git a/src/support/filetools.cpp b/src/support/filetools.cpp index 2357f38482..e009fcc7b6 100644 --- a/src/support/filetools.cpp +++ b/src/support/filetools.cpp @@ -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(); } diff --git a/src/support/getcwd.cpp b/src/support/getcwd.cpp index c3ab5f3229..7c98a28a67 100644 --- a/src/support/getcwd.cpp +++ b/src/support/getcwd.cpp @@ -11,63 +11,14 @@ #include #include "support/lyxlib.h" -#include "support/docstring.h" -#include "support/os.h" - -#include - -#include - -#ifdef HAVE_UNISTD_H -# include -#endif - -#ifdef _WIN32 -# include -#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 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 diff --git a/src/support/lyxlib.h b/src/support/lyxlib.h index 7312c294a6..f2b73eb8bb 100644 --- a/src/support/lyxlib.h +++ b/src/support/lyxlib.h @@ -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(), diff --git a/src/support/rename.cpp b/src/support/rename.cpp index ec6fca5389..4ad0c76b45 100644 --- a/src/support/rename.cpp +++ b/src/support/rename.cpp @@ -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; diff --git a/src/support/socktools.cpp b/src/support/socktools.cpp index b6c6e5ac07..d66bb0e8a2 100644 --- a/src/support/socktools.cpp +++ b/src/support/socktools.cpp @@ -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 index e67e2b1dc0..0000000000 --- a/src/support/unlink.cpp +++ /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 - -#include "support/lyxlib.h" -#include "support/FileName.h" - -#ifdef HAVE_UNISTD_H -# include -#endif - -namespace lyx { -namespace support { - -int unlink(FileName const & pathname) -{ - return ::unlink(pathname.toFilesystemEncoding().c_str()); -} - - -} // namespace support -} // namespace lyx