encfs/base/ConfigReader.cpp

141 lines
3.6 KiB
C++
Raw Permalink Normal View History

/*****************************************************************************
* Author: Valient Gough <vgough@pobox.com>
*
*****************************************************************************
* Copyright (c) 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
2014-10-19 04:19:33 +02:00
* option) any later version.
*
* 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 "ConfigReader.h"
2015-06-18 05:33:57 +02:00
#include <cstring>
2015-06-18 06:56:43 +02:00
#include <fcntl.h>
#include <sys/stat.h>
#include <unistd.h>
2015-06-18 06:56:43 +02:00
#include <utility>
2015-06-18 07:59:04 +02:00
#include "ConfigVar.h"
2015-06-18 05:33:57 +02:00
#include "Error.h"
using namespace std;
2015-06-18 05:33:57 +02:00
namespace encfs {
2014-10-19 04:19:33 +02:00
ConfigReader::ConfigReader() {}
2014-10-19 04:19:33 +02:00
ConfigReader::~ConfigReader() {}
// read the entire file into a ConfigVar instance and then use that to decode
// into mapped variables.
2014-10-19 04:19:33 +02:00
bool ConfigReader::load(const char *fileName) {
struct stat stbuf;
memset(&stbuf, 0, sizeof(struct stat));
if (lstat(fileName, &stbuf) != 0) return false;
int size = stbuf.st_size;
int fd = open(fileName, O_RDONLY);
if (fd < 0) return false;
2014-10-19 04:19:33 +02:00
char *buf = new char[size];
int res = ::read(fd, buf, size);
close(fd);
if (res != size) {
2015-06-18 05:33:57 +02:00
RLOG(WARNING) << "Partial read of config file, expecting " << size
<< " bytes, got " << res;
delete[] buf;
2014-10-19 04:19:33 +02:00
return false;
}
2014-10-19 04:19:33 +02:00
ConfigVar in;
in.write((unsigned char *)buf, size);
delete[] buf;
return loadFromVar(in);
}
2014-10-19 04:19:33 +02:00
bool ConfigReader::loadFromVar(ConfigVar &in) {
in.resetOffset();
2014-10-19 04:19:33 +02:00
// parse.
int numEntries = in.readInt();
for (int i = 0; i < numEntries; ++i) {
string key, value;
in >> key >> value;
2014-10-19 04:19:33 +02:00
if (key.length() == 0) {
2015-06-18 05:33:57 +02:00
RLOG(ERROR) << "Invalid key encoding in buffer";
2014-10-19 04:19:33 +02:00
return false;
}
2014-10-19 04:19:33 +02:00
ConfigVar newVar(value);
vars.insert(make_pair(key, newVar));
}
2014-10-19 04:19:33 +02:00
return true;
}
2014-10-19 04:19:33 +02:00
bool ConfigReader::save(const char *fileName) const {
// write everything to a ConfigVar, then output to disk
ConfigVar out = toVar();
int fd = ::open(fileName, O_RDWR | O_CREAT, 0640);
if (fd >= 0) {
int retVal = ::write(fd, out.buffer(), out.size());
close(fd);
if (retVal != out.size()) {
2015-06-18 05:33:57 +02:00
RLOG(ERROR) << "Error writing to config file " << fileName;
2014-10-19 04:19:33 +02:00
return false;
}
2014-10-19 04:19:33 +02:00
} else {
2015-06-18 05:33:57 +02:00
RLOG(ERROR) << "Unable to open or create file " << fileName;
2014-10-19 04:19:33 +02:00
return false;
}
2014-10-19 04:19:33 +02:00
return true;
}
2014-10-19 04:19:33 +02:00
ConfigVar ConfigReader::toVar() const {
// write everything to a ConfigVar, then output to disk
ConfigVar out;
out.writeInt(vars.size());
map<string, ConfigVar>::const_iterator it;
for (it = vars.begin(); it != vars.end(); ++it) {
out.writeInt(it->first.size());
out.write((unsigned char *)it->first.data(), it->first.size());
out.writeInt(it->second.size());
out.write((unsigned char *)it->second.buffer(), it->second.size());
}
return out;
}
2014-10-19 04:19:33 +02:00
ConfigVar ConfigReader::operator[](const std::string &varName) const {
// read only
map<string, ConfigVar>::const_iterator it = vars.find(varName);
if (it == vars.end())
return ConfigVar();
else
return it->second;
}
2014-10-19 04:19:33 +02:00
ConfigVar &ConfigReader::operator[](const std::string &varName) {
return vars[varName];
}
2015-06-18 05:33:57 +02:00
} // namespace encfs