2013-01-29 04:07:54 +01:00
|
|
|
/*****************************************************************************
|
|
|
|
* Author: Valient Gough <vgough@pobox.com>
|
|
|
|
*
|
|
|
|
*****************************************************************************
|
|
|
|
* Copyright (c) 2003-2004, Valient Gough
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify it
|
|
|
|
* under the terms of the GNU Lesser General Public License as published by the
|
|
|
|
* Free Software Foundation, either version 3 of the License, or (at your
|
2013-10-20 00:35:26 +02:00
|
|
|
* option) any later version.
|
2013-01-29 04:07:54 +01:00
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful, but WITHOUT
|
|
|
|
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
|
|
|
|
* for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "fs/encfs.h"
|
|
|
|
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <cerrno>
|
|
|
|
#include <cstdio>
|
|
|
|
#include <cstdlib>
|
|
|
|
#include <unistd.h>
|
|
|
|
#ifdef linux
|
|
|
|
#include <sys/fsuid.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include <cstring>
|
|
|
|
|
|
|
|
#include "base/Error.h"
|
|
|
|
#include "base/Mutex.h"
|
|
|
|
#include "fs/Context.h"
|
|
|
|
#include "fs/DirNode.h"
|
|
|
|
#include "fs/FileUtils.h"
|
2013-03-05 07:29:58 +01:00
|
|
|
#include "fs/fsconfig.pb.h"
|
2013-01-29 04:07:54 +01:00
|
|
|
|
|
|
|
#include <glog/logging.h>
|
|
|
|
|
|
|
|
#include <iostream>
|
|
|
|
|
2013-03-05 07:39:51 +01:00
|
|
|
using std::list;
|
|
|
|
using std::string;
|
2013-03-05 07:29:58 +01:00
|
|
|
|
|
|
|
namespace encfs {
|
2013-01-29 04:07:54 +01:00
|
|
|
|
2013-10-20 00:35:26 +02:00
|
|
|
class DirDeleter {
|
|
|
|
public:
|
|
|
|
void operator()(DIR *d) { ::closedir(d); }
|
2013-01-29 04:07:54 +01:00
|
|
|
};
|
|
|
|
|
2013-10-20 00:35:26 +02:00
|
|
|
DirTraverse::DirTraverse(const shared_ptr<DIR> &_dirPtr, uint64_t _iv,
|
|
|
|
const shared_ptr<NameIO> &_naming)
|
|
|
|
: dir(_dirPtr), iv(_iv), naming(_naming) {}
|
2013-01-29 04:07:54 +01:00
|
|
|
|
|
|
|
DirTraverse::DirTraverse(const DirTraverse &src)
|
2013-10-20 00:35:26 +02:00
|
|
|
: dir(src.dir), iv(src.iv), naming(src.naming) {}
|
2013-01-29 04:07:54 +01:00
|
|
|
|
2013-10-20 00:35:26 +02:00
|
|
|
DirTraverse &DirTraverse::operator=(const DirTraverse &src) {
|
2013-01-29 04:07:54 +01:00
|
|
|
dir = src.dir;
|
|
|
|
iv = src.iv;
|
|
|
|
naming = src.naming;
|
|
|
|
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2013-10-20 00:35:26 +02:00
|
|
|
DirTraverse::~DirTraverse() {
|
2013-01-29 04:07:54 +01:00
|
|
|
dir.reset();
|
|
|
|
iv = 0;
|
|
|
|
naming.reset();
|
|
|
|
}
|
|
|
|
|
2013-10-20 00:35:26 +02:00
|
|
|
static bool _nextName(struct dirent *&de, const shared_ptr<DIR> &dir,
|
|
|
|
int *fileType, ino_t *inode) {
|
|
|
|
de = ::readdir(dir.get());
|
2013-01-29 04:07:54 +01:00
|
|
|
|
2013-10-20 00:35:26 +02:00
|
|
|
if (de) {
|
|
|
|
if (fileType) {
|
2013-01-29 04:07:54 +01:00
|
|
|
#if defined(_DIRENT_HAVE_D_TYPE) || defined(__FreeBSD__)
|
|
|
|
*fileType = de->d_type;
|
|
|
|
#else
|
|
|
|
#warning "struct dirent.d_type not supported"
|
|
|
|
*fileType = 0;
|
|
|
|
#endif
|
|
|
|
}
|
2013-10-20 00:35:26 +02:00
|
|
|
if (inode) *inode = de->d_ino;
|
2013-01-29 04:07:54 +01:00
|
|
|
return true;
|
2013-10-20 00:35:26 +02:00
|
|
|
} else {
|
|
|
|
if (fileType) *fileType = 0;
|
2013-01-29 04:07:54 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-10-20 00:35:26 +02:00
|
|
|
std::string DirTraverse::nextPlaintextName(int *fileType, ino_t *inode) {
|
|
|
|
struct dirent *de = 0;
|
|
|
|
while (_nextName(de, dir, fileType, inode)) {
|
|
|
|
try {
|
2013-01-29 04:07:54 +01:00
|
|
|
uint64_t localIv = iv;
|
2013-10-20 00:35:26 +02:00
|
|
|
return naming->decodePath(de->d_name, &localIv);
|
|
|
|
}
|
|
|
|
catch (Error &ex) {
|
2013-01-29 04:07:54 +01:00
|
|
|
// .. .problem decoding, ignore it and continue on to next name..
|
2013-10-20 00:35:26 +02:00
|
|
|
VLOG(1) << "error decoding filename " << de->d_name << " : " << ex.what();
|
2013-01-29 04:07:54 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return string();
|
|
|
|
}
|
|
|
|
|
2013-10-20 00:35:26 +02:00
|
|
|
std::string DirTraverse::nextInvalid() {
|
|
|
|
struct dirent *de = 0;
|
2013-01-29 04:07:54 +01:00
|
|
|
// find the first name which produces a decoding error...
|
2013-10-20 00:35:26 +02:00
|
|
|
while (_nextName(de, dir, (int *)0, (ino_t *)0)) {
|
|
|
|
try {
|
2013-01-29 04:07:54 +01:00
|
|
|
uint64_t localIv = iv;
|
2013-10-20 00:35:26 +02:00
|
|
|
naming->decodePath(de->d_name, &localIv);
|
2013-01-29 04:07:54 +01:00
|
|
|
continue;
|
2013-10-20 00:35:26 +02:00
|
|
|
}
|
|
|
|
catch (Error &ex) {
|
|
|
|
return string(de->d_name);
|
2013-01-29 04:07:54 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return string();
|
|
|
|
}
|
|
|
|
|
2013-10-20 00:35:26 +02:00
|
|
|
struct RenameEl {
|
2013-01-29 04:07:54 +01:00
|
|
|
// ciphertext names
|
|
|
|
string oldCName;
|
2013-10-20 00:35:26 +02:00
|
|
|
string newCName; // intermediate name (not final cname)
|
2013-01-29 04:07:54 +01:00
|
|
|
|
|
|
|
// plaintext names
|
|
|
|
string oldPName;
|
|
|
|
string newPName;
|
|
|
|
|
|
|
|
bool isDirectory;
|
|
|
|
};
|
|
|
|
|
2013-10-20 00:35:26 +02:00
|
|
|
class RenameOp {
|
|
|
|
private:
|
2013-01-29 04:07:54 +01:00
|
|
|
DirNode *dn;
|
2013-10-20 00:35:26 +02:00
|
|
|
shared_ptr<list<RenameEl> > renameList;
|
2013-01-29 04:07:54 +01:00
|
|
|
list<RenameEl>::const_iterator last;
|
|
|
|
|
2013-10-20 00:35:26 +02:00
|
|
|
public:
|
|
|
|
RenameOp(DirNode *_dn, const shared_ptr<list<RenameEl> > &_renameList)
|
|
|
|
: dn(_dn), renameList(_renameList) {
|
2013-01-29 04:07:54 +01:00
|
|
|
last = renameList->begin();
|
|
|
|
}
|
|
|
|
|
|
|
|
RenameOp(const RenameOp &src)
|
2013-10-20 00:35:26 +02:00
|
|
|
: dn(src.dn), renameList(src.renameList), last(src.last) {}
|
2013-01-29 04:07:54 +01:00
|
|
|
|
|
|
|
~RenameOp();
|
|
|
|
|
2013-10-20 00:35:26 +02:00
|
|
|
operator bool() const { return renameList; }
|
2013-01-29 04:07:54 +01:00
|
|
|
|
|
|
|
bool apply();
|
|
|
|
void undo();
|
|
|
|
};
|
|
|
|
|
2013-10-20 00:35:26 +02:00
|
|
|
RenameOp::~RenameOp() {
|
|
|
|
if (renameList) {
|
2013-01-29 04:07:54 +01:00
|
|
|
// got a bunch of decoded filenames sitting in memory.. do a little
|
|
|
|
// cleanup before leaving..
|
|
|
|
list<RenameEl>::iterator it;
|
2013-10-20 00:35:26 +02:00
|
|
|
for (it = renameList->begin(); it != renameList->end(); ++it) {
|
|
|
|
it->oldPName.assign(it->oldPName.size(), ' ');
|
|
|
|
it->newPName.assign(it->newPName.size(), ' ');
|
2013-01-29 04:07:54 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-10-20 00:35:26 +02:00
|
|
|
bool RenameOp::apply() {
|
|
|
|
try {
|
|
|
|
while (last != renameList->end()) {
|
2013-01-29 04:07:54 +01:00
|
|
|
// backing store rename.
|
|
|
|
VLOG(2) << "renaming " << last->oldCName << "-> " << last->newCName;
|
|
|
|
|
|
|
|
struct stat st;
|
|
|
|
bool preserve_mtime = ::stat(last->oldCName.c_str(), &st) == 0;
|
|
|
|
|
|
|
|
// internal node rename..
|
2013-10-20 00:35:26 +02:00
|
|
|
dn->renameNode(last->oldPName.c_str(), last->newPName.c_str());
|
2013-01-29 04:07:54 +01:00
|
|
|
|
|
|
|
// rename on disk..
|
2013-10-20 00:35:26 +02:00
|
|
|
if (::rename(last->oldCName.c_str(), last->newCName.c_str()) == -1) {
|
|
|
|
LOG(WARNING) << "Error renaming " << last->oldCName << ": "
|
|
|
|
<< strerror(errno);
|
|
|
|
dn->renameNode(last->newPName.c_str(), last->oldPName.c_str(), false);
|
2013-01-29 04:07:54 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-10-20 00:35:26 +02:00
|
|
|
if (preserve_mtime) {
|
2013-01-29 04:07:54 +01:00
|
|
|
struct utimbuf ut;
|
|
|
|
ut.actime = st.st_atime;
|
|
|
|
ut.modtime = st.st_mtime;
|
|
|
|
::utime(last->newCName.c_str(), &ut);
|
|
|
|
}
|
|
|
|
|
|
|
|
++last;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
2013-10-20 00:35:26 +02:00
|
|
|
}
|
|
|
|
catch (Error &err) {
|
2013-01-29 04:07:54 +01:00
|
|
|
LOG(WARNING) << "caught error in rename application: " << err.what();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-10-20 00:35:26 +02:00
|
|
|
void RenameOp::undo() {
|
2013-01-29 04:07:54 +01:00
|
|
|
VLOG(1) << "in undoRename";
|
|
|
|
|
2013-10-20 00:35:26 +02:00
|
|
|
if (last == renameList->begin()) {
|
2013-01-29 04:07:54 +01:00
|
|
|
VLOG(1) << "nothing to undo";
|
2013-10-20 00:35:26 +02:00
|
|
|
return; // nothing to undo
|
2013-01-29 04:07:54 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// list has to be processed backwards, otherwise we may rename
|
|
|
|
// directories and directory contents in the wrong order!
|
|
|
|
int undoCount = 0;
|
|
|
|
int errorCount = 0;
|
|
|
|
list<RenameEl>::const_iterator it = last;
|
|
|
|
|
2013-10-20 00:35:26 +02:00
|
|
|
while (it != renameList->begin()) {
|
2013-01-29 04:07:54 +01:00
|
|
|
--it;
|
|
|
|
|
|
|
|
VLOG(1) << "undo: renaming " << it->newCName << " -> " << it->oldCName;
|
|
|
|
|
2013-10-20 00:35:26 +02:00
|
|
|
::rename(it->newCName.c_str(), it->oldCName.c_str());
|
|
|
|
try {
|
|
|
|
dn->renameNode(it->newPName.c_str(), it->oldPName.c_str(), false);
|
|
|
|
}
|
|
|
|
catch (Error &err) {
|
2013-01-29 04:07:54 +01:00
|
|
|
if (++errorCount == 1)
|
|
|
|
LOG(WARNING) << "error in rename und: " << err.what();
|
|
|
|
// continue on anyway...
|
|
|
|
}
|
|
|
|
++undoCount;
|
|
|
|
};
|
|
|
|
|
|
|
|
LOG(WARNING) << "Undo rename count: " << undoCount;
|
|
|
|
}
|
|
|
|
|
2013-10-20 00:35:26 +02:00
|
|
|
DirNode::DirNode(EncFS_Context *_ctx, const string &sourceDir,
|
|
|
|
const FSConfigPtr &_config) {
|
|
|
|
Lock _lock(mutex);
|
2013-01-29 04:07:54 +01:00
|
|
|
|
|
|
|
ctx = _ctx;
|
|
|
|
rootDir = sourceDir;
|
|
|
|
fsConfig = _config;
|
|
|
|
|
|
|
|
// make sure rootDir ends in '/', so that we can form a path by appending
|
|
|
|
// the rest..
|
2013-10-20 00:35:26 +02:00
|
|
|
if (rootDir[rootDir.length() - 1] != '/') rootDir.append(1, '/');
|
2013-01-29 04:07:54 +01:00
|
|
|
|
|
|
|
naming = fsConfig->nameCoding;
|
|
|
|
}
|
|
|
|
|
2013-10-20 00:35:26 +02:00
|
|
|
DirNode::~DirNode() {}
|
2013-01-29 04:07:54 +01:00
|
|
|
|
2013-10-20 00:35:26 +02:00
|
|
|
bool DirNode::hasDirectoryNameDependency() const {
|
2013-01-29 04:07:54 +01:00
|
|
|
return naming ? naming->getChainedNameIV() : false;
|
|
|
|
}
|
|
|
|
|
2013-10-20 00:35:26 +02:00
|
|
|
string DirNode::rootDirectory() {
|
2013-01-29 04:07:54 +01:00
|
|
|
// don't update last access here, otherwise 'du' would cause lastAccess to
|
|
|
|
// be reset.
|
|
|
|
// chop off '/' terminator from root dir.
|
2013-10-20 00:35:26 +02:00
|
|
|
return string(rootDir, 0, rootDir.length() - 1);
|
2013-01-29 04:07:54 +01:00
|
|
|
}
|
|
|
|
|
2013-10-20 00:35:26 +02:00
|
|
|
string DirNode::cipherPath(const char *plaintextPath) {
|
|
|
|
return rootDir + naming->encodePath(plaintextPath);
|
2013-01-29 04:07:54 +01:00
|
|
|
}
|
|
|
|
|
2013-10-20 00:35:26 +02:00
|
|
|
string DirNode::cipherPathWithoutRoot(const char *plaintextPath) {
|
|
|
|
return naming->encodePath(plaintextPath);
|
2013-01-29 04:07:54 +01:00
|
|
|
}
|
|
|
|
|
2013-10-20 00:35:26 +02:00
|
|
|
string DirNode::plainPath(const char *cipherPath_) {
|
|
|
|
try {
|
|
|
|
if (!strncmp(cipherPath_, rootDir.c_str(), rootDir.length())) {
|
|
|
|
return naming->decodePath(cipherPath_ + rootDir.length());
|
|
|
|
} else {
|
|
|
|
if (cipherPath_[0] == '+') {
|
2013-01-29 04:07:54 +01:00
|
|
|
// decode as fully qualified path
|
2013-10-20 00:35:26 +02:00
|
|
|
return string("/") +
|
|
|
|
naming->decodeName(cipherPath_ + 1, strlen(cipherPath_ + 1));
|
|
|
|
} else {
|
|
|
|
return naming->decodePath(cipherPath_);
|
2013-01-29 04:07:54 +01:00
|
|
|
}
|
|
|
|
}
|
2013-10-20 00:35:26 +02:00
|
|
|
}
|
|
|
|
catch (Error &err) {
|
2013-01-29 04:07:54 +01:00
|
|
|
LOG(ERROR) << "decode err: " << err.what();
|
|
|
|
return string();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-10-20 00:35:26 +02:00
|
|
|
string DirNode::relativeCipherPath(const char *plaintextPath) {
|
|
|
|
try {
|
|
|
|
if (plaintextPath[0] == '/') {
|
2013-01-29 04:07:54 +01:00
|
|
|
// mark with '+' to indicate special decoding..
|
2013-10-20 00:35:26 +02:00
|
|
|
return string("+") +
|
|
|
|
naming->encodeName(plaintextPath + 1, strlen(plaintextPath + 1));
|
|
|
|
} else {
|
|
|
|
return naming->encodePath(plaintextPath);
|
2013-01-29 04:07:54 +01:00
|
|
|
}
|
2013-10-20 00:35:26 +02:00
|
|
|
}
|
|
|
|
catch (Error &err) {
|
2013-01-29 04:07:54 +01:00
|
|
|
LOG(ERROR) << "encode err: " << err.what();
|
|
|
|
return string();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-10-20 00:35:26 +02:00
|
|
|
DirTraverse DirNode::openDir(const char *plaintextPath) {
|
|
|
|
string cyName = rootDir + naming->encodePath(plaintextPath);
|
|
|
|
// rDebug("openDir on %s", cyName.c_str() );
|
2013-01-29 04:07:54 +01:00
|
|
|
|
2013-10-20 00:35:26 +02:00
|
|
|
DIR *dir = ::opendir(cyName.c_str());
|
|
|
|
if (dir == NULL) {
|
2013-01-29 04:07:54 +01:00
|
|
|
VLOG(1) << "opendir error " << strerror(errno);
|
2013-10-20 00:35:26 +02:00
|
|
|
return DirTraverse(shared_ptr<DIR>(), 0, shared_ptr<NameIO>());
|
|
|
|
} else {
|
|
|
|
shared_ptr<DIR> dp(dir, DirDeleter());
|
2013-01-29 04:07:54 +01:00
|
|
|
|
|
|
|
uint64_t iv = 0;
|
|
|
|
// if we're using chained IV mode, then compute the IV at this
|
|
|
|
// directory level..
|
2013-10-20 00:35:26 +02:00
|
|
|
try {
|
|
|
|
if (naming->getChainedNameIV()) naming->encodePath(plaintextPath, &iv);
|
|
|
|
}
|
|
|
|
catch (Error &err) {
|
2013-01-29 04:07:54 +01:00
|
|
|
LOG(ERROR) << "encode err: " << err.what();
|
|
|
|
}
|
2013-10-20 00:35:26 +02:00
|
|
|
return DirTraverse(dp, iv, naming);
|
2013-01-29 04:07:54 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-10-20 00:35:26 +02:00
|
|
|
bool DirNode::genRenameList(list<RenameEl> &renameList, const char *fromP,
|
|
|
|
const char *toP) {
|
2013-01-29 04:07:54 +01:00
|
|
|
uint64_t fromIV = 0, toIV = 0;
|
|
|
|
|
|
|
|
// compute the IV for both paths
|
2013-10-20 00:35:26 +02:00
|
|
|
string fromCPart = naming->encodePath(fromP, &fromIV);
|
|
|
|
string toCPart = naming->encodePath(toP, &toIV);
|
2013-01-29 04:07:54 +01:00
|
|
|
|
|
|
|
// where the files live before the rename..
|
|
|
|
string sourcePath = rootDir + fromCPart;
|
|
|
|
|
|
|
|
// ok..... we wish it was so simple.. should almost never happen
|
2013-10-20 00:35:26 +02:00
|
|
|
if (fromIV == toIV) return true;
|
2013-01-29 04:07:54 +01:00
|
|
|
|
|
|
|
// generate the real destination path, where we expect to find the files..
|
|
|
|
VLOG(1) << "opendir " << sourcePath;
|
2013-10-20 00:35:26 +02:00
|
|
|
shared_ptr<DIR> dir =
|
|
|
|
shared_ptr<DIR>(opendir(sourcePath.c_str()), DirDeleter());
|
|
|
|
if (!dir) return false;
|
2013-01-29 04:07:54 +01:00
|
|
|
|
|
|
|
struct dirent *de = NULL;
|
2013-10-20 00:35:26 +02:00
|
|
|
while ((de = ::readdir(dir.get())) != NULL) {
|
2013-01-29 04:07:54 +01:00
|
|
|
// decode the name using the oldIV
|
|
|
|
uint64_t localIV = fromIV;
|
|
|
|
string plainName;
|
|
|
|
|
2013-10-20 00:35:26 +02:00
|
|
|
if ((de->d_name[0] == '.') &&
|
|
|
|
((de->d_name[1] == '\0') ||
|
|
|
|
((de->d_name[1] == '.') && (de->d_name[2] == '\0')))) {
|
2013-01-29 04:07:54 +01:00
|
|
|
// skip "." and ".."
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2013-10-20 00:35:26 +02:00
|
|
|
try {
|
|
|
|
plainName = naming->decodePath(de->d_name, &localIV);
|
|
|
|
}
|
|
|
|
catch (Error &ex) {
|
2013-01-29 04:07:54 +01:00
|
|
|
// if filename can't be decoded, then ignore it..
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// any error in the following will trigger a rename failure.
|
2013-10-20 00:35:26 +02:00
|
|
|
try {
|
2013-01-29 04:07:54 +01:00
|
|
|
// re-encode using the new IV..
|
|
|
|
localIV = toIV;
|
2013-10-20 00:35:26 +02:00
|
|
|
string newName = naming->encodePath(plainName.c_str(), &localIV);
|
2013-01-29 04:07:54 +01:00
|
|
|
|
|
|
|
// store rename information..
|
|
|
|
string oldFull = sourcePath + '/' + de->d_name;
|
|
|
|
string newFull = sourcePath + '/' + newName;
|
|
|
|
|
|
|
|
RenameEl ren;
|
|
|
|
ren.oldCName = oldFull;
|
|
|
|
ren.newCName = newFull;
|
|
|
|
ren.oldPName = string(fromP) + '/' + plainName;
|
|
|
|
ren.newPName = string(toP) + '/' + plainName;
|
|
|
|
|
|
|
|
bool isDir;
|
|
|
|
#if defined(_DIRENT_HAVE_D_TYPE)
|
2013-10-20 00:35:26 +02:00
|
|
|
if (de->d_type != DT_UNKNOWN) {
|
2013-01-29 04:07:54 +01:00
|
|
|
isDir = (de->d_type == DT_DIR);
|
|
|
|
} else
|
|
|
|
#endif
|
|
|
|
{
|
2013-10-20 00:35:26 +02:00
|
|
|
isDir = isDirectory(oldFull.c_str());
|
2013-01-29 04:07:54 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
ren.isDirectory = isDir;
|
|
|
|
|
2013-10-20 00:35:26 +02:00
|
|
|
if (isDir) {
|
2013-01-29 04:07:54 +01:00
|
|
|
// recurse.. We want to add subdirectory elements before the
|
|
|
|
// parent, as that is the logical rename order..
|
2013-10-20 00:35:26 +02:00
|
|
|
if (!genRenameList(renameList, ren.oldPName.c_str(),
|
|
|
|
ren.newPName.c_str())) {
|
2013-01-29 04:07:54 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
VLOG(1) << "adding file " << oldFull << " to rename list";
|
2013-10-20 00:35:26 +02:00
|
|
|
renameList.push_back(ren);
|
|
|
|
}
|
|
|
|
catch (Error &err) {
|
2013-01-29 04:07:54 +01:00
|
|
|
// We can't convert this name, because we don't have a valid IV for
|
|
|
|
// it (or perhaps a valid key).. It will be inaccessible..
|
2013-10-20 00:35:26 +02:00
|
|
|
LOG(WARNING) << "Aborting rename: error on file "
|
|
|
|
<< fromCPart.append(1, '/').append(de->d_name) << ":"
|
|
|
|
<< err.what();
|
2013-01-29 04:07:54 +01:00
|
|
|
|
|
|
|
// abort.. Err on the side of safety and disallow rename, rather
|
|
|
|
// then loosing files..
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
A bit of a pain.. If a directory is renamed in a filesystem with
|
|
|
|
directory initialization vector chaining, then we have to recursively
|
|
|
|
rename every descendent of this directory, as all initialization vectors
|
|
|
|
will have changed..
|
|
|
|
|
|
|
|
Returns a list of renamed items on success, a null list on failure.
|
|
|
|
*/
|
2013-10-20 00:35:26 +02:00
|
|
|
shared_ptr<RenameOp> DirNode::newRenameOp(const char *fromP, const char *toP) {
|
2013-01-29 04:07:54 +01:00
|
|
|
// Do the rename in two stages to avoid chasing our tail
|
|
|
|
// Undo everything if we encounter an error!
|
2013-10-20 00:35:26 +02:00
|
|
|
shared_ptr<list<RenameEl> > renameList(new list<RenameEl>);
|
|
|
|
if (!genRenameList(*renameList.get(), fromP, toP)) {
|
2013-01-29 04:07:54 +01:00
|
|
|
LOG(WARNING) << "Error during generation of recursive rename list";
|
|
|
|
return shared_ptr<RenameOp>();
|
|
|
|
} else
|
2013-10-20 00:35:26 +02:00
|
|
|
return shared_ptr<RenameOp>(new RenameOp(this, renameList));
|
2013-01-29 04:07:54 +01:00
|
|
|
}
|
|
|
|
|
2013-10-20 00:35:26 +02:00
|
|
|
int DirNode::mkdir(const char *plaintextPath, mode_t mode, uid_t uid,
|
|
|
|
gid_t gid) {
|
|
|
|
string cyName = rootDir + naming->encodePath(plaintextPath);
|
|
|
|
rAssert(!cyName.empty());
|
2013-01-29 04:07:54 +01:00
|
|
|
|
|
|
|
VLOG(1) << "mkdir on " << cyName;
|
|
|
|
|
|
|
|
// if uid or gid are set, then that should be the directory owner
|
|
|
|
int olduid = -1;
|
|
|
|
int oldgid = -1;
|
2013-10-20 00:35:26 +02:00
|
|
|
if (uid != 0) olduid = setfsuid(uid);
|
|
|
|
if (gid != 0) oldgid = setfsgid(gid);
|
2013-01-29 04:07:54 +01:00
|
|
|
|
2013-10-20 00:35:26 +02:00
|
|
|
int res = ::mkdir(cyName.c_str(), mode);
|
2013-01-29 04:07:54 +01:00
|
|
|
|
2013-10-20 00:35:26 +02:00
|
|
|
if (olduid >= 0) setfsuid(olduid);
|
|
|
|
if (oldgid >= 0) setfsgid(oldgid);
|
2013-01-29 04:07:54 +01:00
|
|
|
|
2013-10-20 00:35:26 +02:00
|
|
|
if (res == -1) {
|
2013-01-29 04:07:54 +01:00
|
|
|
int eno = errno;
|
2013-10-20 00:35:26 +02:00
|
|
|
LOG(WARNING) << "mkdir error on " << cyName << " mode " << mode << ": "
|
|
|
|
<< strerror(eno);
|
2013-01-29 04:07:54 +01:00
|
|
|
res = -eno;
|
|
|
|
} else
|
|
|
|
res = 0;
|
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2013-10-20 00:35:26 +02:00
|
|
|
int DirNode::rename(const char *fromPlaintext, const char *toPlaintext) {
|
|
|
|
Lock _lock(mutex);
|
2013-01-29 04:07:54 +01:00
|
|
|
|
2013-10-20 00:35:26 +02:00
|
|
|
string fromCName = rootDir + naming->encodePath(fromPlaintext);
|
|
|
|
string toCName = rootDir + naming->encodePath(toPlaintext);
|
|
|
|
rAssert(!fromCName.empty());
|
|
|
|
rAssert(!toCName.empty());
|
2013-01-29 04:07:54 +01:00
|
|
|
|
|
|
|
VLOG(1) << "rename " << fromCName << " -> " << toCName;
|
|
|
|
|
2013-10-20 00:35:26 +02:00
|
|
|
shared_ptr<FileNode> toNode = findOrCreate(toPlaintext);
|
2013-01-29 04:07:54 +01:00
|
|
|
|
|
|
|
shared_ptr<RenameOp> renameOp;
|
2013-10-20 00:35:26 +02:00
|
|
|
if (hasDirectoryNameDependency() && isDirectory(fromCName.c_str())) {
|
2013-01-29 04:07:54 +01:00
|
|
|
VLOG(1) << "recursive rename begin";
|
2013-10-20 00:35:26 +02:00
|
|
|
renameOp = newRenameOp(fromPlaintext, toPlaintext);
|
2013-01-29 04:07:54 +01:00
|
|
|
|
2013-10-20 00:35:26 +02:00
|
|
|
if (!renameOp || !renameOp->apply()) {
|
|
|
|
if (renameOp) renameOp->undo();
|
2013-01-29 04:07:54 +01:00
|
|
|
|
|
|
|
LOG(WARNING) << "rename aborted";
|
|
|
|
return -EACCES;
|
|
|
|
}
|
|
|
|
VLOG(1) << "recursive rename end";
|
|
|
|
}
|
|
|
|
|
|
|
|
int res = 0;
|
2013-10-20 00:35:26 +02:00
|
|
|
try {
|
2013-01-29 04:07:54 +01:00
|
|
|
struct stat st;
|
|
|
|
bool preserve_mtime = ::stat(fromCName.c_str(), &st) == 0;
|
|
|
|
|
2013-10-20 00:35:26 +02:00
|
|
|
renameNode(fromPlaintext, toPlaintext);
|
|
|
|
res = ::rename(fromCName.c_str(), toCName.c_str());
|
2013-01-29 04:07:54 +01:00
|
|
|
|
2013-10-20 00:35:26 +02:00
|
|
|
if (res == -1) {
|
2013-01-29 04:07:54 +01:00
|
|
|
// undo
|
|
|
|
res = -errno;
|
2013-10-20 00:35:26 +02:00
|
|
|
renameNode(toPlaintext, fromPlaintext, false);
|
2013-01-29 04:07:54 +01:00
|
|
|
|
2013-10-20 00:35:26 +02:00
|
|
|
if (renameOp) renameOp->undo();
|
|
|
|
} else if (preserve_mtime) {
|
2013-01-29 04:07:54 +01:00
|
|
|
struct utimbuf ut;
|
|
|
|
ut.actime = st.st_atime;
|
|
|
|
ut.modtime = st.st_mtime;
|
|
|
|
::utime(toCName.c_str(), &ut);
|
|
|
|
}
|
2013-10-20 00:35:26 +02:00
|
|
|
}
|
|
|
|
catch (Error &err) {
|
2013-01-29 04:07:54 +01:00
|
|
|
// exception from renameNode, just show the error and continue..
|
|
|
|
LOG(ERROR) << "rename err: " << err.what();
|
|
|
|
res = -EIO;
|
|
|
|
}
|
|
|
|
|
2013-10-20 00:35:26 +02:00
|
|
|
if (res != 0) {
|
|
|
|
VLOG(1) << "rename failed: " << strerror(errno);
|
2013-01-29 04:07:54 +01:00
|
|
|
res = -errno;
|
|
|
|
}
|
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2013-10-20 00:35:26 +02:00
|
|
|
int DirNode::link(const char *from, const char *to) {
|
|
|
|
Lock _lock(mutex);
|
2013-01-29 04:07:54 +01:00
|
|
|
|
2013-10-20 00:35:26 +02:00
|
|
|
string fromCName = rootDir + naming->encodePath(from);
|
|
|
|
string toCName = rootDir + naming->encodePath(to);
|
2013-01-29 04:07:54 +01:00
|
|
|
|
2013-10-20 00:35:26 +02:00
|
|
|
rAssert(!fromCName.empty());
|
|
|
|
rAssert(!toCName.empty());
|
2013-01-29 04:07:54 +01:00
|
|
|
|
|
|
|
VLOG(1) << "link " << fromCName << " -> " << toCName;
|
|
|
|
|
|
|
|
int res = -EPERM;
|
2013-10-20 00:35:26 +02:00
|
|
|
if (fsConfig->config->external_iv()) {
|
2013-01-29 04:07:54 +01:00
|
|
|
VLOG(1) << "hard links not supported with external IV chaining!";
|
2013-10-20 00:35:26 +02:00
|
|
|
} else {
|
|
|
|
res = ::link(fromCName.c_str(), toCName.c_str());
|
|
|
|
if (res == -1)
|
2013-01-29 04:07:54 +01:00
|
|
|
res = -errno;
|
|
|
|
else
|
|
|
|
res = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
The node is keyed by filename, so a rename means the internal node names
|
|
|
|
must be changed.
|
|
|
|
*/
|
2013-10-20 00:35:26 +02:00
|
|
|
shared_ptr<FileNode> DirNode::renameNode(const char *from, const char *to) {
|
|
|
|
return renameNode(from, to, true);
|
2013-01-29 04:07:54 +01:00
|
|
|
}
|
|
|
|
|
2013-10-20 00:35:26 +02:00
|
|
|
shared_ptr<FileNode> DirNode::renameNode(const char *from, const char *to,
|
|
|
|
bool forwardMode) {
|
|
|
|
shared_ptr<FileNode> node = findOrCreate(from);
|
2013-01-29 04:07:54 +01:00
|
|
|
|
2013-10-20 00:35:26 +02:00
|
|
|
if (node) {
|
2013-01-29 04:07:54 +01:00
|
|
|
uint64_t newIV = 0;
|
2013-10-20 00:35:26 +02:00
|
|
|
string cname = rootDir + naming->encodePath(to, &newIV);
|
|
|
|
|
|
|
|
VLOG(1) << "renaming internal node " << node->cipherName() << " -> "
|
|
|
|
<< cname.c_str();
|
|
|
|
|
|
|
|
if (node->setName(to, cname.c_str(), newIV, forwardMode)) {
|
|
|
|
if (ctx) ctx->renameNode(from, to);
|
|
|
|
} else {
|
|
|
|
// rename error! - put it back
|
2013-01-29 04:07:54 +01:00
|
|
|
LOG(ERROR) << "renameNode failed";
|
|
|
|
throw Error("Internal node name change failed!");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return node;
|
|
|
|
}
|
|
|
|
|
2013-10-20 00:35:26 +02:00
|
|
|
shared_ptr<FileNode> DirNode::findOrCreate(const char *plainName) {
|
2013-01-29 04:07:54 +01:00
|
|
|
shared_ptr<FileNode> node;
|
2013-10-20 00:35:26 +02:00
|
|
|
if (ctx) node = ctx->lookupNode(plainName);
|
2013-01-29 04:07:54 +01:00
|
|
|
|
2013-10-20 00:35:26 +02:00
|
|
|
if (!node) {
|
2013-01-29 04:07:54 +01:00
|
|
|
uint64_t iv = 0;
|
2013-10-20 00:35:26 +02:00
|
|
|
string cipherName = naming->encodePath(plainName, &iv);
|
|
|
|
node.reset(new FileNode(this, fsConfig, plainName,
|
|
|
|
(rootDir + cipherName).c_str()));
|
2013-01-29 04:07:54 +01:00
|
|
|
|
2013-10-20 00:35:26 +02:00
|
|
|
if (fsConfig->config->external_iv()) node->setName(0, 0, iv);
|
2013-01-29 04:07:54 +01:00
|
|
|
|
|
|
|
VLOG(1) << "created FileNode for " << node->cipherName();
|
|
|
|
}
|
|
|
|
|
|
|
|
return node;
|
|
|
|
}
|
|
|
|
|
2013-10-20 00:35:26 +02:00
|
|
|
shared_ptr<FileNode> DirNode::lookupNode(const char *plainName,
|
|
|
|
const char *requestor) {
|
2013-01-29 04:07:54 +01:00
|
|
|
(void)requestor;
|
2013-10-20 00:35:26 +02:00
|
|
|
Lock _lock(mutex);
|
2013-01-29 04:07:54 +01:00
|
|
|
|
2013-10-20 00:35:26 +02:00
|
|
|
shared_ptr<FileNode> node = findOrCreate(plainName);
|
2013-01-29 04:07:54 +01:00
|
|
|
|
|
|
|
return node;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
Similar to lookupNode, except that we also call open() and only return a
|
|
|
|
node on sucess.. This is done in one step to avoid any race conditions
|
|
|
|
with the stored state of the file.
|
|
|
|
*/
|
2013-10-20 00:35:26 +02:00
|
|
|
shared_ptr<FileNode> DirNode::openNode(const char *plainName,
|
|
|
|
const char *requestor, int flags,
|
|
|
|
int *result) {
|
2013-01-29 04:07:54 +01:00
|
|
|
(void)requestor;
|
2013-10-20 00:35:26 +02:00
|
|
|
rAssert(result != NULL);
|
|
|
|
Lock _lock(mutex);
|
2013-01-29 04:07:54 +01:00
|
|
|
|
2013-10-20 00:35:26 +02:00
|
|
|
shared_ptr<FileNode> node = findOrCreate(plainName);
|
2013-01-29 04:07:54 +01:00
|
|
|
|
2013-10-20 00:35:26 +02:00
|
|
|
if (node && (*result = node->open(flags)) >= 0)
|
2013-01-29 04:07:54 +01:00
|
|
|
return node;
|
|
|
|
else
|
|
|
|
return shared_ptr<FileNode>();
|
|
|
|
}
|
|
|
|
|
2013-10-20 00:35:26 +02:00
|
|
|
int DirNode::unlink(const char *plaintextName) {
|
|
|
|
string cyName = naming->encodePath(plaintextName);
|
2013-01-29 04:07:54 +01:00
|
|
|
VLOG(1) << "unlink " << cyName;
|
|
|
|
|
2013-10-20 00:35:26 +02:00
|
|
|
Lock _lock(mutex);
|
2013-01-29 04:07:54 +01:00
|
|
|
|
|
|
|
int res = 0;
|
2013-10-20 00:35:26 +02:00
|
|
|
if (ctx && ctx->lookupNode(plaintextName)) {
|
2013-01-29 04:07:54 +01:00
|
|
|
// If FUSE is running with "hard_remove" option where it doesn't
|
|
|
|
// hide open files for us, then we can't allow an unlink of an open
|
|
|
|
// file..
|
2013-10-20 00:35:26 +02:00
|
|
|
LOG(WARNING) << "Refusing to unlink open file: " << cyName
|
|
|
|
<< ", hard_remove option is probably in effect";
|
2013-01-29 04:07:54 +01:00
|
|
|
res = -EBUSY;
|
2013-10-20 00:35:26 +02:00
|
|
|
} else {
|
2013-01-29 04:07:54 +01:00
|
|
|
string fullName = rootDir + cyName;
|
2013-10-20 00:35:26 +02:00
|
|
|
res = ::unlink(fullName.c_str());
|
|
|
|
if (res == -1) {
|
2013-01-29 04:07:54 +01:00
|
|
|
res = -errno;
|
|
|
|
VLOG(1) << "unlink error: " << strerror(errno);
|
2013-10-20 00:35:26 +02:00
|
|
|
}
|
2013-01-29 04:07:54 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|
2013-03-05 07:29:58 +01:00
|
|
|
|
|
|
|
} // namespace encfs
|