mirror of
https://github.com/fscotto/fsnap.git
synced 2026-11-26 11:19:52 +00:00
Cosmetic cleanups across the tree
No behavioural change; verified byte-identical scan output over /usr/include and /etc and byte-identical snapshots against the previous build, with valgrind clean on every command. - free() accepts NULL, so drop six guards around it in types.c. - RecordObjectRelease returned an int no caller ever read; make it void, which is the idiom for a free-like function. - RecordObjectFieldName bounded its index only from above. The underlying type of an enum is the compiler's choice, so compare as unsigned and catch a negative value too. - copy() opened the destination "w+" but never reads it back; "w" says what it does. - Testing strlen(s) == 0 walks the whole string to answer a question about its first byte. - Two comments contradicted the code: split_record finds eight separators, not six, and strconcat explains itself in terms of a strcat it does not call. - Drop includes nothing uses: <inttypes.h> from list.c and create.c, and <string.h> from list.c and from scan.c, which stopped needing it once the strlen above was gone. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_018KyCHPAxWzqa3vHKdXJkgx
This commit is contained in:
@@ -57,7 +57,7 @@ const char *RecordObjectPath(const struct RecordObject *self);
|
||||
const char *RecordObjectTarget(const struct RecordObject *self);
|
||||
uintmax_t RecordObjectFingerPrint(const struct RecordObject *self);
|
||||
|
||||
/* Free self and all owned resources. Returns 0, or -1 if self is NULL. */
|
||||
int RecordObjectRelease(struct RecordObject *self);
|
||||
/* Free self and all owned resources. Does nothing if self is NULL. */
|
||||
void RecordObjectRelease(struct RecordObject *self);
|
||||
|
||||
#endif
|
||||
|
||||
@@ -3,7 +3,6 @@
|
||||
#include "utility.h"
|
||||
#include "walk.h"
|
||||
#include <errno.h>
|
||||
#include <inttypes.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
@@ -27,11 +26,11 @@ static int write_record(const char *path, void *context) {
|
||||
}
|
||||
|
||||
int create(const char *directory, const char *output_file) {
|
||||
if (directory == NULL || strlen(directory) == 0) {
|
||||
if (directory == NULL || directory[0] == '\0') {
|
||||
errno = EINVAL;
|
||||
return -1;
|
||||
}
|
||||
if (output_file == NULL || strlen(output_file) == 0) {
|
||||
if (output_file == NULL || output_file[0] == '\0') {
|
||||
errno = EINVAL;
|
||||
return -1;
|
||||
}
|
||||
|
||||
@@ -2,10 +2,8 @@
|
||||
#include "types.h"
|
||||
#include "utility.h"
|
||||
#include <errno.h>
|
||||
#include <inttypes.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
int list(const char *snapshot) {
|
||||
FILE *snapshot_file = fopen(snapshot, "r");
|
||||
|
||||
@@ -4,7 +4,6 @@
|
||||
#include <errno.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
static int print_file(const char *file, void *context) {
|
||||
(void)context;
|
||||
@@ -13,7 +12,7 @@ static int print_file(const char *file, void *context) {
|
||||
|
||||
/* Scan recursively directory, printing all entry */
|
||||
int scan(const char *directory) {
|
||||
if (directory == NULL || strlen(directory) == 0) {
|
||||
if (directory == NULL || directory[0] == '\0') {
|
||||
errno = EINVAL;
|
||||
return -1;
|
||||
}
|
||||
|
||||
21
src/types.c
21
src/types.c
@@ -16,7 +16,9 @@ static const char *field_names[] = {
|
||||
"time", "target", "fingerprint", "path", "none"};
|
||||
|
||||
const char *RecordObjectFieldName(enum Fields f) {
|
||||
return (f <= NONE) ? field_names[f] : "unknown";
|
||||
/* The underlying type of an enum is the compiler's choice, so guard the
|
||||
low side too: as unsigned, a negative value wraps past NONE. */
|
||||
return ((unsigned)f <= NONE) ? field_names[f] : "unknown";
|
||||
}
|
||||
|
||||
/*======================= static functions ===================================*/
|
||||
@@ -38,7 +40,7 @@ static int split_record(char *line, char *fields[NFIELDS], char delim) {
|
||||
}
|
||||
|
||||
/* Only the path may contain an escaped delimiter, and it is the last field,
|
||||
so the six separators found above are always the real ones. Validating
|
||||
so the separators found above are always the real ones. Validating
|
||||
what is left is the unescaper's job. */
|
||||
fields[NFIELDS - 1] = p;
|
||||
|
||||
@@ -411,7 +413,6 @@ int RecordObjectWrite(struct RecordObject *self, const char *path, FILE *out) {
|
||||
s = sanitize_path(path);
|
||||
if (s == NULL)
|
||||
goto cleanup;
|
||||
if (self->path != NULL)
|
||||
free(self->path);
|
||||
self->path = s;
|
||||
s = NULL;
|
||||
@@ -448,7 +449,6 @@ int RecordObjectWrite(struct RecordObject *self, const char *path, FILE *out) {
|
||||
}
|
||||
target[n] = '\0';
|
||||
}
|
||||
if (self->target != NULL)
|
||||
free(self->target);
|
||||
self->target = target;
|
||||
target = NULL;
|
||||
@@ -474,28 +474,19 @@ int RecordObjectWrite(struct RecordObject *self, const char *path, FILE *out) {
|
||||
self->path);
|
||||
|
||||
cleanup:
|
||||
if (s != NULL)
|
||||
free(s);
|
||||
if (target != NULL)
|
||||
free(target);
|
||||
if (f != NULL)
|
||||
fclose(f);
|
||||
return rc;
|
||||
}
|
||||
|
||||
int RecordObjectRelease(struct RecordObject *self) {
|
||||
void RecordObjectRelease(struct RecordObject *self) {
|
||||
if (self == NULL)
|
||||
return -1;
|
||||
if (self->path != NULL) {
|
||||
return;
|
||||
free(self->path);
|
||||
self->path = NULL;
|
||||
}
|
||||
if (self->target != NULL) {
|
||||
free(self->target);
|
||||
self->target = NULL;
|
||||
}
|
||||
free(self);
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* Accessors */
|
||||
|
||||
@@ -10,8 +10,8 @@
|
||||
char *strconcat(const char *src, int n, ...) {
|
||||
va_list ap;
|
||||
|
||||
/* First pass: total length. strcat never reallocates, so the buffer has to
|
||||
be sized up front. */
|
||||
/* First pass: total length. The result is one allocation, so it has to be
|
||||
sized before anything is copied into it. */
|
||||
size_t len = strlen(src);
|
||||
va_start(ap, n);
|
||||
for (int i = 0; i < n; i++) {
|
||||
@@ -55,7 +55,7 @@ int copy(const char *src, const char *dst) {
|
||||
if (file1 == NULL)
|
||||
goto cleanup;
|
||||
|
||||
file2 = fopen(dst, "w+");
|
||||
file2 = fopen(dst, "w");
|
||||
if (file2 == NULL)
|
||||
goto cleanup;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user