Nextcloud's Groupfolder app support #3

Merged
bartfaik04 merged 2 commits from groupfolder-extension-support into main 2025-06-01 13:06:45 +02:00
8 changed files with 31 additions and 85 deletions

View File

@@ -1,2 +1,2 @@
all:
g++ -lrt -std=c++17 src/main.cpp src/usermanager.cpp src/guarder.cpp -o ncsambawatcher
g++ -lrt -std=c++17 src/main.cpp src/usermanager.cpp -o ncsambawatcher

11
src/definitions.h Normal file
View File

@@ -0,0 +1,11 @@
#ifndef _LOCATIONS_H
#define _LOCATIONS_H
#define LOGFILE "journalctl -u smbd --since now -f"
#define USER_LOG_LOCATION 3
#define SCAN_CMD_USR "docker exec --user www-data nextcloud /var/www/html/occ files:scan --path="
#define SCAN_CMD_GRP "docker exec --user www-data nextcloud /var/www/html/occ groupfolder:scan "
#endif // _LOCATIONS_H

View File

@@ -1 +0,0 @@
#include "guarder.h"

View File

@@ -1,66 +0,0 @@
#ifndef _GUARDER_H
#define _GUARDER_H
#include <sys/types.h>
#include <sys/shm.h>
#include <sys/ipc.h>
#include <sys/sem.h>
#include <unistd.h>
class guarder{
private:
const int shmid;
const int semid;
void semaphoreOp(int op) {
struct sembuf sb{};
sb.sem_num = 0;
sb.sem_op = op;
sb.sem_flg = 0;
semop(semid, &sb, 1);
}
void setFlag(bool value)
{
semaphoreOp(-1);
bool *flag = static_cast<bool *>(shmat(shmid, nullptr, 0));
*flag = value;
shmdt(flag);
semaphoreOp(1);
}
public:
guarder() : shmid(shmget(IPC_PRIVATE, sizeof(bool), 0666 | IPC_CREAT)), semid(semget(IPC_PRIVATE, 1, 0666 | IPC_CREAT))
{
setFlagOff();
}
void setFlagOff()
{
setFlag(false);
}
void setFlagOn()
{
setFlag(true);
}
bool isFlagOn()
{
semaphoreOp(-1);
bool *flag = static_cast<bool *>(shmat(shmid, nullptr, 0));
bool ret = *flag;
shmdt(flag);
semaphoreOp(1);
return ret;
}
~guarder()
{
shmctl(shmid, IPC_RMID, nullptr);
semctl(semid, 0, IPC_RMID);
}
};
#endif // _GUARDER_H

View File

@@ -1,8 +0,0 @@
#ifndef _LOCATIONS_H
#define _LOCATIONS_H
#define LOGFILE "journalctl -u smbd --since now -f"
#define USER_LOG_LOCATION 3
#endif // _LOCATIONS_H

View File

@@ -12,8 +12,6 @@
#include <cstdio>
#include "usermanager.h"
#define SCAN_CMD_USR "docker exec --user www-data nextcloud /var/www/html/occ files:scan --path="
userManager manager;
std::condition_variable cv;
std::mutex mtx;
@@ -30,7 +28,7 @@ void readingThreadFunc()
if (line.find('|') == std::string::npos)
continue;
std::vector<std::string> x = splitLogFile(line, '|');
std::vector<std::string> x = splitString(line, '|');
std::string user(x.at(USER_LOG_LOCATION));
{
@@ -70,7 +68,7 @@ void scannerThreadFunc()
}
else if (child == 0) // child
{
std::string cmd = (std::string(SCAN_CMD_USR) + user);
std::string cmd = userManager::getScanCommandFromUser(user);
execl("/bin/sh", "sh", "-c", cmd.c_str(), static_cast<char *>(nullptr));
std::cerr << "Scan failed" << std::endl;
_exit(EXIT_FAILURE);

View File

@@ -1,9 +1,9 @@
#include "usermanager.h"
std::vector<std::string> splitLogFile(const std::string& input, char delimiter = '|')
std::vector<std::string> splitString(const std::string& str, char delimiter = '|')
{
std::vector<std::string> ret;
std::stringstream ss(input);
std::stringstream ss(str);
std::string token;
while (std::getline(ss, token, delimiter)) {
@@ -12,3 +12,13 @@ std::vector<std::string> splitLogFile(const std::string& input, char delimiter =
return ret;
}
std::string userManager::getScanCommandFromUser(const std::string &user)
{
if (user.find("__groupfolder") != std::string::npos)
{
return std::string(SCAN_CMD_GRP) + splitString(user, '/').back();
}
return std::string(SCAN_CMD_USR) + user;
}

View File

@@ -7,9 +7,9 @@
#include <set>
#include <sstream>
#include <mutex>
#include "locations.h"
#include "definitions.h"
std::vector<std::string> splitLogFile(const std::string& input, char delimiter);
std::vector<std::string> splitString(const std::string& input, char delimiter);
class userManager
{
@@ -19,9 +19,11 @@ private:
public:
static std::string getScanCommandFromUser(const std::string&);
void addUserFromLogLine(std::string &line)
{
addUser(splitLogFile(line, '|').at(USER_LOG_LOCATION));
addUser(splitString(line, '|').at(USER_LOG_LOCATION));
}
void addUser(std::string &user)