Skip to content

Commit

Permalink
XrdS3: add helper class to have extneded attributes in a file - for t…
Browse files Browse the repository at this point in the history
…he time being we don't use it
  • Loading branch information
apeters1971 committed Jun 27, 2024
1 parent 99cdb58 commit bdcb0b1
Show file tree
Hide file tree
Showing 2 changed files with 173 additions and 2 deletions.
144 changes: 143 additions & 1 deletion src/XrdS3/XrdS3Utils.cc
Original file line number Diff line number Diff line change
Expand Up @@ -27,18 +27,22 @@
//------------------------------------------------------------------------------
#include <sys/stat.h>
#include <sys/xattr.h>

#include <sys/types.h>
#include <fcntl.h>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <sstream>
#include <fstream>
#include <algorithm>
//------------------------------------------------------------------------------
#include "XrdPosix/XrdPosixExtern.hh"
//------------------------------------------------------------------------------

namespace S3 {

std::atomic<bool> S3Utils::sFileAttributes=false; // this indicates if we use native xattr (false) or xattr in hidden files

//------------------------------------------------------------------------------
//! Encode a string according to RFC 3986
//!
Expand Down Expand Up @@ -477,4 +481,142 @@ S3Utils::ScanDir(const std::filesystem::path& fullpath, const std::filesystem::p
return sentries.size();
}

S3Utils::FileAttributes::FileAttributes(const std::string& changelogFile) : changelogFile(changelogFile) {
loadChangelog();
}

std::string
S3Utils::FileAttributes::getattr(const std::string& name) {
auto it = attributes.find(name);
if (it != attributes.end()) {
return it->second;
}
return "";
}

void
S3Utils::FileAttributes::setattr(const std::string& name, const std::string& value, bool persist) {
attributes[name] = value;
if (persist) {
saveChangelog("set", name, value);
}
}

std::vector<std::string>
S3Utils::FileAttributes::listattr() {
std::vector<std::string> keys;
for (const auto& pair : attributes) {
keys.push_back(pair.first);
}
return keys;
}

void
S3Utils::FileAttributes::rmattr(const std::string& name) {
attributes.erase(name);
saveChangelog("remove", name);
}

void
S3Utils::FileAttributes::trimChangelog() {
int tempFileDescriptor = open("temp_changelog.txt", O_CREAT | O_WRONLY | O_TRUNC, 0666);
if (tempFileDescriptor == -1) {
std::cerr << "Failed to open temporary changelog file." << std::endl;
return;
}
for (const auto& pair : attributes) {
saveToTempFile(tempFileDescriptor, "set", pair.first, pair.second);
}
close(tempFileDescriptor);
if (unlink(changelogFile.c_str()) == -1) {
std::cerr << "Failed to unlink original changelog file." << std::endl;
return;
}
if (rename("temp_changelog.txt", changelogFile.c_str()) == -1) {
std::cerr << "Failed to rename temporary changelog file." << std::endl;
return;
}
}

void
S3Utils::FileAttributes::loadChangelog() {
int changelogFileDescriptor = open(changelogFile.c_str(), O_RDONLY);
if (changelogFileDescriptor == -1) {
std::cerr << "Failed to open changelog file." << std::endl;
return;
}

char buffer[4096];
ssize_t bytesRead;
std::string jsonBuffer;
while ((bytesRead = read(changelogFileDescriptor, buffer, sizeof(buffer) - 1)) > 0) {
buffer[bytesRead] = '\0'; // Null-terminate the buffer
jsonBuffer += buffer;
}

close(changelogFileDescriptor);

Json::CharReaderBuilder readerBuilder;
Json::CharReader* reader = readerBuilder.newCharReader();
Json::Value root;
std::string errs;
if (!reader->parse(jsonBuffer.c_str(), jsonBuffer.c_str() + jsonBuffer.length(), &root, &errs)) {
std::cerr << "Failed to parse changelog file: " << errs << std::endl;
delete reader;
return;
}

delete reader;

// Process parsed JSON data
for (const auto& entry : root) {
std::string action = entry["action"].asString();
std::string name = entry["name"].asString();
if (action == "set") {
std::string value = entry["value"].asString();
attributes[name] = value;
} else if (action == "remove") {
attributes.erase(name);
}
}
}

void
S3Utils::FileAttributes::saveChangelog(const std::string& action, const std::string& name, const std::string& value) {
Json::Value entry;
entry["action"] = action;
entry["name"] = name;
if (!value.empty()) {
entry["value"] = value;
}
std::string entryStr = entry.toStyledString() + "\n";

int changelogFileDescriptor = open(changelogFile.c_str(), O_CREAT | O_WRONLY | O_APPEND, 0666);
if (changelogFileDescriptor == -1) {
std::cerr << "Failed to open changelog file." << std::endl;
return;
}

if (write(changelogFileDescriptor, entryStr.c_str(), entryStr.size()) == -1) {
std::cerr << "Failed to write to changelog file." << std::endl;
}

close(changelogFileDescriptor);
}

void
S3Utils::FileAttributes::saveToTempFile(int tempFileDescriptor, const std::string& action, const std::string& name, const std::string& value) {
Json::Value entry;
entry["action"] = action;
entry["name"] = name;
if (!value.empty()) {
entry["value"] = value;
}
std::string entryStr = entry.toStyledString() + "\n";

if (write(tempFileDescriptor, entryStr.c_str(), entryStr.size()) == -1) {
std::cerr << "Failed to write to temporary changelog file." << std::endl;
}
}

} // namespace S3
31 changes: 30 additions & 1 deletion src/XrdS3/XrdS3Utils.hh
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,13 @@
#include <functional>
#include <iomanip>
#include <map>
#include <mutex>
#include <atomic>
#include <vector>
#include <numeric>
#include <sstream>
#include <string>
#include <json/json.h>
//------------------------------------------------------------------------------

namespace S3 {
Expand Down Expand Up @@ -164,7 +167,33 @@ class S3Utils {

static int ScanDir(const std::filesystem::path &path, const std::filesystem::path &basepath, std::vector<S3Utils::BasicPath>& entries);

private:

static std::atomic<bool> sFileAttributes;

class FileAttributes {
public:
FileAttributes(const std::string& changelogFile);

std::string getattr(const std::string& name);
void setattr(const std::string& name, const std::string& value, bool persist=false);
void persist();
std::vector<std::string> listattr();
void rmattr(const std::string& name);
void trimChangelog();

private:
std::map<std::string, std::string> attributes;
std::deque<std::string> changes;
std::string changelogFile;
std::mutex mutex;

void loadChangelog();
void saveChangelog(const std::string& action, const std::string& name, const std::string& value = "");
void saveChangeslog(const std::string& action);
void saveToTempFile(int tempFileDescriptor, const std::string& action, const std::string& name, const std::string& value = "");
};

private:
std::bitset<256> mEncoder;
std::bitset<256> mObjectEncoder;
unsigned char mDecoder[256];
Expand Down

0 comments on commit bdcb0b1

Please sign in to comment.