From 13f18f99ea2042f523de55da6e12e98d4e887b8e Mon Sep 17 00:00:00 2001 From: bartfaik04 Date: Mon, 2 Jun 2025 13:02:52 +0200 Subject: [PATCH 01/29] Better makefile (AI created) --- .gitignore | 4 +++- Makefile | 33 +++++++++++++++++++++++++++++++-- 2 files changed, 34 insertions(+), 3 deletions(-) diff --git a/.gitignore b/.gitignore index 2ebe5e7..e5ce47e 100644 --- a/.gitignore +++ b/.gitignore @@ -89,4 +89,6 @@ dkms.conf settings.json watch.c ncwatchfile -ncsambawatcher \ No newline at end of file +ncsambawatcher + +obj/ \ No newline at end of file diff --git a/Makefile b/Makefile index fc542f6..2fa8abc 100644 --- a/Makefile +++ b/Makefile @@ -1,2 +1,31 @@ -all: - g++ -lrt -std=c++17 src/main.cpp src/usermanager.cpp -o ncsambawatcher \ No newline at end of file +# Compiler and flags +CXX := g++ +CXXFLAGS := -std=c++17 -Wall -Wextra -O2 + +# Directories +SRC_DIR := src +OBJ_DIR := obj +BUILD_DIR := . +TARGET := $(BUILD_DIR)/ncsambawatcher + +# Create list of source and object files +SRCS := $(wildcard $(SRC_DIR)/*.cpp) +OBJS := $(SRCS:$(SRC_DIR)/%.cpp=$(OBJ_DIR)/%.o) + +# Default target +all: $(TARGET) + +# Link object files into final binary +$(TARGET): $(OBJS) + $(CXX) $(CXXFLAGS) -o $@ $^ + +# Compile .cpp to .o into obj/ +$(OBJ_DIR)/%.o: $(SRC_DIR)/%.cpp + @mkdir -p $(OBJ_DIR) + $(CXX) $(CXXFLAGS) -c $< -o $@ + +# Clean build artifacts +clean: + rm -f $(OBJ_DIR)/*.o $(TARGET) + +.PHONY: all clean From c494e9697c867cc97e3f2fddaac850435a1ec049 Mon Sep 17 00:00:00 2001 From: bartfaik04 Date: Mon, 2 Jun 2025 13:18:02 +0200 Subject: [PATCH 02/29] Modify config files --- configs/ncsambawatcher.config.default | 1 + configs/smb.24.04.conf | 46 ++++++++++++++++++++++----- 2 files changed, 39 insertions(+), 8 deletions(-) create mode 100644 configs/ncsambawatcher.config.default diff --git a/configs/ncsambawatcher.config.default b/configs/ncsambawatcher.config.default new file mode 100644 index 0000000..93e4ee9 --- /dev/null +++ b/configs/ncsambawatcher.config.default @@ -0,0 +1 @@ +NEXTCLOUD_CONTAINER_NAME=nextcloud diff --git a/configs/smb.24.04.conf b/configs/smb.24.04.conf index 78643cd..13bce10 100644 --- a/configs/smb.24.04.conf +++ b/configs/smb.24.04.conf @@ -1,5 +1,4 @@ [global] - vfs objects = full_audit full_audit:prefix = %u|%I|%m|%S full_audit:success = mkdirat unlinkat renameat write @@ -7,12 +6,43 @@ full_audit:facility = local5 full_audit:priority = NOTICE -# Put this line only for the groupfolder's share -[Some gorupfolder share] +# Example usershare +[] #CHANGEME + path = /path/to/nextcloud/data//files/ #CHANGEME + valid users = #CHANGEME + force user = www-data + force group = www-data + create mask = 0755 + force create mode = 0755 + directory mask = 0755 + force directory mode = 0755 + guest ok = no + public = no + writable = yes + browsable = yes + hide dot files = no + inherit owner = yes + hide unreadable = no - full_audit:prefix = %u|%I|%m|__groupfolders/ +# Example groupfolder share +[Sharename] + path = /path/to/nextcloud/data/__groupfolders/ #CHANGEME + valid users = usernames #CHANGEME + force user = www-data + force group = www-data + create mask = 0755 + force create mode = 0755 + directory mask = 0755 + force directory mode = 0755 + guest ok = no + public = no + writable = yes + browsable = yes + hide dot files = no + inherit owner = yes + hide unreadable = no + full_audit:prefix = %u|%I|%m|__groupfolders/ #CHANGEME -# To disable logs for a specific share -[A share] - - vfs objects = \ No newline at end of file +# To disable logs for a specific share, add this line to that share +[Sharename] + vfs objects = \ No newline at end of file From 2951b4868000f013c982ca7aaaab1c039172ad05 Mon Sep 17 00:00:00 2001 From: bartfaik04 Date: Mon, 2 Jun 2025 13:39:06 +0200 Subject: [PATCH 03/29] Added CI --- .gitea/workflows/ci.yml | 58 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100644 .gitea/workflows/ci.yml diff --git a/.gitea/workflows/ci.yml b/.gitea/workflows/ci.yml new file mode 100644 index 0000000..c14b36e --- /dev/null +++ b/.gitea/workflows/ci.yml @@ -0,0 +1,58 @@ +name: CI + +on: + push: {} + +jobs: + build: + runs-on: ubuntu-24.04 + steps: + - name: Checkout code + uses: actions/checkout@v3 + + - name: Install build tools + run: sudo apt update && sudo apt install -y build-essential zip + + - name: Compile project + run: make + + - name: Save build output + uses: actions/upload-artifact@v3 + with: + name: ncsambawatcher + path: ./ncsambawatcher + + release: + if: startsWith(github.ref, 'refs/tags/') + runs-on: ubuntu-24.04 + needs: build + steps: + - name: Checkout code + uses: actions/checkout@v3 + + - name: Download compiled binary + uses: actions/download-artifact@v3 + with: + name: ncsambawatcher + + - name: Copy files + run: | + mkdir build + cp ncsambawatcher build/ncsambawatcher + cp configs/ncsambawatcher.config.default build/ncsambawatcher.config + cp init.sh config/init.sh + + - name: Create release zip + run: | + cd build + zip ../ncsambawatcher.zip ./* + + - name: Publish release + uses: actions/create-release@v1 + env: + GITEA_TOKEN: ${{ secrets.GITEA_TOKEN }} + with: + tag_name: ${{ github.ref_name }} + release_name: Release ${{ github.ref_name }} + files: | + ncsambawatcher.zip From 15fe1b0e9b09b79a46ee6d320bf402f8d97013c6 Mon Sep 17 00:00:00 2001 From: bartfaik04 Date: Mon, 2 Jun 2025 13:42:25 +0200 Subject: [PATCH 04/29] Update ci --- .gitea/workflows/ci.yml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.gitea/workflows/ci.yml b/.gitea/workflows/ci.yml index c14b36e..468e79f 100644 --- a/.gitea/workflows/ci.yml +++ b/.gitea/workflows/ci.yml @@ -6,6 +6,7 @@ on: jobs: build: runs-on: ubuntu-24.04 + tags: [ubuntu] steps: - name: Checkout code uses: actions/checkout@v3 @@ -25,6 +26,7 @@ jobs: release: if: startsWith(github.ref, 'refs/tags/') runs-on: ubuntu-24.04 + tags: [ubuntu] needs: build steps: - name: Checkout code From bad5123b029ef025f3fe6d0c692942aa68e2f5d3 Mon Sep 17 00:00:00 2001 From: bartfaik04 Date: Mon, 2 Jun 2025 13:45:37 +0200 Subject: [PATCH 05/29] Update ci --- .gitea/workflows/ci.yml | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/.gitea/workflows/ci.yml b/.gitea/workflows/ci.yml index 468e79f..9109484 100644 --- a/.gitea/workflows/ci.yml +++ b/.gitea/workflows/ci.yml @@ -5,8 +5,7 @@ on: jobs: build: - runs-on: ubuntu-24.04 - tags: [ubuntu] + runs-on: ubuntu steps: - name: Checkout code uses: actions/checkout@v3 @@ -25,8 +24,7 @@ jobs: release: if: startsWith(github.ref, 'refs/tags/') - runs-on: ubuntu-24.04 - tags: [ubuntu] + runs-on: ubuntu needs: build steps: - name: Checkout code From a2058ce2d69112ae03d580201a4fdd131f1b4343 Mon Sep 17 00:00:00 2001 From: bartfaik04 Date: Mon, 2 Jun 2025 13:53:54 +0200 Subject: [PATCH 06/29] Update ci.yml --- .gitea/workflows/ci.yml | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/.gitea/workflows/ci.yml b/.gitea/workflows/ci.yml index 9109484..34617a9 100644 --- a/.gitea/workflows/ci.yml +++ b/.gitea/workflows/ci.yml @@ -7,8 +7,8 @@ jobs: build: runs-on: ubuntu steps: - - name: Checkout code - uses: actions/checkout@v3 + #- name: Checkout code + # uses: actions/checkout@v3 - name: Install build tools run: sudo apt update && sudo apt install -y build-essential zip @@ -27,8 +27,8 @@ jobs: runs-on: ubuntu needs: build steps: - - name: Checkout code - uses: actions/checkout@v3 + #- name: Checkout code + # uses: actions/checkout@v3 - name: Download compiled binary uses: actions/download-artifact@v3 From cd5287163271a6faaa4a3584781636c023a783ed Mon Sep 17 00:00:00 2001 From: bartfaik04 Date: Mon, 2 Jun 2025 13:55:27 +0200 Subject: [PATCH 07/29] Remove sudo from ci.yml --- .gitea/workflows/ci.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.gitea/workflows/ci.yml b/.gitea/workflows/ci.yml index 34617a9..72b4b33 100644 --- a/.gitea/workflows/ci.yml +++ b/.gitea/workflows/ci.yml @@ -11,7 +11,7 @@ jobs: # uses: actions/checkout@v3 - name: Install build tools - run: sudo apt update && sudo apt install -y build-essential zip + run: apt update && apt install -y build-essential zip - name: Compile project run: make From c0a1b0bbd5b9befcbe7a6010f1d70a8e1dbbbcb4 Mon Sep 17 00:00:00 2001 From: bartfaik04 Date: Mon, 2 Jun 2025 16:21:20 +0200 Subject: [PATCH 08/29] Separate the ci jobs --- .gitea/workflows/build.yml | 27 ++++++++++++++++ .gitea/workflows/{ci.yml => release.yml} | 39 +++++++++--------------- 2 files changed, 42 insertions(+), 24 deletions(-) create mode 100644 .gitea/workflows/build.yml rename .gitea/workflows/{ci.yml => release.yml} (63%) diff --git a/.gitea/workflows/build.yml b/.gitea/workflows/build.yml new file mode 100644 index 0000000..52a9abd --- /dev/null +++ b/.gitea/workflows/build.yml @@ -0,0 +1,27 @@ +name: Build + +on: + push: {} + +defaults: + run: + shell: bash + working-directory: . + +jobs: + build: + runs-on: ubuntu + container: + image: ubuntu:24.04 + steps: + - name: Install build tools + run: apt update && apt install -y build-essential + + - name: Compile project + run: make + + - name: Save build output + uses: actions/upload-artifact@v3 + with: + name: ncsambawatcher + path: ./ncsambawatcher \ No newline at end of file diff --git a/.gitea/workflows/ci.yml b/.gitea/workflows/release.yml similarity index 63% rename from .gitea/workflows/ci.yml rename to .gitea/workflows/release.yml index 72b4b33..8e26b4f 100644 --- a/.gitea/workflows/ci.yml +++ b/.gitea/workflows/release.yml @@ -1,35 +1,26 @@ -name: CI +name: Release on: - push: {} + push: + tags: + - v.* -jobs: - build: - runs-on: ubuntu - steps: - #- name: Checkout code - # uses: actions/checkout@v3 - - - name: Install build tools - run: apt update && apt install -y build-essential zip - - - name: Compile project - run: make - - - name: Save build output - uses: actions/upload-artifact@v3 - with: - name: ncsambawatcher - path: ./ncsambawatcher +defaults: + run: + shell: bash + working-directory: . +jobs: release: - if: startsWith(github.ref, 'refs/tags/') runs-on: ubuntu needs: build + container: + image: ubuntu:24.04 steps: - #- name: Checkout code - # uses: actions/checkout@v3 - + steps: + - name: Install build tools + run: apt update && apt install -y zip + - name: Download compiled binary uses: actions/download-artifact@v3 with: From bd9ccd33eff5632b7d692f021f91c044846097ac Mon Sep 17 00:00:00 2001 From: bartfaik04 Date: Mon, 2 Jun 2025 16:27:29 +0200 Subject: [PATCH 09/29] Cloning project in CI --- .gitea/workflows/build.yml | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/.gitea/workflows/build.yml b/.gitea/workflows/build.yml index 52a9abd..352f10d 100644 --- a/.gitea/workflows/build.yml +++ b/.gitea/workflows/build.yml @@ -14,8 +14,11 @@ jobs: container: image: ubuntu:24.04 steps: - - name: Install build tools - run: apt update && apt install -y build-essential + - name: Install tools + run: apt update && apt install -y build-essential git + + - name: Clone repository + run: git clone "${GITEA_REPOSITORY_URL}" project && cp -r project/* ./ - name: Compile project run: make From 754cc29a83de0b255f3913b4eaf41b18733b59d1 Mon Sep 17 00:00:00 2001 From: bartfaik04 Date: Mon, 2 Jun 2025 16:30:25 +0200 Subject: [PATCH 10/29] Use Github's checkout instead of apt git --- .gitea/workflows/build.yml | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/.gitea/workflows/build.yml b/.gitea/workflows/build.yml index 352f10d..ab26cb8 100644 --- a/.gitea/workflows/build.yml +++ b/.gitea/workflows/build.yml @@ -14,11 +14,11 @@ jobs: container: image: ubuntu:24.04 steps: + - name: Checkout code + uses: actions/checkout@v3 + - name: Install tools - run: apt update && apt install -y build-essential git - - - name: Clone repository - run: git clone "${GITEA_REPOSITORY_URL}" project && cp -r project/* ./ + run: apt update && apt install -y build-essential - name: Compile project run: make From e95d9bb55275920ba4f19ffcf547be44d60236d5 Mon Sep 17 00:00:00 2001 From: bartfaik04 Date: Mon, 2 Jun 2025 16:34:25 +0200 Subject: [PATCH 11/29] Change ubuntu image to node --- .gitea/workflows/build.yml | 2 +- .gitea/workflows/release.yml | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/.gitea/workflows/build.yml b/.gitea/workflows/build.yml index ab26cb8..f5cd41d 100644 --- a/.gitea/workflows/build.yml +++ b/.gitea/workflows/build.yml @@ -12,7 +12,7 @@ jobs: build: runs-on: ubuntu container: - image: ubuntu:24.04 + image: node:20 steps: - name: Checkout code uses: actions/checkout@v3 diff --git a/.gitea/workflows/release.yml b/.gitea/workflows/release.yml index 8e26b4f..df0bd76 100644 --- a/.gitea/workflows/release.yml +++ b/.gitea/workflows/release.yml @@ -15,7 +15,7 @@ jobs: runs-on: ubuntu needs: build container: - image: ubuntu:24.04 + image: node:20 steps: steps: - name: Install build tools From a1b8d589be8e502024adaf510ecf188cb0ac7783 Mon Sep 17 00:00:00 2001 From: bartfaik04 Date: Fri, 6 Jun 2025 21:24:45 +0200 Subject: [PATCH 12/29] Fix release job --- .gitea/workflows/release.yml | 1 - 1 file changed, 1 deletion(-) diff --git a/.gitea/workflows/release.yml b/.gitea/workflows/release.yml index df0bd76..084e811 100644 --- a/.gitea/workflows/release.yml +++ b/.gitea/workflows/release.yml @@ -17,7 +17,6 @@ jobs: container: image: node:20 steps: - steps: - name: Install build tools run: apt update && apt install -y zip From ed068875c38fc8fd87e9ae81fcba8a16ab67d2e0 Mon Sep 17 00:00:00 2001 From: bartfaik04 Date: Fri, 6 Jun 2025 23:52:05 +0200 Subject: [PATCH 13/29] Implemented configfilemanager --- src/configFileManager.h | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 src/configFileManager.h diff --git a/src/configFileManager.h b/src/configFileManager.h new file mode 100644 index 0000000..3c2886c --- /dev/null +++ b/src/configFileManager.h @@ -0,0 +1,40 @@ +#ifndef _CONFIGFILEMANAGER_H +#define _CONFIGFILEMANAGER_H + +#include +#include +#include +#include +#include +#include "usermanager.h" + +class configFileManager{ +private: + std::map configs; + +public: + configFileManager(std::string filepath = "./ncsambawatcher.config") + { + std::ifstream is(filepath); + std::string tmp; + while(std::getline(is, tmp)) + { + std::vector splited = splitString(tmp, '='); + if (splited.size() != 2) + { + std::cerr << "Invalid line: " << tmp << std::endl; + continue; + } + + configs.insert(std::make_pair(splited.at(0), splited.at(1))); + } + } + + std::string at(std::string &config) + { + return configs.at(config); + } + +}; + +#endif // _CONFIGFILEMANAGER_H \ No newline at end of file From 31ec9267933309249b12355d8c25896f0e383687 Mon Sep 17 00:00:00 2001 From: bartfaik04 Date: Sat, 7 Jun 2025 09:26:15 +0200 Subject: [PATCH 14/29] Use configfilemanager to get the real command --- ...onfigFileManager.h => configfilemanager.h} | 16 ++++++--- src/definitions.h | 6 ++-- src/main.cpp | 18 +++++++++- src/usermanager.cpp | 35 ++++++++++--------- src/usermanager.h | 5 ++- 5 files changed, 54 insertions(+), 26 deletions(-) rename src/{configFileManager.h => configfilemanager.h} (68%) diff --git a/src/configFileManager.h b/src/configfilemanager.h similarity index 68% rename from src/configFileManager.h rename to src/configfilemanager.h index 3c2886c..7ebe552 100644 --- a/src/configFileManager.h +++ b/src/configfilemanager.h @@ -6,19 +6,22 @@ #include #include #include -#include "usermanager.h" +#include "definitions.h" -class configFileManager{ +class configfilemanager{ private: std::map configs; public: - configFileManager(std::string filepath = "./ncsambawatcher.config") + configfilemanager(std::string filepath = "./ncsambawatcher.config") { std::ifstream is(filepath); std::string tmp; while(std::getline(is, tmp)) { + if (tmp.at(0) == '#') // ignore comments + continue; + std::vector splited = splitString(tmp, '='); if (splited.size() != 2) { @@ -30,11 +33,16 @@ public: } } - std::string at(std::string &config) + std::string at(const std::string &config) { return configs.at(config); } + std::string at(const char* config) + { + return at(std::string(config)); + } + }; #endif // _CONFIGFILEMANAGER_H \ No newline at end of file diff --git a/src/definitions.h b/src/definitions.h index 4e755b9..c9a6e9d 100644 --- a/src/definitions.h +++ b/src/definitions.h @@ -5,7 +5,9 @@ #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 " +#define SCAN_CMD_USR "docker exec --user www-data %1% /var/www/html/occ files:scan --path=" +#define SCAN_CMD_GRP "docker exec --user www-data %1% /var/www/html/occ groupfolder:scan " + +std::vector splitString(const std::string& input, char delimiter); #endif // _LOCATIONS_H \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp index 8974326..a8b5894 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -10,11 +10,27 @@ #include #include #include +#include "definitions.h" #include "usermanager.h" +#include "configfilemanager.h" userManager manager; std::condition_variable cv; std::mutex mtx; +configfilemanager cfm; + +std::vector splitString(const std::string& str, char delimiter = '|') +{ + std::vector ret; + std::stringstream ss(str); + std::string token; + + while (std::getline(ss, token, delimiter)) { + ret.push_back(token); + } + + return ret; +} void readingThreadFunc() { @@ -68,7 +84,7 @@ void scannerThreadFunc() } else if (child == 0) // child { - std::string cmd = userManager::getScanCommandFromUser(user); + std::string cmd = userManager::getScanCommandFromUser(user, cfm); execl("/bin/sh", "sh", "-c", cmd.c_str(), static_cast(nullptr)); std::cerr << "Scan failed" << std::endl; _exit(EXIT_FAILURE); diff --git a/src/usermanager.cpp b/src/usermanager.cpp index 8d1bf13..86d4ace 100644 --- a/src/usermanager.cpp +++ b/src/usermanager.cpp @@ -1,24 +1,27 @@ #include "usermanager.h" -std::vector splitString(const std::string& str, char delimiter = '|') -{ - std::vector ret; - std::stringstream ss(str); - std::string token; - - while (std::getline(ss, token, delimiter)) { - ret.push_back(token); - } - - return ret; -} - -std::string userManager::getScanCommandFromUser(const std::string &user) +std::string userManager::getScanCommandFromUser(const std::string &user, configfilemanager &cfm) { + std::string contname = cfm.at("NEXTCLOUD_CONTAINER_NAME"); + std::string baseCommand; + std::string userCommand; + if (user.find("__groupfolder") != std::string::npos) { - return std::string(SCAN_CMD_GRP) + splitString(user, '/').back(); + baseCommand = SCAN_CMD_GRP; + userCommand = splitString(user, '/').back(); + } + else + { + baseCommand = SCAN_CMD_USR; + userCommand = user; } - return std::string(SCAN_CMD_USR) + user; + size_t pos = 0; + while ((pos = baseCommand.find("%1%", pos)) != std::string::npos) { + baseCommand.replace(pos, contname.length(), contname); + pos += contname.length(); // Move past the replacement + } + + return baseCommand + userCommand; } \ No newline at end of file diff --git a/src/usermanager.h b/src/usermanager.h index ff4c4b4..799c90b 100644 --- a/src/usermanager.h +++ b/src/usermanager.h @@ -8,8 +8,7 @@ #include #include #include "definitions.h" - -std::vector splitString(const std::string& input, char delimiter); +#include "configfilemanager.h" class userManager { @@ -19,7 +18,7 @@ private: public: - static std::string getScanCommandFromUser(const std::string&); + static std::string getScanCommandFromUser(const std::string&, configfilemanager& cfm); void addUserFromLogLine(std::string &line) { From f8b66b4e07fa25a0ace47d7d796eda7fc22f1a18 Mon Sep 17 00:00:00 2001 From: bartfaik04 Date: Sat, 7 Jun 2025 10:12:36 +0200 Subject: [PATCH 15/29] Check file persistence --- src/configfilemanager.cpp | 1 + src/configfilemanager.h | 13 +++++++++++-- 2 files changed, 12 insertions(+), 2 deletions(-) create mode 100644 src/configfilemanager.cpp diff --git a/src/configfilemanager.cpp b/src/configfilemanager.cpp new file mode 100644 index 0000000..f8a871f --- /dev/null +++ b/src/configfilemanager.cpp @@ -0,0 +1 @@ +#include "configfilemanager.h" \ No newline at end of file diff --git a/src/configfilemanager.h b/src/configfilemanager.h index 7ebe552..49a836d 100644 --- a/src/configfilemanager.h +++ b/src/configfilemanager.h @@ -16,9 +16,16 @@ public: configfilemanager(std::string filepath = "./ncsambawatcher.config") { std::ifstream is(filepath); - std::string tmp; - while(std::getline(is, tmp)) + if(!is.good()) { + std::cerr << "File not exits: " << filepath << std::endl; + exit(EXIT_FAILURE); + } + std::string tmp; + + while(!is.eof()) + { + std::getline(is, tmp); if (tmp.at(0) == '#') // ignore comments continue; @@ -31,6 +38,8 @@ public: configs.insert(std::make_pair(splited.at(0), splited.at(1))); } + + std::cout << "Config file loaded successfuly" << std::endl; } std::string at(const std::string &config) From 5d4cd30d95cb7bc777703ad96789b0452af73247 Mon Sep 17 00:00:00 2001 From: bartfaik04 Date: Sat, 7 Jun 2025 10:16:27 +0200 Subject: [PATCH 16/29] lock_guard for cfm --- src/configfilemanager.h | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/configfilemanager.h b/src/configfilemanager.h index 49a836d..d9e671a 100644 --- a/src/configfilemanager.h +++ b/src/configfilemanager.h @@ -6,11 +6,14 @@ #include #include #include +#include #include "definitions.h" class configfilemanager{ private: std::map configs; + std::mutex mtx; + public: configfilemanager(std::string filepath = "./ncsambawatcher.config") @@ -44,6 +47,7 @@ public: std::string at(const std::string &config) { + std::lock_guard lock(mtx); return configs.at(config); } From 0a9fe8f4f887a85fe1e44c0846c4eb3a8ba3dcd3 Mon Sep 17 00:00:00 2001 From: bartfaik04 Date: Sat, 7 Jun 2025 11:21:51 +0200 Subject: [PATCH 17/29] Fix replacement --- src/configfilemanager.h | 1 + src/main.cpp | 2 +- src/usermanager.cpp | 5 +++-- 3 files changed, 5 insertions(+), 3 deletions(-) diff --git a/src/configfilemanager.h b/src/configfilemanager.h index d9e671a..4487ef2 100644 --- a/src/configfilemanager.h +++ b/src/configfilemanager.h @@ -29,6 +29,7 @@ public: while(!is.eof()) { std::getline(is, tmp); + std::cout << tmp << std::endl; if (tmp.at(0) == '#') // ignore comments continue; diff --git a/src/main.cpp b/src/main.cpp index a8b5894..60e7691 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -14,10 +14,10 @@ #include "usermanager.h" #include "configfilemanager.h" +configfilemanager cfm; userManager manager; std::condition_variable cv; std::mutex mtx; -configfilemanager cfm; std::vector splitString(const std::string& str, char delimiter = '|') { diff --git a/src/usermanager.cpp b/src/usermanager.cpp index 86d4ace..163d5b2 100644 --- a/src/usermanager.cpp +++ b/src/usermanager.cpp @@ -5,6 +5,7 @@ std::string userManager::getScanCommandFromUser(const std::string &user, configf std::string contname = cfm.at("NEXTCLOUD_CONTAINER_NAME"); std::string baseCommand; std::string userCommand; + std::string placeholder("%1%"); if (user.find("__groupfolder") != std::string::npos) { @@ -18,8 +19,8 @@ std::string userManager::getScanCommandFromUser(const std::string &user, configf } size_t pos = 0; - while ((pos = baseCommand.find("%1%", pos)) != std::string::npos) { - baseCommand.replace(pos, contname.length(), contname); + while ((pos = baseCommand.find(placeholder, pos)) != std::string::npos) { + baseCommand.replace(pos, placeholder.length(), contname); pos += contname.length(); // Move past the replacement } From e98a8daad709f4bd2fb78f00f3270ecdcb5289f2 Mon Sep 17 00:00:00 2001 From: bartfaik04 Date: Sat, 7 Jun 2025 11:23:01 +0200 Subject: [PATCH 18/29] Missing file for the last commit --- configs/ncsambawatcher.config.default | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/configs/ncsambawatcher.config.default b/configs/ncsambawatcher.config.default index 93e4ee9..22c7d0e 100644 --- a/configs/ncsambawatcher.config.default +++ b/configs/ncsambawatcher.config.default @@ -1 +1 @@ -NEXTCLOUD_CONTAINER_NAME=nextcloud +NEXTCLOUD_CONTAINER_NAME=nextcloud \ No newline at end of file From f3d93e1e464f5e275e61fa26e5e923637aa29bb8 Mon Sep 17 00:00:00 2001 From: bartfaik04 Date: Sat, 7 Jun 2025 11:23:14 +0200 Subject: [PATCH 19/29] Merge 2 workflows into one --- .gitea/workflows/build.yml | 30 ---------------- .gitea/workflows/{release.yml => ci.yml} | 36 +++++++++++++++---- .gitignore | 3 +- ...service => ncsambawatcher.service.default} | 5 ++- init.sh | 8 ++--- 5 files changed, 37 insertions(+), 45 deletions(-) delete mode 100644 .gitea/workflows/build.yml rename .gitea/workflows/{release.yml => ci.yml} (58%) rename configs/{ncsambawatcher.service => ncsambawatcher.service.default} (66%) diff --git a/.gitea/workflows/build.yml b/.gitea/workflows/build.yml deleted file mode 100644 index f5cd41d..0000000 --- a/.gitea/workflows/build.yml +++ /dev/null @@ -1,30 +0,0 @@ -name: Build - -on: - push: {} - -defaults: - run: - shell: bash - working-directory: . - -jobs: - build: - runs-on: ubuntu - container: - image: node:20 - steps: - - name: Checkout code - uses: actions/checkout@v3 - - - name: Install tools - run: apt update && apt install -y build-essential - - - name: Compile project - run: make - - - name: Save build output - uses: actions/upload-artifact@v3 - with: - name: ncsambawatcher - path: ./ncsambawatcher \ No newline at end of file diff --git a/.gitea/workflows/release.yml b/.gitea/workflows/ci.yml similarity index 58% rename from .gitea/workflows/release.yml rename to .gitea/workflows/ci.yml index 084e811..4667d9c 100644 --- a/.gitea/workflows/release.yml +++ b/.gitea/workflows/ci.yml @@ -1,25 +1,48 @@ -name: Release +name: CI on: push: tags: - - v.* + - '*' # Triggers on all tags + branches: + - '**' # Triggers on all branches defaults: run: shell: bash working-directory: . -jobs: - release: +jobs: + build: runs-on: ubuntu + container: + image: node:20 + steps: + - name: Checkout code + uses: actions/checkout@v3 + + - name: Install tools + run: apt update && apt install -y build-essential + + - name: Compile project + run: make + + - name: Save build output + uses: actions/upload-artifact@v3 + with: + name: ncsambawatcher + path: ./ncsambawatcher + + release: + if: startsWith(github.ref, 'refs/tags/') needs: build + runs-on: ubuntu container: image: node:20 steps: - name: Install build tools run: apt update && apt install -y zip - + - name: Download compiled binary uses: actions/download-artifact@v3 with: @@ -30,7 +53,8 @@ jobs: mkdir build cp ncsambawatcher build/ncsambawatcher cp configs/ncsambawatcher.config.default build/ncsambawatcher.config - cp init.sh config/init.sh + cp configs/ncsambawatcher.service.default build/ncsambawatcher.service + cp init.sh build/init.sh - name: Create release zip run: | diff --git a/.gitignore b/.gitignore index e5ce47e..7a33641 100644 --- a/.gitignore +++ b/.gitignore @@ -91,4 +91,5 @@ watch.c ncwatchfile ncsambawatcher -obj/ \ No newline at end of file +obj/ +build/ \ No newline at end of file diff --git a/configs/ncsambawatcher.service b/configs/ncsambawatcher.service.default similarity index 66% rename from configs/ncsambawatcher.service rename to configs/ncsambawatcher.service.default index f4f35f3..e6e3d42 100644 --- a/configs/ncsambawatcher.service +++ b/configs/ncsambawatcher.service.default @@ -4,12 +4,11 @@ After=network.target docker.service Requires=docker.service [Service] -ExecStart=/usr/bin/ncsambawatcher +ExecStart=/path/to/folder/ncsambawatcher +WorkingDirectory=/path/to/folder/ Restart=always User=root Group=root -WorkingDirectory=/usr/bin/ -Environment=PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin StandardOutput=journal StandardError=journal SyslogIdentifier=ncsambawatcher diff --git a/init.sh b/init.sh index 7f4e072..5e98a4c 100755 --- a/init.sh +++ b/init.sh @@ -1,11 +1,9 @@ #!/bin/bash -make +current_dir=$(pwd) +sed -i "s|/path/to/folder/|$current_dir/|g" ncsambawatcher.service -sudo cp ncsambawatcher /usr/bin/ -sudo chmod +x /usr/bin/ncsambawatcher - -sudo cp configs/ncsambawatcher.service /etc/systemd/system +sudo cp ./ncsambawatcher.service /etc/systemd/system sudo systemctl daemon-reload sudo systemctl enable ncsambawatcher.service From 14305081214df8fe7fcaff71763e1e61f674eb37 Mon Sep 17 00:00:00 2001 From: bartfaik04 Date: Sat, 7 Jun 2025 11:57:17 +0200 Subject: [PATCH 20/29] Checkout in release job --- .gitea/workflows/ci.yml | 3 +++ 1 file changed, 3 insertions(+) diff --git a/.gitea/workflows/ci.yml b/.gitea/workflows/ci.yml index 4667d9c..f9170f5 100644 --- a/.gitea/workflows/ci.yml +++ b/.gitea/workflows/ci.yml @@ -40,6 +40,9 @@ jobs: container: image: node:20 steps: + - name: Checkout code + uses: actions/checkout@v3 + - name: Install build tools run: apt update && apt install -y zip From c59e7532c2b21ef0ded9da4fa83de9ce9562859e Mon Sep 17 00:00:00 2001 From: bartfaik04 Date: Sat, 7 Jun 2025 12:07:51 +0200 Subject: [PATCH 21/29] Change secret --- .gitea/workflows/ci.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.gitea/workflows/ci.yml b/.gitea/workflows/ci.yml index f9170f5..8afa886 100644 --- a/.gitea/workflows/ci.yml +++ b/.gitea/workflows/ci.yml @@ -67,7 +67,7 @@ jobs: - name: Publish release uses: actions/create-release@v1 env: - GITEA_TOKEN: ${{ secrets.GITEA_TOKEN }} + GITEA_TOKEN: ${{ secrets.RELEASE_SECRET }} with: tag_name: ${{ github.ref_name }} release_name: Release ${{ github.ref_name }} From 29746299952287b4c278699d07ad5a71551d8385 Mon Sep 17 00:00:00 2001 From: bartfaik04 Date: Sat, 7 Jun 2025 13:46:15 +0200 Subject: [PATCH 22/29] Change ci.yml --- .gitea/workflows/ci.yml | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/.gitea/workflows/ci.yml b/.gitea/workflows/ci.yml index 8afa886..5a1fc03 100644 --- a/.gitea/workflows/ci.yml +++ b/.gitea/workflows/ci.yml @@ -67,9 +67,10 @@ jobs: - name: Publish release uses: actions/create-release@v1 env: - GITEA_TOKEN: ${{ secrets.RELEASE_SECRET }} + GITHUB_TOKEN: ${{ secrets.RELEASE_SECRET }} with: tag_name: ${{ github.ref_name }} release_name: Release ${{ github.ref_name }} files: | ncsambawatcher.zip + draft: true From 45c11829474c58f7f0ca548ca6f45b8a31f0da13 Mon Sep 17 00:00:00 2001 From: bartfaik04 Date: Sat, 7 Jun 2025 13:51:24 +0200 Subject: [PATCH 23/29] Add debug for ci release --- .gitea/workflows/ci.yml | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/.gitea/workflows/ci.yml b/.gitea/workflows/ci.yml index 5a1fc03..a9be96b 100644 --- a/.gitea/workflows/ci.yml +++ b/.gitea/workflows/ci.yml @@ -62,7 +62,10 @@ jobs: - name: Create release zip run: | cd build - zip ../ncsambawatcher.zip ./* + ls -al + zip -rv ../ncsambawatcher.zip ./* + cd ../ + ls -al - name: Publish release uses: actions/create-release@v1 @@ -71,6 +74,5 @@ jobs: with: tag_name: ${{ github.ref_name }} release_name: Release ${{ github.ref_name }} - files: | - ncsambawatcher.zip + files: ./ncsambawatcher.zip draft: true From 2d1eed4289648ef382e1a18bba59a57cfb79a7b7 Mon Sep 17 00:00:00 2001 From: bartfaik04 Date: Sat, 7 Jun 2025 14:01:57 +0200 Subject: [PATCH 24/29] Change to akkuman's release action --- .gitea/workflows/ci.yml | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/.gitea/workflows/ci.yml b/.gitea/workflows/ci.yml index a9be96b..7125306 100644 --- a/.gitea/workflows/ci.yml +++ b/.gitea/workflows/ci.yml @@ -68,11 +68,12 @@ jobs: ls -al - name: Publish release - uses: actions/create-release@v1 + uses: akkuman/gitea-release-action@v1 env: GITHUB_TOKEN: ${{ secrets.RELEASE_SECRET }} with: tag_name: ${{ github.ref_name }} - release_name: Release ${{ github.ref_name }} + name: Release ${{ github.ref_name }} files: ./ncsambawatcher.zip + token: ${{ secrets.RELEASE_SECRET }} draft: true From 3db83f14feb212b3e6bbdb6a5fd33ef2d52ad974 Mon Sep 17 00:00:00 2001 From: bartfaik04 Date: Sat, 7 Jun 2025 14:33:00 +0200 Subject: [PATCH 25/29] Add users from configfile --- configs/ncsambawatcher.config.default | 3 ++- src/main.cpp | 2 ++ src/usermanager.h | 30 ++++++++++++++++++++++----- 3 files changed, 29 insertions(+), 6 deletions(-) diff --git a/configs/ncsambawatcher.config.default b/configs/ncsambawatcher.config.default index 22c7d0e..ab252ea 100644 --- a/configs/ncsambawatcher.config.default +++ b/configs/ncsambawatcher.config.default @@ -1 +1,2 @@ -NEXTCLOUD_CONTAINER_NAME=nextcloud \ No newline at end of file +NEXTCLOUD_CONTAINER_NAME=nextcloud +NEXTCLOUD_USERS=username1 username2 username3 \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp index 60e7691..1f29e15 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -106,6 +106,8 @@ void scannerThreadFunc() int main() { + manager.tryAddUsersFromConfig(cfm); + std::thread readingThread(readingThreadFunc); std::thread scannerThread(scannerThreadFunc); diff --git a/src/usermanager.h b/src/usermanager.h index 799c90b..4067ef7 100644 --- a/src/usermanager.h +++ b/src/usermanager.h @@ -7,6 +7,7 @@ #include #include #include +#include #include "definitions.h" #include "configfilemanager.h" @@ -25,28 +26,30 @@ public: addUser(splitString(line, '|').at(USER_LOG_LOCATION)); } - void addUser(std::string &user) + void addUser(const std::string &user) { std::lock_guard lock(mtx); if (users.count(user) == 0) { users[user] = false; + std::cout << "User added the list: " << user << std::endl; } } - void removeUser(std::string &user) + void removeUser(const std::string &user) { std::lock_guard lock(mtx); users.erase(user); + std::cout << "User removed the list: " << user << std::endl; } - bool isContains(std::string &user) + bool isContains(const std::string &user) { std::lock_guard lock(mtx); return users.count(user) == 1; } - void setUserFlagged(std::string &user) + void setUserFlagged(const std::string &user) { std::lock_guard lock(mtx); if (users.count(user) == 1) @@ -55,7 +58,7 @@ public: } } - void setUserUnflagged(std::string &user) + void setUserUnflagged(const std::string &user) { std::lock_guard lock(mtx); if (users.count(user) == 1) @@ -116,6 +119,23 @@ public: return false; } + + void tryAddUsersFromConfig(configfilemanager &cfm) + { + try + { + std::vector alluser = splitString(cfm.at("NEXTCLOUD_USERS"), ' '); + + for (const std::string& user : alluser) + { + addUser(user); + } + } + catch (std::exception e) + { + std::cerr << "No user added from configuration file" << std::endl; + } + } }; #endif // _USERMAN_H \ No newline at end of file From 4dc9ea3acb25ed1522c7bafdcd5ed6212f8db249 Mon Sep 17 00:00:00 2001 From: bartfaik04 Date: Sat, 7 Jun 2025 14:41:31 +0200 Subject: [PATCH 26/29] Import groupfolders from configfile --- configs/ncsambawatcher.config.default | 3 ++- src/main.cpp | 1 + src/usermanager.h | 17 +++++++++++++++++ 3 files changed, 20 insertions(+), 1 deletion(-) diff --git a/configs/ncsambawatcher.config.default b/configs/ncsambawatcher.config.default index ab252ea..d3ef548 100644 --- a/configs/ncsambawatcher.config.default +++ b/configs/ncsambawatcher.config.default @@ -1,2 +1,3 @@ NEXTCLOUD_CONTAINER_NAME=nextcloud -NEXTCLOUD_USERS=username1 username2 username3 \ No newline at end of file +NEXTCLOUD_USERS=username1 username2 username3 +NEXTCLOUD_GROUPFOLDER_IDS=1 2 3 4 \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp index 1f29e15..4918ec7 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -107,6 +107,7 @@ void scannerThreadFunc() int main() { manager.tryAddUsersFromConfig(cfm); + manager.tryAddGroupIDsFromConfig(cfm); std::thread readingThread(readingThreadFunc); std::thread scannerThread(scannerThreadFunc); diff --git a/src/usermanager.h b/src/usermanager.h index 4067ef7..adbccb9 100644 --- a/src/usermanager.h +++ b/src/usermanager.h @@ -136,6 +136,23 @@ public: std::cerr << "No user added from configuration file" << std::endl; } } + + void tryAddGroupIDsFromConfig(configfilemanager &cfm) + { + try + { + std::vector allids = splitString(cfm.at("NEXTCLOUD_GROUPFOLDER_IDS"), ' '); + + for (const std::string& id : allids) + { + addUser("__groupfolder/" + id); + } + } + catch (std::exception e) + { + std::cerr << "No groupfolder added from configuration file" << std::endl; + } + } }; #endif // _USERMAN_H \ No newline at end of file From 1fef08bc550b3cb1465bc34d797a52e19bd7891f Mon Sep 17 00:00:00 2001 From: bartfaik04 Date: Sat, 7 Jun 2025 15:04:56 +0200 Subject: [PATCH 27/29] Check necessary configs --- src/configfilemanager.h | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/configfilemanager.h b/src/configfilemanager.h index 4487ef2..9fbe44f 100644 --- a/src/configfilemanager.h +++ b/src/configfilemanager.h @@ -43,6 +43,12 @@ public: configs.insert(std::make_pair(splited.at(0), splited.at(1))); } + if (configs.count("NEXTCLOUD_CONTAINER_NAME") == 0) + { + std::cerr << "The container's name not added" << std::endl; + exit(EXIT_FAILURE); + } + std::cout << "Config file loaded successfuly" << std::endl; } From 1b44dd10db560ec5eaf163bc131697bf2308a83b Mon Sep 17 00:00:00 2001 From: bartfaik04 Date: Sat, 7 Jun 2025 15:05:05 +0200 Subject: [PATCH 28/29] Update readme --- README.md | 58 +++++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 56 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 45c2174..66e2bb5 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,57 @@ -# nextcloud-samba-sync +# Nextcloud-Samba Sync -A Nextcloud-Samba File scanner system \ No newline at end of file +## Pre installation + +- Install `samba` and `vfs-modules` +``` +sudo apt update +sudo apt install samba samba-vfs-modules +``` +- Create shares. The example is in `configs` folder + +## Installation + +1. Download a `ncsambawatcher.zip` file from a release above 2.0 +2. Extract somewhere on your server (I recommend your user folder) +3. Make sure the samba configuration file is correct (See `configs/smb.24.04.conf`) +4. Configurate the `ncsambawatcher.config` file + + + + + + + + + + + + + + + + + + + + + + + + + +
NameRequiredDescription
NEXTCLOUD_CONTAINER_NAMEYesThe nextcloud's docker container name
NEXTCLOUD_USERSNoUsernames separated with spaces
NEXTCLOUD_GROUPFOLDER_IDSNoGroupfolder ids separated with spaces (only the number)
+ +Example 1: +``` +NEXTCLOUD_CONTAINER_NAME=nextcloud +``` +Example 2: (See `configs/ncsambawatcher.config.default`) + +5. Run the `init.sh` script +6. You're done :) + +## Notes + +- You don't need to add users and groupfolders to the configfile because the program add automaticly from the logfile +- For a user (not groupfolder): The username and the samba share name **MUST BE THE SAME** From f1b94cbfd81c8f2face54752dd44c85f58eefe86 Mon Sep 17 00:00:00 2001 From: bartfaik04 Date: Sat, 7 Jun 2025 15:29:50 +0200 Subject: [PATCH 29/29] Minor fixes --- src/main.cpp | 4 +++- src/usermanager.h | 16 ++++++++-------- 2 files changed, 11 insertions(+), 9 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index 4918ec7..6547cf5 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -5,6 +5,7 @@ #include #include #include +#include #include #include #include @@ -68,7 +69,7 @@ void scannerThreadFunc() cv.wait(lock, [] { return manager.isAnybodyFlagged(); }); - std::set scanUsers = manager.getFlaggedUsers(); + std::unordered_set scanUsers = manager.getFlaggedUsers(); manager.unflagAllUsers(); lock.unlock(); @@ -85,6 +86,7 @@ void scannerThreadFunc() else if (child == 0) // child { std::string cmd = userManager::getScanCommandFromUser(user, cfm); + std::cout << "Run command: " << cmd << std::endl; execl("/bin/sh", "sh", "-c", cmd.c_str(), static_cast(nullptr)); std::cerr << "Scan failed" << std::endl; _exit(EXIT_FAILURE); diff --git a/src/usermanager.h b/src/usermanager.h index adbccb9..7fda463 100644 --- a/src/usermanager.h +++ b/src/usermanager.h @@ -4,7 +4,7 @@ #include #include #include -#include +#include #include #include #include @@ -76,9 +76,9 @@ public: } } - std::set getUsers() + std::unordered_set getUsers() { - std::set ret; + std::unordered_set ret; std::lock_guard lock(mtx); @@ -90,9 +90,9 @@ public: return ret; } - std::set getFlaggedUsers() + std::unordered_set getFlaggedUsers() { - std::set ret; + std::unordered_set ret; std::lock_guard lock(mtx); @@ -131,7 +131,7 @@ public: addUser(user); } } - catch (std::exception e) + catch (const std::exception &e) { std::cerr << "No user added from configuration file" << std::endl; } @@ -145,10 +145,10 @@ public: for (const std::string& id : allids) { - addUser("__groupfolder/" + id); + addUser("__groupfolders/" + id); } } - catch (std::exception e) + catch (const std::exception &e) { std::cerr << "No groupfolder added from configuration file" << std::endl; }