From 5e0e35c4b102223ff38305290e342d818b11c66d Mon Sep 17 00:00:00 2001 From: EgorWeders <157705588+EgorWeders@users.noreply.github.com> Date: Tue, 5 Nov 2024 12:49:56 -0500 Subject: [PATCH 1/5] Fixes + github make action (#15) * Replace magic numbers for path size to default linux PATH_MAX * Add Branch_size define instead of magic number 8 * Fixe mem leaks and parameter constness discard * Create makefile.yml * Fix not used steps * Add github action --- .github/workflows/makefile.yml | 23 +++++++++++++++++++++++ include/git/lgit.h | 8 ++++---- src/git/lgit.c | 29 +++++++++++++---------------- src/laser.c | 14 +++++++------- 4 files changed, 47 insertions(+), 27 deletions(-) create mode 100644 .github/workflows/makefile.yml diff --git a/.github/workflows/makefile.yml b/.github/workflows/makefile.yml new file mode 100644 index 0000000..f54722a --- /dev/null +++ b/.github/workflows/makefile.yml @@ -0,0 +1,23 @@ +name: Makefile CI + +on: + push: + branches: [ "main" ] + pull_request: + branches: [ "main" ] + +jobs: + build: + + runs-on: ubuntu-latest + + steps: + - uses: actions/checkout@v4 + + + - name: Run make + run: make + + - name: Clean + run: make clean + diff --git a/include/git/lgit.h b/include/git/lgit.h index 549caaa..66d980d 100644 --- a/include/git/lgit.h +++ b/include/git/lgit.h @@ -3,18 +3,18 @@ #include "../../include/utils.h" -#include #include +#include typedef struct { char *name; - unsigned tracked:1; + unsigned tracked : 1; - mode_t mode; + mode_t mode; } lgit_entry; -typedef struct +typedef struct { char **lgit_entry; } lgit_entries; diff --git a/src/git/lgit.c b/src/git/lgit.c index 257aaec..752e43d 100644 --- a/src/git/lgit.c +++ b/src/git/lgit.c @@ -8,10 +8,7 @@ char **lgit_parseGitignore(const char *dir, int *count) { - size_t filename_len = - strlen(dir) + strlen("/.gitignore") + - 1; - + size_t filename_len = strlen(dir) + strlen("/.gitignore") + 1; char *filename = malloc(filename_len); snprintf(filename, filename_len, "%s/.gitignore", dir); @@ -69,51 +66,51 @@ char **lgit_parseGitignore(const char *dir, int *count) // lgit_entries *lgit_getGitEntries(laser_opts opts) // { // lgit_entries *entries = malloc(sizeof(lgit_entries)); -// +// // lgit_parseGit(opts.parentDir); -// +// // return entries; // } -// +// // void lgit_parseGit(char *dir) // { // // TODO: check if git dir exists!! // char *index_file = malloc(strlen(dir) + strlen("/.git/index") + 1); -// +// // snprintf(index_file, strlen(dir) + strlen("/.git/index") + 1, // "%s/.git/index", dir); -// +// // size_t idxfsize; // char *idx_bincontent = lgit_utils_readbin(index_file, &idxfsize); // char *idx_file_as_hex = lgit_utils_binToHex(idx_bincontent, idxfsize); -// +// // // https://git-scm.com/docs/index-format // // HEADER // // first 4 bytes DIRC // char HEADER_SIGNATURE[4] = {'D', 'I', 'R', 'C'}; -// +// // if (strcmp(lgit_utils_getChars(idx_bincontent, 0, 4 * 8), // HEADER_SIGNATURE) != 0) // { // fprintf(stderr, "lsr: %s: file is not formatted right!!\n", index_file); // return; // } -// +// // // next 4 bytes: version // // 4 bytes = 8 hex nums // uint32_t version = // lgit_utils_hexToUint32(lgit_utils_getChars(idx_file_as_hex, 8, 8)); -// +// // // then 32 bit num for index entries // // 32 bit 4 byte // uint32_t entries_num = // lgit_utils_hexToUint32(lgit_utils_getChars(idx_file_as_hex, 16, 8)); -// +// // printf("version: %u, entries_count: %u\n", version, entries_num); // } -// +// // void lgit_entries_free(lgit_entries *lgit) // { // free(lgit); // } -// +// diff --git a/src/laser.c b/src/laser.c index f3aca6b..9487269 100644 --- a/src/laser.c +++ b/src/laser.c @@ -2,7 +2,7 @@ #include "filetypes/checktypes.h" #include "git/lgit.h" #include "utils.h" - +#define BRANCH_SIZE 8 char *strip_parent_dir(const char *full_path, const char *parent_dir) { size_t parent_len = strlen(parent_dir); @@ -22,8 +22,8 @@ int laser_cmp_dirent(const void *a, const void *b, const void *arg) struct dirent *dirent_a = *(struct dirent **)a; struct dirent *dirent_b = *(struct dirent **)b; - char path_a[1024]; - char path_b[1024]; + char path_a[PATH_MAX]; + char path_b[PATH_MAX]; snprintf(path_a, sizeof(path_a), "%s/%s", dir_path, dirent_a->d_name); snprintf(path_b, sizeof(path_b), "%s/%s", dir_path, dirent_b->d_name); @@ -48,7 +48,7 @@ int laser_cmp_dirent(const void *a, const void *b, const void *arg) void laser_print_entry(const char *name, const char *color, char *indent, int depth, int is_last) { - char branch[8] = ""; + char branch[BRANCH_SIZE] = ""; if (depth > 0) strcpy(branch, is_last ? "└─ " : "├─ "); @@ -70,7 +70,7 @@ void laser_process_entries(laser_opts opts, int depth, char *indent, struct dirent **entries = malloc(sizeof(struct dirent *)); int entry_count = 0; - char full_path[1024]; + char full_path[PATH_MAX]; while ((entry = readdir(dir)) != NULL) { snprintf(full_path, sizeof(full_path), "%s/%s", opts.dir, @@ -138,13 +138,13 @@ void laser_process_entries(laser_opts opts, int depth, char *indent, { if (S_ISLNK(file_stat.st_mode) && opts.show_symlinks) { - char symlink_target[1024]; + char symlink_target[PATH_MAX]; int len = readlink(full_path, symlink_target, sizeof(symlink_target) - 1); if (len != -1) { symlink_target[len] = '\0'; - char res_string[2048]; + char res_string[PATH_MAX*2+4];//4 is " -> " snprintf(res_string, sizeof(res_string), "%s -> %s", entries[i]->d_name, symlink_target); laser_print_entry(res_string, SYMLINK_COLOR, indent, depth, From be5d2ab21a7b485c1b758ec1df94ea8d2b5c1370 Mon Sep 17 00:00:00 2001 From: EgorWeders <157705588+EgorWeders@users.noreply.github.com> Date: Tue, 5 Nov 2024 12:56:33 -0500 Subject: [PATCH 2/5] add code style action and error fix (#16) * Replace magic numbers for path size to default linux PATH_MAX * Adds Branch_size define instead of magic number 8 * Fixes mem leaks and parameter constness discard * Create makefile.yml * Fixes not used steps * Adds format and clean target calls * Removes clamg-format usage * Create code-style.yml --- .github/workflows/code-style.yml | 15 +++++++++++++++ 1 file changed, 15 insertions(+) create mode 100644 .github/workflows/code-style.yml diff --git a/.github/workflows/code-style.yml b/.github/workflows/code-style.yml new file mode 100644 index 0000000..bc76511 --- /dev/null +++ b/.github/workflows/code-style.yml @@ -0,0 +1,15 @@ +name: clang-format Check +on: [push, pull_request] +jobs: + formatting-check: + name: Formatting Check + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v3 + - name: Run clang-format style check for C/C++/Protobuf programs. + uses: jidicula/clang-format-action@v4.11.0 + with: + clang-format-style-path : './.clang-format' + clang-format-version: '13' + check-path: '.' + fallback-style: 'LLVM' # optional From fa657ca0d238ac3e48b101dbc15728d33838b294 Mon Sep 17 00:00:00 2001 From: jmattaa Date: Tue, 5 Nov 2024 19:03:52 +0100 Subject: [PATCH 3/5] fix: errors and make install before the program used to install a debug variation if it was built and then make install was run. Now I've added clean as a dependency for the release target so it builds a clean build Signed-off-by: jmattaa --- Makefile | 2 ++ include/laser.h | 1 + src/laser.c | 6 ++++-- 3 files changed, 7 insertions(+), 2 deletions(-) diff --git a/Makefile b/Makefile index a0ac884..7834bf9 100644 --- a/Makefile +++ b/Makefile @@ -30,6 +30,7 @@ debug: $(TARGET) release: CFLAGS += $(CFLAGS_RELEASE) release: LDFLAGS += $(LDFLAGS_RELEASE) +release: clean release: $(TARGET) $(TARGET): $(OBJS) | $(BIN_DIR) @@ -50,6 +51,7 @@ format: install: release install -m 755 $(TARGET) /usr/local/bin + make clean uninstall: rm -f /usr/local/bin/$(notdir $(TARGET)) diff --git a/include/laser.h b/include/laser.h index 5172e01..3301782 100644 --- a/include/laser.h +++ b/include/laser.h @@ -11,6 +11,7 @@ #include #define MAX_ENTRIES 1024 +#define PATH_MAX 1024 void laser_list_directory(laser_opts opts, int depth); void laser_print_entry(const char *name, const char *color, char *indent, int depth, int is_last); diff --git a/src/laser.c b/src/laser.c index 9487269..2844951 100644 --- a/src/laser.c +++ b/src/laser.c @@ -2,7 +2,9 @@ #include "filetypes/checktypes.h" #include "git/lgit.h" #include "utils.h" -#define BRANCH_SIZE 8 + +#define BRANCH_SIZE 8 + char *strip_parent_dir(const char *full_path, const char *parent_dir) { size_t parent_len = strlen(parent_dir); @@ -144,7 +146,7 @@ void laser_process_entries(laser_opts opts, int depth, char *indent, if (len != -1) { symlink_target[len] = '\0'; - char res_string[PATH_MAX*2+4];//4 is " -> " + char res_string[PATH_MAX * 2 + 4]; // 4 is " -> " snprintf(res_string, sizeof(res_string), "%s -> %s", entries[i]->d_name, symlink_target); laser_print_entry(res_string, SYMLINK_COLOR, indent, depth, From e4dcb575a8e7c5cde02d0aa86055f15e36f7d556 Mon Sep 17 00:00:00 2001 From: jmattaa Date: Tue, 5 Nov 2024 19:24:59 +0100 Subject: [PATCH 4/5] update make format and format checking Signed-off-by: jmattaa --- .github/workflows/check-format.yml | 18 ++++++++++++++++++ .github/workflows/code-style.yml | 15 --------------- Makefile | 2 +- include/git/lgit_utils.h | 2 +- include/laser.h | 7 ++++--- include/utils.h | 7 ++++--- scripts/clang-check-format.sh | 8 ++++++++ src/filetypes/archives.c | 3 +-- src/filetypes/checktype.c | 3 ++- src/filetypes/medias.c | 3 +-- 10 files changed, 40 insertions(+), 28 deletions(-) create mode 100644 .github/workflows/check-format.yml delete mode 100644 .github/workflows/code-style.yml create mode 100755 scripts/clang-check-format.sh diff --git a/.github/workflows/check-format.yml b/.github/workflows/check-format.yml new file mode 100644 index 0000000..3a9ba6a --- /dev/null +++ b/.github/workflows/check-format.yml @@ -0,0 +1,18 @@ +name: clang-format CheckV +on: [push, pull_request] + +jobs: + clang-format: + runs-on: ubuntu-latest + + steps: + - name: Checkout code + uses: actions/checkout@v3 + + - name: Install clang-format + run: sudo apt-get install -y clang-format + + - name: Check code formatting + id: check_formatting + run: ./scripts/clang-check-format.sh + shell: bash diff --git a/.github/workflows/code-style.yml b/.github/workflows/code-style.yml deleted file mode 100644 index bc76511..0000000 --- a/.github/workflows/code-style.yml +++ /dev/null @@ -1,15 +0,0 @@ -name: clang-format Check -on: [push, pull_request] -jobs: - formatting-check: - name: Formatting Check - runs-on: ubuntu-latest - steps: - - uses: actions/checkout@v3 - - name: Run clang-format style check for C/C++/Protobuf programs. - uses: jidicula/clang-format-action@v4.11.0 - with: - clang-format-style-path : './.clang-format' - clang-format-version: '13' - check-path: '.' - fallback-style: 'LLVM' # optional diff --git a/Makefile b/Makefile index 7834bf9..0d7f8f6 100644 --- a/Makefile +++ b/Makefile @@ -47,7 +47,7 @@ clean: rm -rf $(OBJ_DIR) $(BIN_DIR) format: - clang-format -i $(PROJECT_DIR)/*.c $(PROJECT_DIR)/include/*.h + clang-format -i $(SRCS) $(PROJECT_DIR)/include/**.h install: release install -m 755 $(TARGET) /usr/local/bin diff --git a/include/git/lgit_utils.h b/include/git/lgit_utils.h index 09ab719..d3be41b 100644 --- a/include/git/lgit_utils.h +++ b/include/git/lgit_utils.h @@ -1,10 +1,10 @@ #ifndef LASER_GIT_UTILS_H #define LASER_GIT_UTILS_H +#include #include #include #include -#include uint32_t lgit_utils_hexToUint32(const char *hex); uint32_t lgit_utils_binToUint32(const char *bin); diff --git a/include/laser.h b/include/laser.h index 3301782..b4a2a2c 100644 --- a/include/laser.h +++ b/include/laser.h @@ -2,18 +2,19 @@ #define LASER_LASER_H #include "colors.h" -#include "utils.h" #include "sort.h" +#include "utils.h" #include +#include #include #include #include -#include #define MAX_ENTRIES 1024 #define PATH_MAX 1024 void laser_list_directory(laser_opts opts, int depth); -void laser_print_entry(const char *name, const char *color, char *indent, int depth, int is_last); +void laser_print_entry(const char *name, const char *color, char *indent, + int depth, int is_last); #endif diff --git a/include/utils.h b/include/utils.h index 9b61d7b..64cbaf5 100644 --- a/include/utils.h +++ b/include/utils.h @@ -4,10 +4,10 @@ #include #include #include -#include -#include #include #include +#include +#include typedef struct laser_opts { @@ -24,7 +24,8 @@ typedef struct laser_opts laser_opts laser_utils_parsecmd(int argc, char **argv); //void laser_utils_format_date(time_t time, char *buffer, size_t buffer_size); -char **laser_utils_grow_strarray(char **array, size_t *alloc_size, size_t count); +char **laser_utils_grow_strarray(char **array, size_t *alloc_size, + size_t count); int laser_string_in_sorted_array(char *target, char **array, int size); int laser_cmp_string(const void *a, const void *b, const void *arg); void laser_swap(void *a, void *b, size_t size); diff --git a/scripts/clang-check-format.sh b/scripts/clang-check-format.sh new file mode 100755 index 0000000..36145e3 --- /dev/null +++ b/scripts/clang-check-format.sh @@ -0,0 +1,8 @@ + files=$(find . -name '*.c' -o -name '*.h') + for file in $files; do + clang-format -style=file $file | diff $file - >/dev/null + if [ $? -ne 0 ]; then \ + echo "::error file=$file::Code not formatted according to clang-format" + exit 1 + fi + done diff --git a/src/filetypes/archives.c b/src/filetypes/archives.c index a604599..03fd6f3 100644 --- a/src/filetypes/archives.c +++ b/src/filetypes/archives.c @@ -7,5 +7,4 @@ const struct laser_magicnumber laser_archiveformats[] = { {(unsigned char[]){0x42, 0x5A, 0x68}, 3}, // BZIP2 {(unsigned char[]){0xFD, 0x37, 0x7A, 0x58, 0x5A}, 5}, // XZ // ADD MORE ARCHIVES - {NULL, 0} -}; + {NULL, 0}}; diff --git a/src/filetypes/checktype.c b/src/filetypes/checktype.c index 69abbc2..7e43f2a 100644 --- a/src/filetypes/checktype.c +++ b/src/filetypes/checktype.c @@ -32,7 +32,8 @@ int laser_checktype(const char *filename, while (formats[i].magic_size != 0) { if (bytesRead >= formats[i].magic_size && - memcmp(buffer, formats[i].magic, formats[i].magic_size) == 0) + memcmp(buffer, formats[i].magic, formats[i].magic_size) == + 0) ///asdjaklsdjalksdj return 1; i++; } diff --git a/src/filetypes/medias.c b/src/filetypes/medias.c index 9f35551..9fbe195 100644 --- a/src/filetypes/medias.c +++ b/src/filetypes/medias.c @@ -8,5 +8,4 @@ const struct laser_magicnumber laser_mediaformats[] = { {(unsigned char[]){0x66, 0x74, 0x79, 0x70, 0x4D, 0x53, 0x4E, 0x56}, 3}, // MP4 // ADD MORE MEDIA TYPES - {NULL, 0} -}; + {NULL, 0}}; From df62f75918bc464c8f1bd131b625fd7ddd5451fd Mon Sep 17 00:00:00 2001 From: jmattaa Date: Tue, 5 Nov 2024 19:27:23 +0100 Subject: [PATCH 5/5] update clang-format check Signed-off-by: jmattaa --- .github/workflows/check-format.yml | 10 +++++++--- .github/workflows/makefile.yml | 1 - 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/.github/workflows/check-format.yml b/.github/workflows/check-format.yml index 3a9ba6a..f91411a 100644 --- a/.github/workflows/check-format.yml +++ b/.github/workflows/check-format.yml @@ -1,5 +1,9 @@ -name: clang-format CheckV -on: [push, pull_request] +name: clang-format Check +on: + push: + branches: [ "main" ] + pull_request: + branches: [ "main" ] jobs: clang-format: @@ -7,7 +11,7 @@ jobs: steps: - name: Checkout code - uses: actions/checkout@v3 + uses: actions/checkout@v4 - name: Install clang-format run: sudo apt-get install -y clang-format diff --git a/.github/workflows/makefile.yml b/.github/workflows/makefile.yml index f54722a..5348f5c 100644 --- a/.github/workflows/makefile.yml +++ b/.github/workflows/makefile.yml @@ -14,7 +14,6 @@ jobs: steps: - uses: actions/checkout@v4 - - name: Run make run: make