From 5a99506ea8b2911dcef8a43082ead006efdeb88f Mon Sep 17 00:00:00 2001 From: Valient Gough Date: Thu, 3 Aug 2017 22:00:32 -0700 Subject: [PATCH] modernize: use C++ headers, auto, make_shared --- encfs/Cipher.cpp | 6 +++--- encfs/CipherFileIO.cpp | 4 ++-- encfs/ConfigReader.cpp | 4 ++-- encfs/ConfigVar.cpp | 4 ++-- encfs/Context.cpp | 6 +++--- encfs/DirNode.cpp | 4 ++-- encfs/Error.h | 2 +- encfs/FileNode.cpp | 4 ++-- encfs/FileUtils.cpp | 18 ++++++++---------- encfs/MACFileIO.cpp | 2 +- encfs/MemoryPool.cpp | 4 ++-- encfs/RawFileIO.cpp | 2 +- encfs/SSL_Cipher.cpp | 2 +- encfs/XmlReader.cpp | 6 +++--- encfs/autosprintf.cpp | 10 +++++----- encfs/base64.cpp | 4 ++-- encfs/encfs.cpp | 6 +++--- encfs/main.cpp | 10 +++++----- encfs/openssl.cpp | 2 +- 19 files changed, 49 insertions(+), 51 deletions(-) diff --git a/encfs/Cipher.cpp b/encfs/Cipher.cpp index 8ae0f9a..00b9a8d 100644 --- a/encfs/Cipher.cpp +++ b/encfs/Cipher.cpp @@ -18,10 +18,10 @@ * along with this program. If not, see . */ +#include #include #include #include -#include #include #include @@ -184,13 +184,13 @@ bool Cipher::nameDecode(unsigned char *data, int len, uint64_t iv64, string Cipher::encodeAsString(const CipherKey &key, const CipherKey &encodingKey) { int encodedKeySize = this->encodedKeySize(); - unsigned char *keyBuf = new unsigned char[encodedKeySize]; + auto *keyBuf = new unsigned char[encodedKeySize]; // write the key, encoding it with itself. this->writeKey(key, keyBuf, encodingKey); int b64Len = B256ToB64Bytes(encodedKeySize); - unsigned char *b64Key = new unsigned char[b64Len + 1]; + auto *b64Key = new unsigned char[b64Len + 1]; changeBase2(keyBuf, encodedKeySize, 8, b64Key, b64Len, 6); B64ToAscii(b64Key, b64Len); diff --git a/encfs/CipherFileIO.cpp b/encfs/CipherFileIO.cpp index 0179db9..40e3d5d 100644 --- a/encfs/CipherFileIO.cpp +++ b/encfs/CipherFileIO.cpp @@ -22,11 +22,11 @@ #include "internal/easylogging++.h" #include +#include +#include #include -#include #include #include -#include #include #include "BlockFileIO.h" diff --git a/encfs/ConfigReader.cpp b/encfs/ConfigReader.cpp index e622ecf..d3f52d1 100644 --- a/encfs/ConfigReader.cpp +++ b/encfs/ConfigReader.cpp @@ -49,7 +49,7 @@ bool ConfigReader::load(const char *fileName) { int fd = open(fileName, O_RDONLY); if (fd < 0) return false; - char *buf = new char[size]; + auto *buf = new char[size]; int res = ::read(fd, buf, size); close(fd); @@ -126,7 +126,7 @@ ConfigVar ConfigReader::toVar() const { ConfigVar ConfigReader::operator[](const std::string &varName) const { // read only - map::const_iterator it = vars.find(varName); + auto it = vars.find(varName); if (it == vars.end()) return ConfigVar(); else diff --git a/encfs/ConfigVar.cpp b/encfs/ConfigVar.cpp index a4fadc8..27792f1 100644 --- a/encfs/ConfigVar.cpp +++ b/encfs/ConfigVar.cpp @@ -112,7 +112,7 @@ void ConfigVar::writeInt(int val) { } int ConfigVar::readInt() const { - const unsigned char *buf = (const unsigned char *)buffer(); + const auto *buf = (const unsigned char *)buffer(); int bytes = this->size(); int offset = at(); int value = 0; @@ -184,7 +184,7 @@ const ConfigVar &operator>>(const ConfigVar &src, std::string &result) { unsigned char tmpBuf[32]; if (length > (int)sizeof(tmpBuf)) { - unsigned char *ptr = new unsigned char[length]; + auto *ptr = new unsigned char[length]; readLen = src.read(ptr, length); result.assign((char *)ptr, length); delete[] ptr; diff --git a/encfs/Context.cpp b/encfs/Context.cpp index fbb8acb..c6bf276 100644 --- a/encfs/Context.cpp +++ b/encfs/Context.cpp @@ -87,7 +87,7 @@ void EncFS_Context::getAndResetUsageCounter(int *usage, int *openCount) { std::shared_ptr EncFS_Context::lookupNode(const char *path) { Lock lock(contextMutex); - FileMap::iterator it = openFiles.find(std::string(path)); + auto it = openFiles.find(std::string(path)); if (it != openFiles.end()) { // every entry in the list is fine... so just use the // first @@ -99,7 +99,7 @@ std::shared_ptr EncFS_Context::lookupNode(const char *path) { void EncFS_Context::renameNode(const char *from, const char *to) { Lock lock(contextMutex); - FileMap::iterator it = openFiles.find(std::string(from)); + auto it = openFiles.find(std::string(from)); if (it != openFiles.end()) { auto val = it->second; openFiles.erase(it); @@ -123,7 +123,7 @@ void EncFS_Context::eraseNode(const char *path, std::shared_ptr fnode) { Lock lock(contextMutex); - FileMap::iterator it = openFiles.find(std::string(path)); + auto it = openFiles.find(std::string(path)); rAssert(it != openFiles.end()); auto &list = it->second; diff --git a/encfs/DirNode.cpp b/encfs/DirNode.cpp index 5560b75..f3d32e6 100644 --- a/encfs/DirNode.cpp +++ b/encfs/DirNode.cpp @@ -219,7 +219,7 @@ void RenameOp::undo() { // list has to be processed backwards, otherwise we may rename // directories and directory contents in the wrong order! int undoCount = 0; - list::const_iterator it = last; + auto it = last; while (it != renameList->begin()) { --it; @@ -485,7 +485,7 @@ std::shared_ptr DirNode::newRenameOp(const char *fromP, RLOG(WARNING) << "Error during generation of recursive rename list"; return std::shared_ptr(); } else - return std::shared_ptr(new RenameOp(this, renameList)); + return std::make_shared(this, renameList); } int DirNode::mkdir(const char *plaintextPath, mode_t mode, uid_t uid, diff --git a/encfs/Error.h b/encfs/Error.h index 3e54536..c045871 100644 --- a/encfs/Error.h +++ b/encfs/Error.h @@ -21,7 +21,7 @@ class Error : public std::runtime_error { RLOG(ERROR) << "Assert failed: " << STR(cond); \ throw encfs::Error(STR(cond)); \ } \ - } while (0) + } while (false) void initLogging(bool enable_debug = false, bool is_daemon = false); diff --git a/encfs/FileNode.cpp b/encfs/FileNode.cpp index 96e27b9..1565ef8 100644 --- a/encfs/FileNode.cpp +++ b/encfs/FileNode.cpp @@ -18,9 +18,9 @@ * along with this program. If not, see . */ -#include +#include +#include #include -#include #include #include #include diff --git a/encfs/FileUtils.cpp b/encfs/FileUtils.cpp index ae4adf3..a8c5b38 100644 --- a/encfs/FileUtils.cpp +++ b/encfs/FileUtils.cpp @@ -323,7 +323,7 @@ bool readV6Config(const char *configFile, EncFSConfig *cfg, ConfigInfo *info) { int encodedSize; config->read("encodedKeySize", &encodedSize); - unsigned char *key = new unsigned char[encodedSize]; + auto *key = new unsigned char[encodedSize]; config->readB64("encodedKeyData", key, encodedSize); cfg->assignKeyData(key, encodedSize); delete[] key; @@ -331,7 +331,7 @@ bool readV6Config(const char *configFile, EncFSConfig *cfg, ConfigInfo *info) { if (cfg->subVersion >= 20080816) { int saltLen; config->read("saltLen", &saltLen); - unsigned char *salt = new unsigned char[saltLen]; + auto *salt = new unsigned char[saltLen]; config->readB64("saltData", salt, saltLen); cfg->assignSaltData(salt, saltLen); delete[] salt; @@ -1161,7 +1161,7 @@ RootPtr createV6Config(EncFS_Context *ctx, "later using encfsctl.\n\n"); int encodedKeySize = cipher->encodedKeySize(); - unsigned char *encodedKey = new unsigned char[encodedKeySize]; + auto *encodedKey = new unsigned char[encodedKeySize]; CipherKey volumeKey = cipher->newRandomKey(); @@ -1216,11 +1216,10 @@ RootPtr createV6Config(EncFS_Context *ctx, fsConfig->idleTracking = enableIdleTracking; fsConfig->opts = opts; - rootInfo = RootPtr(new EncFS_Root); + rootInfo = std::make_shared(); rootInfo->cipher = cipher; rootInfo->volumeKey = volumeKey; - rootInfo->root = - std::shared_ptr(new DirNode(ctx, rootDir, fsConfig)); + rootInfo->root = std::make_shared(ctx, rootDir, fsConfig); return rootInfo; } @@ -1576,7 +1575,7 @@ RootPtr initFS(EncFS_Context *ctx, const std::shared_ptr &opts) { } if (opts->delayMount) { - rootInfo = RootPtr(new EncFS_Root); + rootInfo = std::make_shared(); rootInfo->cipher = cipher; rootInfo->root = std::shared_ptr(); return rootInfo; @@ -1632,11 +1631,10 @@ RootPtr initFS(EncFS_Context *ctx, const std::shared_ptr &opts) { fsConfig->reverseEncryption = opts->reverseEncryption; fsConfig->opts = opts; - rootInfo = RootPtr(new EncFS_Root); + rootInfo = std::make_shared(); rootInfo->cipher = cipher; rootInfo->volumeKey = volumeKey; - rootInfo->root = - std::shared_ptr(new DirNode(ctx, opts->rootDir, fsConfig)); + rootInfo->root = std::make_shared(ctx, opts->rootDir, fsConfig); } else { if (opts->createIfNotFound) { // creating a new encrypted filesystem diff --git a/encfs/MACFileIO.cpp b/encfs/MACFileIO.cpp index 092fb1f..6d2897f 100644 --- a/encfs/MACFileIO.cpp +++ b/encfs/MACFileIO.cpp @@ -21,8 +21,8 @@ #include "MACFileIO.h" #include "internal/easylogging++.h" +#include #include -#include #include #include "BlockFileIO.h" diff --git a/encfs/MemoryPool.cpp b/encfs/MemoryPool.cpp index 37137bc..6291785 100644 --- a/encfs/MemoryPool.cpp +++ b/encfs/MemoryPool.cpp @@ -44,7 +44,7 @@ struct BlockList { }; static BlockList *allocBlock(int size) { - BlockList *block = new BlockList; + auto *block = new BlockList; block->size = size; block->data = BUF_MEM_new(); BUF_MEM_grow(block->data, size); @@ -98,7 +98,7 @@ MemBlock MemoryPool::allocate(int size) { void MemoryPool::release(const MemBlock &mb) { pthread_mutex_lock(&gMPoolMutex); - BlockList *block = (BlockList *)mb.internalData; + auto *block = (BlockList *)mb.internalData; // just to be sure there's nothing important left in buffers.. VALGRIND_MAKE_MEM_UNDEFINED(block->data->data, block->size); diff --git a/encfs/RawFileIO.cpp b/encfs/RawFileIO.cpp index accb3fd..1eb1e93 100644 --- a/encfs/RawFileIO.cpp +++ b/encfs/RawFileIO.cpp @@ -23,9 +23,9 @@ #endif #include "internal/easylogging++.h" #include +#include #include #include -#include #include #include diff --git a/encfs/SSL_Cipher.cpp b/encfs/SSL_Cipher.cpp index 51e5ccf..a3ab05a 100644 --- a/encfs/SSL_Cipher.cpp +++ b/encfs/SSL_Cipher.cpp @@ -479,7 +479,7 @@ static uint64_t _checksum_64(SSLKey *key, const unsigned char *data, for (unsigned int i = 0; i < (mdLen - 1); ++i) h[i % 8] ^= (unsigned char)(md[i]); - uint64_t value = (uint64_t)h[0]; + auto value = (uint64_t)h[0]; for (int i = 1; i < 8; ++i) value = (value << 8) | (uint64_t)h[i]; return value; diff --git a/encfs/XmlReader.cpp b/encfs/XmlReader.cpp index 8016d9c..c4e938b 100644 --- a/encfs/XmlReader.cpp +++ b/encfs/XmlReader.cpp @@ -145,7 +145,7 @@ class XmlNode : virtual public XmlValue { if (name[0] == '@') { const char *value = element->Attribute(name + 1); if (value) - return XmlValuePtr(new XmlValue(value)); + return std::make_shared(value); else return XmlValuePtr(); } else { @@ -182,13 +182,13 @@ XmlValuePtr XmlReader::operator[](const char *name) const { tinyxml2::XMLNode *node = pd->doc->FirstChildElement(name); if (node == nullptr) { RLOG(ERROR) << "Xml node " << name << " not found"; - return XmlValuePtr(new XmlValue()); + return std::make_shared(); } tinyxml2::XMLElement *element = node->ToElement(); if (element == nullptr) { RLOG(ERROR) << "Xml node " << name << " not element"; - return XmlValuePtr(new XmlValue()); + return std::make_shared(); } return XmlValuePtr(new XmlNode(element)); diff --git a/encfs/autosprintf.cpp b/encfs/autosprintf.cpp index 7281825..fbb164b 100644 --- a/encfs/autosprintf.cpp +++ b/encfs/autosprintf.cpp @@ -27,10 +27,10 @@ /* Specification. */ #include "autosprintf.h" -#include // for va_list -#include // for NULL, vasprintf -#include // for free -#include // for strdup +#include // for va_list +#include // for NULL, vasprintf +#include // for free +#include // for strdup namespace gnu { @@ -54,7 +54,7 @@ autosprintf::~autosprintf() { free(str); } autosprintf::operator char *() const { if (str != nullptr) { size_t length = strlen(str) + 1; - char *copy = new char[length]; + auto *copy = new char[length]; memcpy(copy, str, length); return copy; } else diff --git a/encfs/base64.cpp b/encfs/base64.cpp index cc6eb19..9edf8ca 100644 --- a/encfs/base64.cpp +++ b/encfs/base64.cpp @@ -20,7 +20,7 @@ #include "base64.h" -#include // for toupper +#include // for toupper #include "Error.h" @@ -248,7 +248,7 @@ std::string B64StandardEncode(std::vector inputBuffer) { std::string encodedString; encodedString.reserve(B256ToB64Bytes(inputBuffer.size())); long temp; - std::vector::iterator cursor = inputBuffer.begin(); + auto cursor = inputBuffer.begin(); for (size_t idx = 0; idx < inputBuffer.size() / 3; idx++) { temp = (*cursor++) << 16; // Convert to big endian temp += (*cursor++) << 8; diff --git a/encfs/encfs.cpp b/encfs/encfs.cpp index 855863f..07d1451 100644 --- a/encfs/encfs.cpp +++ b/encfs/encfs.cpp @@ -18,17 +18,17 @@ #include "encfs.h" #include +#include #include +#include #include #include +#include #include -#include #include -#include #include #include #include -#include #include #include #ifdef linux diff --git a/encfs/main.cpp b/encfs/main.cpp index 3b10884..640fe4f 100644 --- a/encfs/main.cpp +++ b/encfs/main.cpp @@ -18,18 +18,18 @@ #include #include +#include #include +#include #include #include #include #include #include #include -#include #include #include #include -#include #include #include "Context.h" @@ -493,7 +493,7 @@ static bool processArgs(int argc, char *argv[], static void *idleMonitor(void *); void *encfs_init(fuse_conn_info *conn) { - EncFS_Context *ctx = (EncFS_Context *)fuse_get_context()->private_data; + auto *ctx = (EncFS_Context *)fuse_get_context()->private_data; // set fuse connection options conn->async_read = true; @@ -599,7 +599,7 @@ int main(int argc, char *argv[]) { // context is not a smart pointer because it will live for the life of // the filesystem. - auto ctx = std::shared_ptr(new EncFS_Context); + auto ctx = std::make_shared(); ctx->publicFilesystem = encfsArgs->opts->ownerCreate; RootPtr rootInfo = initFS(ctx.get(), encfsArgs->opts); @@ -709,7 +709,7 @@ const int ActivityCheckInterval = 10; static bool unmountFS(EncFS_Context *ctx); static void *idleMonitor(void *_arg) { - EncFS_Context *ctx = (EncFS_Context *)_arg; + auto *ctx = (EncFS_Context *)_arg; std::shared_ptr arg = ctx->args; const int timeoutCycles = 60 * arg->idleTimeout / ActivityCheckInterval; diff --git a/encfs/openssl.cpp b/encfs/openssl.cpp index d859f1c..f336a6b 100644 --- a/encfs/openssl.cpp +++ b/encfs/openssl.cpp @@ -20,9 +20,9 @@ #include "openssl.h" +#include #include #include -#include #define NO_DES #include