Fix remaining naming and style inconsistencies

- Add const to RecordObjectCompare parameters
- Add static to crc32_table and table_computed in hash.c
- Rename NewRecordObject to RecordObjectNew
- Replace magic number 8192 with BUFFER_SIZE in utility.c
- Unify comment style to /* */ across all files
- Standardize _POSIX_C_SOURCE to 200809L
- Remove unreachable dead code in diff.c
This commit is contained in:
Fabio Scotto di Santolo
2026-08-26 00:03:13 +02:00
parent a7f13e408b
commit 464bb58c8c
9 changed files with 23 additions and 27 deletions

View File

@@ -23,11 +23,11 @@ const char *RecordObjectFieldName(enum Fields f);
struct RecordObject; struct RecordObject;
// Methods /* Methods */
struct RecordObject *NewRecordObject(); struct RecordObject *RecordObjectNew();
int RecordObjectUnpack(struct RecordObject *, char *); int RecordObjectUnpack(struct RecordObject *, char *);
int RecordObjectWrite(struct RecordObject *self, const char *path, FILE *out); int RecordObjectWrite(struct RecordObject *self, const char *path, FILE *out);
int RecordObjectCompare(struct RecordObject *, struct RecordObject *); int RecordObjectCompare(const struct RecordObject *, const struct RecordObject *);
char RecordObjectFileType(const struct RecordObject *); char RecordObjectFileType(const struct RecordObject *);
unsigned int RecordObjectPermissions(const struct RecordObject *); unsigned int RecordObjectPermissions(const struct RecordObject *);
uintmax_t RecordObjectUid(const struct RecordObject *); uintmax_t RecordObjectUid(const struct RecordObject *);

View File

