Added directory checking and creation functions to Util library

This commit is contained in:
Thulinma 2017-06-23 15:05:05 +02:00
parent 3c68d537f6
commit 5a4cb100fa
2 changed files with 346 additions and 246 deletions

View file

@ -2,11 +2,16 @@
#define _FILE_OFFSET_BITS 64
#include "util.h"
#include "timing.h"
#include "defines.h"
#include "bitfields.h"
#include <stdio.h>
#include "defines.h"
#include "timing.h"
#include <errno.h> // errno, ENOENT, EEXIST
#include <iostream>
#include <stdio.h>
#include <sys/stat.h> // stat
#if defined(_WIN32)
#include <direct.h> // _mkdir
#endif
#define RECORD_POINTER p + getOffset() + (getRecordPosition(recordNo) * getRSize()) + fd.offset
#define RAXHDR_FIELDOFFSET p[1]
@ -19,7 +24,85 @@
#define RAX_REQDFIELDS_LEN 28
namespace Util{
bool stringScan(const std::string & src, const std::string & pattern, std::deque<std::string> & result){
/// Helper function that cross-platform checks if a given directory exists.
bool isDirectory(const std::string &path){
#if defined(_WIN32)
struct _stat info;
if (_stat(path.c_str(), &info) != 0){return false;}
return (info.st_mode & _S_IFDIR) != 0;
#else
struct stat info;
if (stat(path.c_str(), &info) != 0){return false;}
return (info.st_mode & S_IFDIR) != 0;
#endif
}
bool createPathFor(const std::string &file){
int pos = file.find_last_of('/');
#if defined(_WIN32)
// Windows also supports backslashes as directory separator
if (pos == std::string::npos){pos = file.find_last_of('\\');}
#endif
if (pos == std::string::npos){
return true; // There is no parent
}
// Fail if we cannot create a parent
return createPath(file.substr(0, pos));
}
/// Helper function that will attempt to create the given path if it not yet exists.
/// Returns true if path exists or was successfully created, false otherwise.
bool createPath(const std::string &path){
#if defined(_WIN32)
int ret = _mkdir(path.c_str());
#else
mode_t mode = 0755;
int ret = mkdir(path.c_str(), mode);
#endif
if (ret == 0){// Success!
INFO_MSG("Created directory: %s", path.c_str());
return true;
}
switch (errno){
case ENOENT:{// Parent does not exist
int pos = path.find_last_of('/');
#if defined(_WIN32)
// Windows also supports backslashes as directory separator
if (pos == std::string::npos){pos = path.find_last_of('\\');}
#endif
if (pos == std::string::npos){
// fail if there is no parent
// Theoretically cannot happen, but who knows
FAIL_MSG("Could not create %s: %s", path.c_str(), strerror(errno));
return false;
}
// Fail if we cannot create a parent
if (!createPath(path.substr(0, pos))) return false;
#if defined(_WIN32)
ret = _mkdir(path.c_str());
#else
ret = mkdir(path.c_str(), mode);
#endif
if (ret){FAIL_MSG("Could not create %s: %s", path.c_str(), strerror(errno));}
return (ret == 0);
}
case EEXIST: // Is a file or directory
if (isDirectory(path)){
return true; // All good, already exists
}else{
FAIL_MSG("Not a directory: %s", path.c_str());
return false;
}
default: // Generic failure
FAIL_MSG("Could not create %s: %s", path.c_str(), strerror(errno));
return false;
}
}
bool stringScan(const std::string &src, const std::string &pattern,
std::deque<std::string> &result){
result.clear();
std::deque<size_t> positions;
size_t pos = pattern.find("%", 0);
@ -27,15 +110,14 @@ namespace Util{
positions.push_back(pos);
pos = pattern.find("%", pos + 1);
}
if (positions.size() == 0){
return false;
}
if (positions.size() == 0){return false;}
size_t sourcePos = 0;
size_t patternPos = 0;
std::deque<size_t>::iterator posIter = positions.begin();
while (sourcePos != std::string::npos){
// Match first part of the string
if (pattern.substr(patternPos, *posIter - patternPos) != src.substr(sourcePos, *posIter - patternPos)){
if (pattern.substr(patternPos, *posIter - patternPos) !=
src.substr(sourcePos, *posIter - patternPos)){
break;
}
sourcePos += *posIter - patternPos;
@ -108,16 +190,13 @@ namespace Util{
// Simple sizes in bytes
case 2: size = p[offset + 1 + nameLen + 1]; break;
case 3: size = *(uint16_t *)(p + offset + 1 + nameLen + 1); break;
case 4:
size = Bit::btoh24(p+offset+1+nameLen+1);
break;
case 4: size = Bit::btoh24(p + offset + 1 + nameLen + 1); break;
case 5: size = *(uint32_t *)(p + offset + 1 + nameLen + 1); break;
default:
WARN_MSG("Unhandled field data size!");
break;
default: WARN_MSG("Unhandled field data size!"); break;
}
fields[fieldName] = RelAccXFieldData(fieldType, size, dataOffset);
DONTEVEN_MSG("Field %s: type %u, size %lu, offset %lu", fieldName.c_str(), fieldType, size, dataOffset);
DONTEVEN_MSG("Field %s: type %u, size %lu, offset %lu", fieldName.c_str(), fieldType, size,
dataOffset);
dataOffset += size;
offset += nameLen + typeLen + 1;
}
@ -138,7 +217,9 @@ namespace Util{
/// Gets the number of records present
/// Defaults to the record count if set to zero.
uint32_t RelAccX::getPresent() const{return (RAXHDR_PRESENT ? RAXHDR_PRESENT : RAXHDR_RECORDCNT);}
uint32_t RelAccX::getPresent() const{
return (RAXHDR_PRESENT ? RAXHDR_PRESENT : RAXHDR_RECORDCNT);
}
/// Gets the offset from the structure start where records begin.
uint16_t RelAccX::getOffset() const{return *(uint16_t *)(p + 26);}
@ -162,8 +243,10 @@ namespace Util{
}
/// Converts the given record number into an offset of records after getOffset()'s offset.
///Does no bounds checking whatsoever, allowing access to not-yet-created or already-deleted records.
///This access method is stable with changing start/end positions and present record counts, because it only
/// Does no bounds checking whatsoever, allowing access to not-yet-created or already-deleted
/// records.
/// This access method is stable with changing start/end positions and present record counts,
/// because it only
/// depends on the record count, which may not change for ring buffers.
uint32_t RelAccX::getRecordPosition(uint64_t recordNo) const{
if (getRCount()){
@ -224,15 +307,16 @@ namespace Util{
return 0; // Not an integer type, or not implemented
}
std::string RelAccX::toPrettyString() const{
std::stringstream r;
uint64_t delled = getDeleted();
uint64_t max = delled + getRCount();
r << "RelAccX: " << getRCount() << " x " << getRSize() << "b @" << getOffset() << " (#" << getDeleted() << " - #" << (getDeleted()+getPresent()-1) << ")" << std::endl;
r << "RelAccX: " << getRCount() << " x " << getRSize() << "b @" << getOffset() << " (#"
<< getDeleted() << " - #" << (getDeleted() + getPresent() - 1) << ")" << std::endl;
for (uint64_t i = delled; i < max; ++i){
r << " #" << i << ":" << std::endl;
for (std::map<std::string, RelAccXFieldData>::const_iterator it = fields.begin(); it != fields.end(); ++it){
for (std::map<std::string, RelAccXFieldData>::const_iterator it = fields.begin();
it != fields.end(); ++it){
r << " " << it->first << ": ";
switch (it->second.type & 0xF0){
case RAX_INT: r << (int64_t)getInt(it->first, i) << std::endl; break;
@ -267,7 +351,8 @@ namespace Util{
return;
}
if (!name.size() || name.size() > 31){
WARN_MSG("Attempting to add a field with illegal name: %s (%u chars)", name.c_str(), name.size());
WARN_MSG("Attempting to add a field with illegal name: %s (%u chars)", name.c_str(),
name.size());
return;
}
// calculate fLen if missing
@ -326,7 +411,6 @@ namespace Util{
/// Defaults to the record count if set to zero.
void RelAccX::setPresent(uint32_t n){RAXHDR_PRESENT = n;}
/// Sets the ready flag.
/// After calling this function, addField() may no longer be called.
/// Fails if exit, reload or ready are (already) set.
@ -396,8 +480,10 @@ namespace Util{
WARN_MSG("Setting non-integer %s", name.c_str());
}
///Updates the deleted record counter, the start position and the present record counter, shifting the ring buffer start position forward without moving the ring buffer end position.
///If the records present counter would be pushed into the negative by this function, sets it to zero, defaulting it to the record count for all relevant purposes.
/// Updates the deleted record counter, the start position and the present record counter,
/// shifting the ring buffer start position forward without moving the ring buffer end position.
/// If the records present counter would be pushed into the negative by this function, sets it to
/// zero, defaulting it to the record count for all relevant purposes.
void RelAccX::deleteRecords(uint32_t amount){
uint32_t &startPos = RAXHDR_STARTPOS;
uint64_t &deletedRecs = RAXHDR_DELETED;
@ -411,8 +497,10 @@ namespace Util{
}
}
///Updates the present record counter, shifting the ring buffer end position forward without moving the ring buffer start position.
///If the records present counter would be pushed past the record counter by this function, sets it to zero, defaulting it to the record count for all relevant purposes.
/// Updates the present record counter, shifting the ring buffer end position forward without
/// moving the ring buffer start position.
/// If the records present counter would be pushed past the record counter by this function, sets
/// it to zero, defaulting it to the record count for all relevant purposes.
void RelAccX::addRecords(uint32_t amount){
uint32_t &recsPresent = RAXHDR_PRESENT;
uint32_t &recordsCount = RAXHDR_RECORDCNT;
@ -422,6 +510,5 @@ namespace Util{
recsPresent += amount;
}
}
}

View file

@ -1,10 +1,14 @@
#include <string>
#include <deque>
#include <map>
#include <stdint.h>
#include <string>
namespace Util{
bool stringScan(const std::string & src, const std::string & pattern, std::deque<std::string> & result);
bool isDirectory(const std::string &path);
bool createPathFor(const std::string &file);
bool createPath(const std::string &path);
bool stringScan(const std::string &src, const std::string &pattern,
std::deque<std::string> &result);
uint64_t ftell(FILE *stream);
uint64_t fseek(FILE *stream, uint64_t offset, int whence);
@ -37,10 +41,14 @@ namespace Util {
#define RAX_128STRING 0x33
#define RAX_256STRING 0x34
#define RAX_RAW 0x40
#define RAX_256RAW 0x44
#define RAX_512RAW 0x45
/// Reliable Access class.
/// Provides reliable access to memory data structures, using dynamic static offsets and a status field.
/// All internal fields are host byte order (since no out-of-machine accesses happen), except 24 bit fields, which are network byte order.
/// Provides reliable access to memory data structures, using dynamic static offsets and a status
/// field.
/// All internal fields are host byte order (since no out-of-machine accesses happen), except 24
/// bit fields, which are network byte order.
/// Data structure:
/// 1 byte status bit fields (1 = ready, 2 = exit, 4 = reload)
/// 1 byte field_offset (where the field description starts)
@ -53,7 +61,8 @@ namespace Util {
/// @field_offset: offset-field_offset bytes fields:
/// 5 bits field name len (< 32), 3 bits type len (1-5)
/// len bytes field name string (< 32 bytes)
/// 1 byte field type (0x01 = RelAccX, 0x1X = uint, 0x2X = int, 0x3X = string, 0x4X = binary)
/// 1 byte field type (0x01 = RelAccX, 0x1X = uint, 0x2X = int, 0x3X = string, 0x4X =
/// binary)
/// if type-len > 1: rest-of-type-len bytes max len
/// else, for 0xYX:
/// Y=1/2: X+1 bytes maxlen (1-16b)
@ -63,9 +72,11 @@ namespace Util {
/// 0x1X/2X: X+1 bytes (u)int data
/// 0x3X: max maxlen bytes string data, zero term'd
/// 0x4X: maxlen bytes binary data
/// Setting ready means the record size, offset and fields will no longer change. Count may still go up (not down)
/// Setting ready means the record size, offset and fields will no longer change. Count may still
/// go up (not down)
/// Setting exit means the writer has exited, and readers should exit too.
/// Setting reload means the writer needed to change fields, and the pointer should be closed and re-opened through outside means (e.g. closing and re-opening the containing shm page).
/// Setting reload means the writer needed to change fields, and the pointer should be closed and
/// re-opened through outside means (e.g. closing and re-opening the containing shm page).
class RelAccX{
public:
RelAccX(char *data, bool waitReady = true);
@ -98,11 +109,13 @@ namespace Util {
void setInt(const std::string &name, uint64_t val, uint64_t recordNo = 0);
void deleteRecords(uint32_t amount);
void addRecords(uint32_t amount);
protected:
static uint32_t getDefaultSize(uint8_t fType);
private:
char *p;
std::map<std::string, RelAccXFieldData> fields;
};
}