* FileName:

- 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
This commit is contained in:
Abdelrazak Younes 2007-11-28 09:01:49 +00:00
parent 163d6f3a11
commit f07105d0e5
24 changed files with 70 additions and 135 deletions

View 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;

View 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

View 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();
}

View 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;

View 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();
}

View 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;
}

View 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");
}

View 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;
}

View File

@ -62,7 +62,6 @@ using support::makeAbsPath;
using support::makeDisplayPath;
using support::onlyFilename;
using support::onlyPath;
using support::unlink;
namespace Alert = frontend::Alert;

View 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

View 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

View 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_);

View 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();
}
}

View 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();
}

View 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;
}

View 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());

View 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;

View 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 \

View 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();
}

View File

@ -11,63 +11,14 @@
#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

View 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(),

View 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;

View 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;
}

View File

@ -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