@@ -1,4 +1,4 @@
#define _POSIX_C_SOURCE 1 #define _POSIX_C_SOURCE 200809L
#define _DEFAULT_SOURCE #define _DEFAULT_SOURCE
#define _XOPEN_SOURCE 700 #define _XOPEN_SOURCE 700
#include "create.h" #include "create.h"
@@ -14,14 +14,14 @@
#include <unistd.h> #include <unistd.h>
static int write_record(const char *path, void *context) { static int write_record(const char *path, void *context) {
// skip snapshot files /* skip snapshot files */
const char *base = strrchr(path, '/'); const char *base = strrchr(path, '/');
base = base ? base + 1 : path; base = base ? base + 1 : path;
size_t n = strlen(base); size_t n = strlen(base);
if (n >= 6 && strcasecmp(base + n - 6, ".fsnap") == 0) if (n >= 6 && strcasecmp(base + n - 6, ".fsnap") == 0)
return 0; return 0;
struct RecordObject *r = NewRecordObject(); struct RecordObject *r = RecordObjectNew();
if (r == NULL) if (r == NULL)
return -1; return -1;
int ret = RecordObjectWrite(r, path, (FILE *)context); int ret = RecordObjectWrite(r, path, (FILE *)context);
@@ -69,7 +69,7 @@ int create(const char *directory, const char *output_file) {
goto out; goto out;
} }
// FIXME: It doesn't atomic copy, using rename /* FIXME: It doesn't atomic copy, using rename */
if (copy(template, output_file) == -1) { if (copy(template, output_file) == -1) {
rc = -1; rc = -1;
goto out; goto out;

View File

@@ -31,7 +31,7 @@ static int load_in_memory(const char *file, struct RecordObject ***records,
int i = 0; int i = 0;
size_t size = 0; size_t size = 0;
while (getline(&buf, &size, stream) != -1 && i < *count) { while (getline(&buf, &size, stream) != -1 && i < *count) {
struct RecordObject *objp = NewRecordObject(); struct RecordObject *objp = RecordObjectNew();
if (objp == NULL) { if (objp == NULL) {
errno = ENOMEM; errno = ENOMEM;
goto failure; goto failure;
@@ -92,18 +92,12 @@ int diff(const char *file1, const char *file2) {
int ret1 = load_in_memory(file1, &records1, &len1); int ret1 = load_in_memory(file1, &records1, &len1);
if (ret1 == -1) { if (ret1 == -1) {
goto failure; goto failure;
} else if (ret1 > 0) {
exit_failure = ret1;
goto failure;
} }
int len2 = 0; int len2 = 0;
int ret2 = load_in_memory(file2, &records2, &len2); int ret2 = load_in_memory(file2, &records2, &len2);
if (ret2 == -1) { if (ret2 == -1) {
goto failure; goto failure;
} else if (ret2 > 0) {
exit_failure = ret2;
goto failure;
} }
qsort(records1, (size_t)len1, sizeof(records1[0]), order_by_path); qsort(records1, (size_t)len1, sizeof(records1[0]), order_by_path);

View File

@@ -3,8 +3,8 @@
#define CRC32_POLY 0xEDB88320 #define CRC32_POLY 0xEDB88320
#define BUFFER_SIZE 4096 #define BUFFER_SIZE 4096
uint32_t crc32_table[256]; static uint32_t crc32_table[256];
int table_computed = 0; static int table_computed = 0;
static void init_crc32_table() { static void init_crc32_table() {
for (uint32_t i = 0; i < 256; i++) { for (uint32_t i = 0; i < 256; i++) {

View File

@@ -33,7 +33,7 @@ int list(const char *snapshot) {
goto out; goto out;
} }
while (getline(&buf, &size, snapshot_file) != -1 && i < arr_size) { while (getline(&buf, &size, snapshot_file) != -1 && i < arr_size) {
struct RecordObject *objp = NewRecordObject(); struct RecordObject *objp = RecordObjectNew();
if (objp == NULL) { if (objp == NULL) {
rc = -1; rc = -1;
goto out; goto out;

View File

@@ -10,7 +10,7 @@ int main(int argc, char *argv[]) {
return EXIT_FAILURE; return EXIT_FAILURE;
} }
// I check the argument command exists /* Check that the argument command exists */
errno = 0; errno = 0;
const char *cmd = argv[1]; const char *cmd = argv[1];
if (strcmp(cmd, "scan") == 0) { if (strcmp(cmd, "scan") == 0) {

View File

@@ -1,4 +1,4 @@
#define _POSIX_C_SOURCE 1 #define _POSIX_C_SOURCE 200809L
#define _DEFAULT_SOURCE #define _DEFAULT_SOURCE
#define _XOPEN_SOURCE 700 #define _XOPEN_SOURCE 700
#include "hash.h" #include "hash.h"
@@ -24,7 +24,7 @@ const char *RecordObjectFieldName(enum Fields f) {
return (f <= NONE) ? field_names[f] : "unknown"; return (f <= NONE) ? field_names[f] : "unknown";
} }
//====================== static functions ===================================== /*======================= static functions ===================================*/
static int split_record(char *line, char *fields[NFIELDS], char delim) { static int split_record(char *line, char *fields[NFIELDS], char delim) {
char *p = line; char *p = line;
@@ -232,7 +232,7 @@ static char *sanitize_path(const char *path) {
return s; return s;
} }
//========================= RecordObject ================================= /*========================== RecordObject ====================================*/
struct RecordObject { struct RecordObject {
char file_type; char file_type;
@@ -246,7 +246,7 @@ struct RecordObject {
uintmax_t fingerprint; uintmax_t fingerprint;
}; };
struct RecordObject *NewRecordObject() { struct RecordObject *RecordObjectNew() {
struct RecordObject *ret = calloc(1, sizeof(*ret)); struct RecordObject *ret = calloc(1, sizeof(*ret));
if (ret == NULL) if (ret == NULL)
return NULL; return NULL;
@@ -329,7 +329,7 @@ int RecordObjectUnpack(struct RecordObject *self, char *s) {
return 0; return 0;
} }
int RecordObjectCompare(struct RecordObject *self, struct RecordObject *other) { int RecordObjectCompare(const struct RecordObject *self, const struct RecordObject *other) {
field = NONE; field = NONE;
if (self == NULL) if (self == NULL)
@@ -448,7 +448,7 @@ int RecordObjectRelease(struct RecordObject *self) {
return 0; return 0;
} }
// Accessors /* Accessors */
char RecordObjectFileType(const struct RecordObject *self) { char RecordObjectFileType(const struct RecordObject *self) {
return self->file_type; return self->file_type;
} }

View File

@@ -6,6 +6,8 @@
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#define BUFFER_SIZE 8192
char *strconcat(const char *src, int n, ...) { char *strconcat(const char *src, int n, ...) {
va_list ap; va_list ap;
@@ -57,7 +59,7 @@ int copy(const char *src, const char *dst) {
return -1; return -1;
} }
char buffer[8192]; // Buffer di 8KB char buffer[BUFFER_SIZE];
size_t bytes; size_t bytes;
while ((bytes = fread(buffer, 1, sizeof(buffer), file1)) > 0) { while ((bytes = fread(buffer, 1, sizeof(buffer), file1)) > 0) {

View File

@@ -63,8 +63,8 @@ int walk(const char *directory, WalkOp op, void *context) {
#ifdef DT_UNKNOWN #ifdef DT_UNKNOWN
switch (direntp->d_type) { switch (direntp->d_type) {
case DT_UNKNOWN: case DT_UNKNOWN:
// operating system or filesystem do not support dirent->d_type. /* operating system or filesystem do not support dirent->d_type.
// In this case use alternative algorithm for walk directories. In this case use alternative algorithm for walk directories. */
if (handle_unknown(path, op, context) == -1) if (handle_unknown(path, op, context) == -1)
goto out; goto out;
break; break;