mirror of
https://git.lyx.org/repos/lyx.git
synced 2024-12-22 21:21:32 +00:00
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
This commit is contained in:
parent
e61bae3dbb
commit
cf7b238479
@ -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 <boost/bind.hpp>
|
||||
#include <boost/filesystem/exception.hpp>
|
||||
#include <boost/filesystem/operations.hpp>
|
||||
#include <boost/shared_ptr.hpp>
|
||||
|
||||
#include <algorithm>
|
||||
@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -28,7 +28,6 @@
|
||||
#include <boost/filesystem/operations.hpp>
|
||||
|
||||
#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<char*>(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<char*>(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<char*>(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<char*>(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();
|
||||
|
@ -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"
|
||||
|
@ -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"
|
||||
|
||||
|
@ -47,11 +47,8 @@
|
||||
#include "insets/InsetInclude.h"
|
||||
|
||||
#include "support/filetools.h"
|
||||
#include "support/fs_extras.h"
|
||||
#include "support/lyxlib.h"
|
||||
|
||||
#include <boost/bind.hpp>
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -20,7 +20,6 @@
|
||||
#include "FuncRequest.h"
|
||||
#include "LaTeXFeatures.h"
|
||||
|
||||
#include "support/fs_extras.h"
|
||||
#include "support/lstrings.h"
|
||||
|
||||
#include <algorithm>
|
||||
|
@ -39,6 +39,98 @@ using std::endl;
|
||||
|
||||
namespace fs = boost::filesystem;
|
||||
|
||||
// FIXME: merge this
|
||||
//
|
||||
#include <boost/filesystem/config.hpp>
|
||||
#include <boost/detail/workaround.hpp>
|
||||
#include <boost/throw_exception.hpp>
|
||||
|
||||
#ifdef HAVE_SYS_TYPES_H
|
||||
# include <sys/types.h>
|
||||
#endif
|
||||
#ifdef HAVE_SYS_STAT_H
|
||||
# include <sys/stat.h>
|
||||
#endif
|
||||
#include <cerrno>
|
||||
#include <fcntl.h>
|
||||
|
||||
|
||||
// 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 <windows.h>
|
||||
#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
|
||||
|
||||
|
||||
|
||||
|
@ -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
|
||||
|
@ -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 \
|
||||
|
@ -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);
|
||||
}
|
||||
|
||||
|
@ -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 <config.h>
|
||||
|
||||
#include "fs_extras.h"
|
||||
|
||||
#include <boost/filesystem/config.hpp>
|
||||
#include <boost/detail/workaround.hpp>
|
||||
#include <boost/throw_exception.hpp>
|
||||
|
||||
#ifdef HAVE_SYS_TYPES_H
|
||||
# include <sys/types.h>
|
||||
#endif
|
||||
#ifdef HAVE_SYS_STAT_H
|
||||
# include <sys/stat.h>
|
||||
#endif
|
||||
#include <cerrno>
|
||||
#include <fcntl.h>
|
||||
|
||||
|
||||
// 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 <windows.h>
|
||||
#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
|
||||
}
|
||||
|
||||
}
|
||||
}
|
@ -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 <boost/filesystem/path.hpp>
|
||||
|
||||
|
||||
namespace boost {
|
||||
namespace filesystem {
|
||||
|
||||
void copy_file(path const & source, path const & target, bool noclobber);
|
||||
|
||||
}
|
||||
}
|
@ -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"
|
||||
|
Loading…
Reference in New Issue
Block a user