From cf7b2384791c7c37c57df3a2352165405fc21c67 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Andr=C3=A9=20P=C3=B6nitz?= Date: Wed, 7 Nov 2007 19:52:11 +0000 Subject: [PATCH] more use of support::FileName. Would be nice if people tested read/write, and Bo perhaps the 'embedded' feature? git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@21492 a592a061-630c-0410-9148-cb99ea01b6c8 --- src/Buffer.cpp | 11 +-- src/EmbeddedFiles.cpp | 61 ++++++++-------- src/LyXFunc.cpp | 1 - src/VCBackend.cpp | 1 - src/buffer_funcs.cpp | 12 +--- src/insets/InsetCitation.cpp | 1 - src/support/FileName.cpp | 107 ++++++++++++++++++++++++++++ src/support/FileName.h | 3 + src/support/Makefile.am | 2 - src/support/filetools.cpp | 28 ++++---- src/support/fs_extras.cpp | 131 ----------------------------------- src/support/fs_extras.h | 20 ------ src/tex2lyx/tex2lyx.cpp | 1 - 13 files changed, 158 insertions(+), 221 deletions(-) delete mode 100644 src/support/fs_extras.cpp delete mode 100644 src/support/fs_extras.h diff --git a/src/Buffer.cpp b/src/Buffer.cpp index 34e55b1fa7..9a961e22ea 100644 --- a/src/Buffer.cpp +++ b/src/Buffer.cpp @@ -85,7 +85,6 @@ #include "support/FileFilterList.h" #include "support/filetools.h" #include "support/Forkedcall.h" -#include "support/fs_extras.h" #include "support/gzstream.h" #include "support/lyxlib.h" #include "support/os.h" @@ -98,8 +97,6 @@ #endif #include -#include -#include #include #include @@ -153,7 +150,6 @@ using support::suffixIs; namespace Alert = frontend::Alert; namespace os = support::os; -namespace fs = boost::filesystem; namespace { @@ -859,15 +855,14 @@ bool Buffer::save() const backupName = FileName(addName(lyxrc.backupdir_path, mangledName)); } - try { - fs::copy_file(encodedFilename, backupName.toFilesystemEncoding(), false); + if (fileName().copyTo(backupName, false)) { madeBackup = true; - } catch (fs::filesystem_error const & fe) { + } else { Alert::error(_("Backup failure"), bformat(_("Cannot create backup file %1$s.\n" "Please check whether the directory exists and is writeable."), from_utf8(backupName.absFilename()))); - LYXERR(Debug::DEBUG) << "Fs error: " << fe.what() << endl; + //LYXERR(Debug::DEBUG) << "Fs error: " << fe.what() << endl; } } diff --git a/src/EmbeddedFiles.cpp b/src/EmbeddedFiles.cpp index bc7ab0b100..abc9eaf833 100644 --- a/src/EmbeddedFiles.cpp +++ b/src/EmbeddedFiles.cpp @@ -28,7 +28,6 @@ #include #include "support/filetools.h" -#include "support/fs_extras.h" #include "support/convert.h" #include "support/lyxlib.h" #include "support/lstrings.h" @@ -152,20 +151,20 @@ bool EmbeddedFile::extract(Buffer const * buf) const return true; } // copy file - try { - // need to make directory? - string path = support::onlyPath(ext_file); - if (!fs::is_directory(path)) - makedir(const_cast(path.c_str()), 0755); - fs::copy_file(emb_file, ext_file, false); + + // need to make directory? + string path = support::onlyPath(ext_file); + if (!fs::is_directory(path)) + makedir(const_cast(path.c_str()), 0755); + FileName emb(emb_file); + FileName ext(ext_file); + if (emb.copyTo(ext, false)) return true; - } catch (fs::filesystem_error const & fe) { - Alert::error(_("Copy file failure"), - bformat(_("Cannot copy file %1$s to %2$s.\n" - "Please check whether the directory exists and is writeable."), - from_utf8(emb_file), from_utf8(ext_file))); - LYXERR(Debug::DEBUG) << "Fs error: " << fe.what() << endl; - } + Alert::error(_("Copy file failure"), + bformat(_("Cannot copy file %1$s to %2$s.\n" + "Please check whether the directory exists and is writeable."), + from_utf8(emb_file), from_utf8(ext_file))); + //LYXERR(Debug::DEBUG) << "Fs error: " << fe.what() << endl; return false; } @@ -194,20 +193,19 @@ bool EmbeddedFile::updateFromExternalFile(Buffer const * buf) const return true; } // copy file - try { - // need to make directory? - string path = support::onlyPath(emb_file); - if (!fs::is_directory(path)) - makedir(const_cast(path.c_str()), 0755); - fs::copy_file(ext_file, emb_file, false); + FileName emb(emb_file); + FileName ext(ext_file); + // need to make directory? + string path = support::onlyPath(emb_file); + if (!fs::is_directory(path)) + makedir(const_cast(path.c_str()), 0755); + if (ext.copyTo(emb, false)) return true; - } catch (fs::filesystem_error const & fe) { - Alert::error(_("Copy file failure"), - bformat(_("Cannot copy file %1$s to %2$s.\n" - "Please check whether the directory exists and is writeable."), - from_utf8(ext_file), from_utf8(emb_file))); - LYXERR(Debug::DEBUG) << "Fs error: " << fe.what() << endl; - } + Alert::error(_("Copy file failure"), + bformat(_("Cannot copy file %1$s to %2$s.\n" + "Please check whether the directory exists and is writeable."), + from_utf8(ext_file), from_utf8(emb_file))); + //LYXERR(Debug::DEBUG) << "Fs error: " << fe.what() << endl; return false; } @@ -304,20 +302,19 @@ bool EmbeddedFiles::writeFile(DocFileName const & filename) ::zipFiles(zipfile.toFilesystemEncoding(), filenames); // copy file back - try { - fs::copy_file(zipfile.toFilesystemEncoding(), filename.toFilesystemEncoding(), false); - } catch (fs::filesystem_error const & fe) { + if (!zipfile.copyTo(filename, false)) { Alert::error(_("Save failure"), bformat(_("Cannot create file %1$s.\n" "Please check whether the directory exists and is writeable."), from_utf8(filename.absFilename()))); - LYXERR(Debug::DEBUG) << "Fs error: " << fe.what() << endl; + //LYXERR(Debug::DEBUG) << "Fs error: " << fe.what() << endl; } return true; } -EmbeddedFiles::EmbeddedFileList::const_iterator EmbeddedFiles::find(std::string filename) const +EmbeddedFiles::EmbeddedFileList::const_iterator +EmbeddedFiles::find(std::string filename) const { EmbeddedFileList::const_iterator it = file_list_.begin(); EmbeddedFileList::const_iterator it_end = file_list_.end(); diff --git a/src/LyXFunc.cpp b/src/LyXFunc.cpp index 89bcdf1728..baeebe77c3 100644 --- a/src/LyXFunc.cpp +++ b/src/LyXFunc.cpp @@ -88,7 +88,6 @@ #include "support/environment.h" #include "support/FileFilterList.h" #include "support/filetools.h" -#include "support/fs_extras.h" #include "support/lstrings.h" #include "support/Path.h" #include "support/Package.h" diff --git a/src/VCBackend.cpp b/src/VCBackend.cpp index a405d2cf29..3527fb2eee 100644 --- a/src/VCBackend.cpp +++ b/src/VCBackend.cpp @@ -16,7 +16,6 @@ #include "support/Path.h" #include "support/filetools.h" -#include "support/fs_extras.h" #include "support/lstrings.h" #include "support/Systemcall.h" diff --git a/src/buffer_funcs.cpp b/src/buffer_funcs.cpp index c29fe05439..7aab6ba0e0 100644 --- a/src/buffer_funcs.cpp +++ b/src/buffer_funcs.cpp @@ -47,11 +47,8 @@ #include "insets/InsetInclude.h" #include "support/filetools.h" -#include "support/fs_extras.h" #include "support/lyxlib.h" -#include - using std::min; using std::string; @@ -216,19 +213,16 @@ depth_type getItemDepth(ParIterator const & it) Paragraph & prev_par = *prev_it; depth_type const prev_depth = getDepth(prev_it); if (labeltype == prev_par.layout()->labeltype) { - if (prev_depth < min_depth) { + if (prev_depth < min_depth) return prev_par.itemdepth + 1; - } - else if (prev_depth == min_depth) { + if (prev_depth == min_depth) return prev_par.itemdepth; - } } min_depth = std::min(min_depth, prev_depth); // small optimization: if we are at depth 0, we won't // find anything else - if (prev_depth == 0) { + if (prev_depth == 0) return 0; - } } } diff --git a/src/insets/InsetCitation.cpp b/src/insets/InsetCitation.cpp index 69367f6afb..fb2601d836 100644 --- a/src/insets/InsetCitation.cpp +++ b/src/insets/InsetCitation.cpp @@ -20,7 +20,6 @@ #include "FuncRequest.h" #include "LaTeXFeatures.h" -#include "support/fs_extras.h" #include "support/lstrings.h" #include diff --git a/src/support/FileName.cpp b/src/support/FileName.cpp index 4d54593682..ba2a653ecd 100644 --- a/src/support/FileName.cpp +++ b/src/support/FileName.cpp @@ -39,6 +39,98 @@ using std::endl; namespace fs = boost::filesystem; +// FIXME: merge this +// +#include +#include +#include + +#ifdef HAVE_SYS_TYPES_H +# include +#endif +#ifdef HAVE_SYS_STAT_H +# include +#endif +#include +#include + + +// BOOST_POSIX or BOOST_WINDOWS specify which API to use. +# if !defined( BOOST_WINDOWS ) && !defined( BOOST_POSIX ) +# if defined(_WIN32) || defined(__WIN32__) || defined(WIN32) || defined(__CYGWIN__) +# define BOOST_WINDOWS +# else +# define BOOST_POSIX +# endif +# endif + +#if defined (BOOST_WINDOWS) +# define WIN32_LEAN_AND_MEAN +# include +#endif + + +static bool copy_file(std::string const & source, std::string const & target, bool noclobber) +{ + +#ifdef BOOST_POSIX + int const infile = ::open(source.c_str(), O_RDONLY); + if (infile == -1) + return false; + + struct stat source_stat; + int const ret = ::fstat(infile, &source_stat); + if (ret == -1) { + //int err = errno; + ::close(infile); + } + + int const flags = O_WRONLY | O_CREAT | (noclobber ? O_EXCL : O_TRUNC); + + int const outfile = ::open(target.c_str(), flags, source_stat.st_mode); + if (outfile == -1) { + int err = errno; + ::close(infile); + return false; + } + + std::size_t const buf_sz = 32768; + char buf[buf_sz]; + ssize_t in = -1; + ssize_t out = -1; + + while (true) { + in = ::read(infile, buf, buf_sz); + if (in == -1) { + break; + } else if (in == 0) { + break; + } else { + out = ::write(outfile, buf, in); + if (out == -1) { + break; + } + } + } + + int err = errno; + + ::close(infile); + ::close(outfile); + + if (in == -1 || out == -1) + return false; +#endif + +#ifdef BOOST_WINDOWS + if (::CopyFile(source.c_str(), target.c_str(), noclobber) == 0) { + // CopyFile is probably not setting errno so this is most + // likely wrong. + return false; + } +#endif +} + namespace lyx { namespace support { @@ -76,6 +168,18 @@ void FileName::erase() } +bool FileName::copyTo(FileName const & name, bool noclobber) const +{ + try { + copy_file(toFilesystemEncoding(), name.toFilesystemEncoding(), noclobber); + return true; + } + catch (...) { + } + return false; +} + + string FileName::toFilesystemEncoding() const { QByteArray const encoded = QFile::encodeName(toqstr(name_)); @@ -568,3 +672,6 @@ bool operator!=(DocFileName const & lhs, DocFileName const & rhs) } // namespace support } // namespace lyx + + + diff --git a/src/support/FileName.h b/src/support/FileName.h index 188fa6bd76..a2b1c3c48d 100644 --- a/src/support/FileName.h +++ b/src/support/FileName.h @@ -72,6 +72,9 @@ 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, bool noclobber) const; + /// remove directory and all contents, returns true on success bool destroyDirectory() const; /// Creates directory. Returns true on success diff --git a/src/support/Makefile.am b/src/support/Makefile.am index 50887db40a..bb84cc85ca 100644 --- a/src/support/Makefile.am +++ b/src/support/Makefile.am @@ -50,8 +50,6 @@ liblyxsupport_la_SOURCES = \ ForkedCallQueue.h \ ForkedcallsController.cpp \ ForkedcallsController.h \ - fs_extras.cpp \ - fs_extras.h \ getcwd.cpp \ gzstream.cpp \ gzstream.h \ diff --git a/src/support/filetools.cpp b/src/support/filetools.cpp index 0d04782818..7901119a6f 100644 --- a/src/support/filetools.cpp +++ b/src/support/filetools.cpp @@ -24,7 +24,6 @@ #include "support/convert.h" #include "support/environment.h" #include "support/filetools.h" -#include "support/fs_extras.h" #include "support/lstrings.h" #include "support/lyxlib.h" #include "support/os.h" @@ -396,20 +395,19 @@ string const createBufferTmpDir() FileName const createLyXTmpDir(FileName const & deflt) { - if (!deflt.empty() && deflt.absFilename() != "/tmp") { - if (mkdir(deflt, 0777)) { - if (deflt.isDirWritable()) { - // deflt could not be created because it - // did exist already, so let's create our own - // dir inside deflt. - return createTmpDir(deflt, "lyx_tmpdir"); - } else { - // some other error occured. - return createTmpDir(FileName("/tmp"), "lyx_tmpdir"); - } - } else - return deflt; + if (deflt.empty() || deflt.absFilename() == "/tmp") + return createTmpDir(FileName("/tmp"), "lyx_tmpdir"); + + if (!mkdir(deflt, 0777)) + return deflt; + + if (deflt.isDirWritable()) { + // deflt could not be created because it + // did exist already, so let's create our own + // dir inside deflt. + return createTmpDir(deflt, "lyx_tmpdir"); } else { + // some other error occured. return createTmpDir(FileName("/tmp"), "lyx_tmpdir"); } } @@ -423,7 +421,7 @@ string const onlyPath(string const & filename) return filename; // Find last / or start of filename - string::size_type j = filename.rfind('/'); + size_t j = filename.rfind('/'); return j == string::npos ? "./" : filename.substr(0, j + 1); } diff --git a/src/support/fs_extras.cpp b/src/support/fs_extras.cpp deleted file mode 100644 index 0a3dfe0084..0000000000 --- a/src/support/fs_extras.cpp +++ /dev/null @@ -1,131 +0,0 @@ -// -*- C++ -*- -/* \file fs_extras.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 "fs_extras.h" - -#include -#include -#include - -#ifdef HAVE_SYS_TYPES_H -# include -#endif -#ifdef HAVE_SYS_STAT_H -# include -#endif -#include -#include - - -// BOOST_POSIX or BOOST_WINDOWS specify which API to use. -# if !defined( BOOST_WINDOWS ) && !defined( BOOST_POSIX ) -# if defined(_WIN32) || defined(__WIN32__) || defined(WIN32) || defined(__CYGWIN__) -# define BOOST_WINDOWS -# else -# define BOOST_POSIX -# endif -# endif - -#if defined (BOOST_WINDOWS) -# define WIN32_LEAN_AND_MEAN -# include -#endif - -namespace fs = boost::filesystem; - -namespace boost { -namespace filesystem { - -void copy_file(path const & source, path const & target, bool noclobber) -{ - -#ifdef BOOST_POSIX - int const infile = ::open(source.string().c_str(), O_RDONLY); - if (infile == -1) { - boost::throw_exception( - filesystem_path_error( - "boost::filesystem::copy_file", - source, target, - fs::lookup_errno(errno))); - } - - struct stat source_stat; - int const ret = ::fstat(infile, &source_stat); - if (ret == -1) { - int err = errno; - ::close(infile); - boost::throw_exception(filesystem_path_error( - "boost::filesystem::copy_file", - source, target, - fs::lookup_errno(err))); - } - - int const flags = O_WRONLY | O_CREAT | (noclobber ? O_EXCL : O_TRUNC); - - int const outfile = ::open(target.string().c_str(), flags, source_stat.st_mode); - if (outfile == -1) { - int err = errno; - ::close(infile); - boost::throw_exception( - filesystem_path_error( - "boost::filesystem::copy_file", - source, target, - fs::lookup_errno(err))); - } - - std::size_t const buf_sz = 32768; - char buf[buf_sz]; - ssize_t in = -1; - ssize_t out = -1; - - while (true) { - in = ::read(infile, buf, buf_sz); - if (in == -1) { - break; - } else if (in == 0) { - break; - } else { - out = ::write(outfile, buf, in); - if (out == -1) { - break; - } - } - } - - int err = errno; - - ::close(infile); - ::close(outfile); - - if (in == -1 || out == -1) - boost::throw_exception( - filesystem_path_error( - "boost::filesystem::copy_file", - source, target, - fs::lookup_errno(err))); -#endif -#ifdef BOOST_WINDOWS - if (::CopyFile(source.string().c_str(), target.string().c_str(), noclobber) == 0) { - // CopyFile is probably not setting errno so this is most - // likely wrong. - boost::throw_exception( - filesystem_path_error( - "boost::filesystem::copy_file", - source, target, - fs::lookup_error_code(errno))); - } -#endif -} - -} -} diff --git a/src/support/fs_extras.h b/src/support/fs_extras.h deleted file mode 100644 index cdb026735b..0000000000 --- a/src/support/fs_extras.h +++ /dev/null @@ -1,20 +0,0 @@ -// -*- C++ -*- -/* \file fs_extras.h - * 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 - - -namespace boost { -namespace filesystem { - -void copy_file(path const & source, path const & target, bool noclobber); - -} -} diff --git a/src/tex2lyx/tex2lyx.cpp b/src/tex2lyx/tex2lyx.cpp index f2af56d9a7..5c987b177b 100644 --- a/src/tex2lyx/tex2lyx.cpp +++ b/src/tex2lyx/tex2lyx.cpp @@ -21,7 +21,6 @@ #include "support/convert.h" #include "support/filetools.h" -#include "support/fs_extras.h" #include "support/lstrings.h" #include "support/lyxlib.h" #include "support/ExceptionMessage.h" -- 2.39.2