Added mkdirp for merged output to version/channel folders
This commit is contained in:
Родитель
a3e6b647dd
Коммит
a87f0294af
|
@ -4,6 +4,7 @@
|
|||
#include "Aggregate.h"
|
||||
#include "../CompressedFileReader.h"
|
||||
#include "../../utils/ParseDate.h"
|
||||
#include "../../utils/mkdirp.h"
|
||||
|
||||
#include "rapidjson/document.h"
|
||||
|
||||
|
@ -84,22 +85,32 @@ void ResultSet::updateFileInFolder(string folder) {
|
|||
string filename;
|
||||
filename.reserve(256 + folder.size());
|
||||
for(auto pair : _channelVersionMap) {
|
||||
string channelDashVersion = pair.first;
|
||||
for(size_t i = 0; i < channelDashVersion.length(); i++) {
|
||||
if(channelDashVersion[i] == '/')
|
||||
channelDashVersion[i] = '-';
|
||||
// Find first slash (and there by channel)
|
||||
size_t slash = pair.first.find("/");
|
||||
if (slash == string::npos) {
|
||||
fprintf(stderr, "Cannot output channel/version without slash: '%s'\n",
|
||||
pair.first.data());
|
||||
continue;
|
||||
}
|
||||
filename = folder + channelDashVersion;
|
||||
string channel = pair.first.substr(0, slash);
|
||||
|
||||
// Check if filename exists
|
||||
if (access(filename.data(), F_OK) != 0) {
|
||||
filename = folder + pair.first;
|
||||
if (access(filename.data(), F_OK) == 0) {
|
||||
// If it exists we'll want to merge it into the current data-set
|
||||
ifstream file(filename);
|
||||
mergeStream(file);
|
||||
file.close();
|
||||
}
|
||||
|
||||
// Create channel folder if it doesn't already exists
|
||||
mkdirp((folder + channel).data());
|
||||
|
||||
// Output updated file
|
||||
FILE* f = fopen(filename.data(), "w");
|
||||
if (!f) {
|
||||
fprintf(stderr, "Failed to open '%s'\n", filename.data());
|
||||
continue;
|
||||
}
|
||||
pair.second->output(f, pair.first);
|
||||
fclose(f);
|
||||
|
|
|
@ -0,0 +1,46 @@
|
|||
#ifndef MKDIRP_H
|
||||
#define MKDIRP_H
|
||||
|
||||
#include <sys/stat.h>
|
||||
#include <errno.h>
|
||||
|
||||
#define DEFAULT_MODE S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH
|
||||
|
||||
/** Utility function to create directory tree */
|
||||
bool mkdirp(const char* path, mode_t mode = DEFAULT_MODE) {
|
||||
// Invalid string
|
||||
if (path[0] == '\0') {
|
||||
return false;
|
||||
}
|
||||
|
||||
// const cast for hack
|
||||
char* p = const_cast<char*>(path);
|
||||
|
||||
// Find next slash mkdir() it and until we're at end of string
|
||||
while (*p != '\0') {
|
||||
// Skip first character
|
||||
p++;
|
||||
|
||||
// Find first slash or end
|
||||
while(*p != '\0' && *p != '/') p++;
|
||||
|
||||
// Remember value from p
|
||||
char v = *p;
|
||||
|
||||
// Write end of string at p
|
||||
*p = '\0';
|
||||
|
||||
// Create folder from path to '\0' inserted at p
|
||||
if(mkdir(path, mode) != 0 && errno != EEXIST) {
|
||||
*p = v;
|
||||
return false;
|
||||
}
|
||||
|
||||
// Restore path to it's former glory
|
||||
*p = v;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
#endif // MKDIRP_H
|
Загрузка…
Ссылка в новой задаче