Skip to content

Commit

Permalink
新增文件权限比较功能,添加 INDI 服务器接口,创建配置模块的 CMake 和 xmake 配置
Browse files Browse the repository at this point in the history
  • Loading branch information
AstroAir committed Nov 10, 2024
1 parent 4632507 commit 7fac5c6
Show file tree
Hide file tree
Showing 15 changed files with 2,512 additions and 611 deletions.
1 change: 1 addition & 0 deletions data/name.json

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions data/ngc2019.json

Large diffs are not rendered by default.

114 changes: 114 additions & 0 deletions src/atom/io/file_permission.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
#include "file_permission.hpp"

#ifdef _WIN32
#include <aclapi.h>
#include <windows.h>
#else
#include <sys/stat.h>
#include <unistd.h>
#endif

namespace atom::io {
#ifdef _WIN32
std::string getFilePermissions(const std::string &filePath) {
DWORD dwRtnCode = 0;
PACL pDACL = NULL;
PSECURITY_DESCRIPTOR pSD = NULL;
EXPLICIT_ACCESS *pEA = NULL;
std::string permissions;

dwRtnCode = GetNamedSecurityInfoA(filePath.c_str(), SE_FILE_OBJECT,
DACL_SECURITY_INFORMATION, NULL, NULL,
&pDACL, NULL, &pSD);
if (dwRtnCode != ERROR_SUCCESS) {
std::cerr << "GetNamedSecurityInfoA error: " << dwRtnCode << std::endl;
return "";
}

if (pDACL != NULL) {
for (DWORD i = 0; i < pDACL->AceCount; i++) {
ACE_HEADER *aceHeader;
if (GetAce(pDACL, i, (LPVOID *)&aceHeader)) {
ACCESS_ALLOWED_ACE *ace = (ACCESS_ALLOWED_ACE *)aceHeader;
if (ace->Header.AceType == ACCESS_ALLOWED_ACE_TYPE) {
if (ace->Mask & GENERIC_READ)
permissions += "r";
else
permissions += "-";
if (ace->Mask & GENERIC_WRITE)
permissions += "w";
else
permissions += "-";
if (ace->Mask & GENERIC_EXECUTE)
permissions += "x";
else
permissions += "-";
}
}
}
}

if (pSD != NULL)
LocalFree((HLOCAL)pSD);

return permissions;
}

std::string getSelfPermissions() {
char path[MAX_PATH];
if (GetModuleFileNameA(NULL, path, MAX_PATH) == 0) {
std::cerr << "GetModuleFileNameA error: " << GetLastError()
<< std::endl;
return "";
}
return getFilePermissions(path);
}
#else
auto getFilePermissions(const std::string &filePath) -> std::string {
struct stat fileStat;
if (stat(filePath.c_str(), &fileStat) < 0) {
perror("stat error");
return "";
}

std::string permissions;
permissions += (fileStat.st_mode & S_IRUSR) ? "r" : "-"; // User Read
permissions += (fileStat.st_mode & S_IWUSR) ? "w" : "-"; // User Write
permissions += (fileStat.st_mode & S_IXUSR) ? "x" : "-"; // User Execute
permissions += (fileStat.st_mode & S_IRGRP) ? "r" : "-"; // Group Read
permissions += (fileStat.st_mode & S_IWGRP) ? "w" : "-"; // Group Write
permissions += (fileStat.st_mode & S_IXGRP) ? "x" : "-"; // Group Execute
permissions += (fileStat.st_mode & S_IROTH) ? "r" : "-"; // Others Read
permissions += (fileStat.st_mode & S_IWOTH) ? "w" : "-"; // Others Write
permissions += (fileStat.st_mode & S_IXOTH) ? "x" : "-"; // Others Execute

return permissions;
}

auto getSelfPermissions() -> std::string {
char path[1024];
ssize_t len = readlink("/proc/self/exe", path, sizeof(path) - 1);
if (len < 0) {
perror("readlink error");
return "";
}
path[len] = '\0'; // 确保字符串以'\0'结束
return getFilePermissions(path);
}
#endif

auto compareFileAndSelfPermissions(const std::string &filePath)
-> std::optional<bool> {
std::string filePermissions = getFilePermissions(filePath);
if (filePermissions.empty()) {
return std::nullopt;
}

std::string selfPermissions = getSelfPermissions();
if (selfPermissions.empty()) {
return std::nullopt;
}

return filePermissions == selfPermissions;
}
} // namespace atom::io
12 changes: 12 additions & 0 deletions src/atom/io/file_permission.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#ifndef ATOM_IO_FILE_PERMISSION_HPP
#define ATOM_IO_FILE_PERMISSION_HPP

#include <optional>
#include <string>

namespace atom::io {
auto compareFileAndSelfPermissions(const std::string &filePath)
-> std::optional<bool>;
}

#endif
61 changes: 61 additions & 0 deletions src/debug/config/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
# Minimum required CMake version
cmake_minimum_required(VERSION 3.20)

# Project name and version, using C and C++ languages
project(lithium-config VERSION 1.0.0 LANGUAGES C CXX)

# Project description and information
# This project is the official configuration module for the Lithium server.
# Author: Max Qian
# License: GPL3
# Project Name: Lithium-Config
# Description: The official config module for lithium server
# Author: Max Qian
# License: GPL3

# Project sources
set(PROJECT_SOURCES
configor.cpp
)

# Project headers
set(PROJECT_HEADERS
configor.hpp
)

# Required libraries for the project
set(PROJECT_LIBS
loguru
${CMAKE_THREAD_LIBS_INIT}
)

if (WIN32)
list(APPEND PROJECT_LIBS ws2_32)
endif()

# Create object library
add_library(${PROJECT_NAME}_OBJECT OBJECT ${PROJECT_SOURCES} ${PROJECT_HEADERS})

# Set object library property to be position independent code
set_property(TARGET ${PROJECT_NAME}_OBJECT PROPERTY POSITION_INDEPENDENT_CODE ON)

# Create static library
add_library(${PROJECT_NAME} STATIC $<TARGET_OBJECTS:${PROJECT_NAME}_OBJECT>)

# Set static library properties
set_target_properties(${PROJECT_NAME} PROPERTIES
VERSION ${PROJECT_VERSION} # Version number
SOVERSION 1 # Compatibility version
OUTPUT_NAME ${PROJECT_NAME} # Output name
)

# Include directories so that project headers can be included
target_include_directories(${PROJECT_NAME} PUBLIC ${CMAKE_CURRENT_SOURCE_DIR})

# Link libraries required by the project
target_link_libraries(${PROJECT_NAME} PRIVATE ${PROJECT_LIBS})

# Install target to install the static library to a specified location
install(TARGETS ${PROJECT_NAME}
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
)
Loading

0 comments on commit 7fac5c6

Please sign in to comment.