define renameormove() function and use it in internfile

This commit is contained in:
Jean-Francois Dockes
2010-09-27 12:59:42 +02:00
parent 2ab51e449c
commit 5b8bb28c5d
4 changed files with 145 additions and 44 deletions
+7 -35
View File
@@ -1007,49 +1007,21 @@ bool FileInterner::maybeUncompressToTemp(TempFile& temp, const string& fn,
return false;
}
// uncompressfile choses the output file name, there is good
// reason for this, but it's not nice here. Have to copy or rename
// the uncompressed file
string uncomped;
if (!uncompressfile(cnf, fn, ucmd, tmpdir, uncomped)) {
return false;
}
// uncompressfile choses the output file name, there is good
// reason for this, but it's not nice here. Have to copy or rename
// the uncompressed file.
// Hopefully the cross-dev case won't happen as we're
// probably choosing the temp names in the same dir. However...
// Unix really should have a rename-else-copy call...
if (stat(temp->filename(), &st) < 0) {
LOGERR(("FileInterner::maybeUncompressToTemp: can't stat [%s]\n",
temp->filename()));
// reason for this, but it's not nice here. Have to move, the
// uncompressed file, hopefully staying on the same dev.
string reason;
if (!renameormove(uncomped.c_str(), temp->filename(), reason)) {
LOGERR(("FileInterner::maybeUncompress: move [%s] -> [%s] "
"failed: %s\n",
uncomped.c_str(), temp->filename(), reason.c_str()));
return false;
}
struct stat st1;
if (stat(uncomped.c_str(), &st1) < 0) {
LOGERR(("FileInterner::maybeUncompressToTemp: can't stat [%s]\n",
uncomped.c_str()));
return false;
}
if (st.st_dev == st1.st_dev) {
if (rename(uncomped.c_str(), temp->filename()) < 0) {
LOGERR(("FileInterner::maybeUncompress: rename [%s] -> [%s]"
"failed, errno %d\n",
uncomped.c_str(), temp->filename(), errno));
return false;
}
} else {
string reason;
bool ret = copyfile(uncomped.c_str(), temp->filename(), reason);
if (ret == false) {
LOGERR(("FileInterner::maybeUncompress: copy [%s] -> [%s]"
"failed: %s\n",
uncomped.c_str(), temp->filename(), reason.c_str()));
return false;
}
// We let the tempdir cleanup get rid of uncomped
}
return true;
}