2013-01-29 04:07:54 +01:00
|
|
|
/*****************************************************************************
|
|
|
|
* Author: Valient Gough <vgough@pobox.com>
|
|
|
|
*
|
|
|
|
*****************************************************************************
|
|
|
|
* Copyright (c) 2004-2013, 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 "base/ConfigReader.h"
|
|
|
|
|
|
|
|
#include <glog/logging.h>
|
|
|
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <cstring>
|
|
|
|
|
2013-03-05 07:29:58 +01:00
|
|
|
#include "base/types.h"
|
2013-01-29 04:07:54 +01:00
|
|
|
|
2013-03-05 07:39:51 +01:00
|
|
|
using std::make_pair;
|
|
|
|
using std::map;
|
|
|
|
using std::string;
|
2013-01-29 04:07:54 +01:00
|
|
|
|
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
|
|
|
ConfigReader::ConfigReader() {}
|
2013-01-29 04:07:54 +01:00
|
|
|
|
2013-10-20 00:35:26 +02:00
|
|
|
ConfigReader::~ConfigReader() {}
|
2013-01-29 04:07:54 +01:00
|
|
|
|
|
|
|
// read the entire file into a ConfigVar instance and then use that to decode
|
|
|
|
// into mapped variables.
|
2013-10-20 00:35:26 +02:00
|
|
|
bool ConfigReader::load(const char *fileName) {
|
2013-01-29 04:07:54 +01:00
|
|
|
struct stat stbuf;
|
2013-10-20 00:35:26 +02:00
|
|
|
memset(&stbuf, 0, sizeof(struct stat));
|
|
|
|
if (lstat(fileName, &stbuf) != 0) return false;
|
2013-01-29 04:07:54 +01:00
|
|
|
|
|
|
|
int size = stbuf.st_size;
|
|
|
|
|
2013-10-20 00:35:26 +02:00
|
|
|
int fd = open(fileName, O_RDONLY);
|
|
|
|
if (fd < 0) return false;
|
2013-01-29 04:07:54 +01:00
|
|
|
|
|
|
|
char *buf = new char[size];
|
|
|
|
|
2013-10-20 00:35:26 +02:00
|
|
|
int res = ::read(fd, buf, size);
|
|
|
|
close(fd);
|
2013-01-29 04:07:54 +01:00
|
|
|
|
2013-10-20 00:35:26 +02:00
|
|
|
if (res != size) {
|
|
|
|
LOG(WARNING) << "Partial read of config file, expecting " << size
|
|
|
|
<< " bytes, got " << res;
|
2013-01-29 04:07:54 +01:00
|
|
|
delete[] buf;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
ConfigVar in;
|
2013-10-20 00:35:26 +02:00
|
|
|
in.write((byte *)buf, size);
|
2013-01-29 04:07:54 +01:00
|
|
|
delete[] buf;
|
|
|
|
|
2013-10-20 00:35:26 +02:00
|
|
|
return loadFromVar(in);
|
2013-01-29 04:07:54 +01:00
|
|
|
}
|
|
|
|
|
2013-10-20 00:35:26 +02:00
|
|
|
bool ConfigReader::loadFromVar(ConfigVar &in) {
|
2013-01-29 04:07:54 +01:00
|
|
|
in.resetOffset();
|
|
|
|
|
|
|
|
// parse.
|
|
|
|
int numEntries = in.readInt();
|
|
|
|
|
2013-10-20 00:35:26 +02:00
|
|
|
for (int i = 0; i < numEntries; ++i) {
|
2013-01-29 04:07:54 +01:00
|
|
|
string key, value;
|
|
|
|
in >> key >> value;
|
|
|
|
|
2013-10-20 00:35:26 +02:00
|
|
|
if (key.length() == 0) {
|
2013-01-29 04:07:54 +01:00
|
|
|
LOG(ERROR) << "Invalid key encoding in buffer";
|
|
|
|
return false;
|
|
|
|
}
|
2013-10-20 00:35:26 +02:00
|
|
|
ConfigVar newVar(value);
|
|
|
|
vars.insert(make_pair(key, newVar));
|
2013-01-29 04:07:54 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-10-20 00:35:26 +02:00
|
|
|
ConfigVar ConfigReader::operator[](const std::string &varName) const {
|
2013-01-29 04:07:54 +01:00
|
|
|
// read only
|
2013-10-20 00:35:26 +02:00
|
|
|
map<string, ConfigVar>::const_iterator it = vars.find(varName);
|
|
|
|
if (it == vars.end())
|
2013-01-29 04:07:54 +01:00
|
|
|
return ConfigVar();
|
|
|
|
else
|
|
|
|
return it->second;
|
|
|
|
}
|
|
|
|
|
2013-10-20 00:35:26 +02:00
|
|
|
ConfigVar &ConfigReader::operator[](const std::string &varName) {
|
|
|
|
return vars[varName];
|
2013-01-29 04:07:54 +01:00
|
|
|
}
|
|
|
|
|
2013-03-05 07:29:58 +01:00
|
|
|
} // namespace encfs
|