Skip to content

Commit 1afc16e

Browse files
committed
Review comments
1 parent 913e7a8 commit 1afc16e

8 files changed

Lines changed: 44 additions & 17 deletions

File tree

include/core/path_utils.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
#define LIGHTNVR_PATH_UTILS_H
33

44
#include <stddef.h>
5-
#include <fcntl.h>
5+
#include <sys/types.h>
66

77
/**
88
* Sanitize a stream name for use as a path or object name.
@@ -16,9 +16,10 @@ void sanitize_stream_name(const char *input, char *output, size_t output_size);
1616

1717
/**
1818
* Ensure the specified directory exists, creating it if necessary. Does not recur.
19+
* Sets errno on failure.
1920
*
2021
* @param path Directory path to create
21-
* @return 0 on success, -1 on error
22+
* @return 0 on success, negative errno on error
2223
*/
2324
int ensure_dir(const char *path);
2425

src/core/daemon.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,7 @@ static void daemon_signal_handler(int sig) {
198198
int write_pid_file(const char *pid_file) {
199199
// Make sure the directory exists
200200
if (ensure_path(pid_file) != 0) {
201+
log_error("Failed to create PID file directory %s: %s", pid_file, strerror(errno));
201202
return -1;
202203
}
203204

src/core/path_utils.c

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,12 @@
11
#include <ctype.h>
22
#include <dirent.h>
33
#include <errno.h>
4+
#include <fcntl.h>
45
#include <libgen.h>
6+
#include <limits.h>
57
#include <stdbool.h>
68
#include <stdio.h>
7-
#include <stdlib.h>
8-
#include <string.h>
9-
#include <strings.h>
109
#include <sys/stat.h>
11-
#include <sys/types.h>
1210
#include <unistd.h>
1311

1412
#include "core/path_utils.h"
@@ -38,14 +36,16 @@ int ensure_dir(const char *path) {
3836
if (!S_ISDIR(st.st_mode)) {
3937
// Path exists but is not a directory
4038
log_error("Regular file in the way of directory at %s", path);
41-
return -1;
39+
// Also set errno
40+
errno = ENOTDIR;
41+
return -ENOTDIR;
4242
}
43-
// If path exists and is directory, fall through to update p
43+
// If path exists and is directory, we're done
4444
} else {
4545
// Create this directory level
4646
if (mkdir(path, 0755) != 0 && errno != EEXIST) {
4747
log_error("Failed to create directory %s: %s", path, strerror(errno));
48-
return -1;
48+
return -errno;
4949
}
5050
}
5151

@@ -56,9 +56,9 @@ int ensure_dir(const char *path) {
5656
* Create a directory recursively (like mkdir -p), internal implementation.
5757
*
5858
* Will modify path in-place, but restores it before returning. Does not perform
59-
* input validation.
59+
* input validation. Returns the negative error code if one is encountered.
6060
*/
61-
int _mkdir_recursive(char *path) {
61+
static int _mkdir_recursive(char *path) {
6262
// Iterate through path components and create each directory
6363
char *p = path;
6464

@@ -126,7 +126,7 @@ int mkdir_recursive(const char *path) {
126126
* Set permissions on a file or directory (like chmod). Sets fd_out if the
127127
* path is a directory for use in recursive chmod.
128128
*/
129-
int _chmod_path(const char *path, mode_t mode, int *fd_out) {
129+
static int _chmod_path(const char *path, mode_t mode, int *fd_out) {
130130
// Open as a directory first (O_NOFOLLOW prevents following symlinks, eliminating
131131
// the TOCTOU race between the old lstat() check and chmod() on the same path).
132132
int fd = open(path, O_RDONLY | O_DIRECTORY | O_NOFOLLOW);
@@ -223,6 +223,13 @@ int chmod_recursive(const char *path, mode_t mode) {
223223
int result = 0;
224224

225225
size_t offset = snprintf(full_path, sizeof(full_path), "%s/", path);
226+
if (offset >= PATH_MAX) {
227+
// If the path is already max length, we won't be able to append anything to
228+
// it. In this case emit an error and return.
229+
log_error("Path too long to recur for chmod");
230+
close(fd);
231+
return -1;
232+
}
226233
char *path_ptr = full_path + offset;
227234

228235
while ((entry = readdir(dir)) != NULL) {

src/video/hls_writer.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -414,7 +414,7 @@ static int ensure_output_directory(hls_writer_t *writer) {
414414

415415
// Create directory if necessary
416416
if (mkdir_recursive(safe_dir_path)) {
417-
log_error("Failed to create HLS output directory");
417+
log_error("Failed to create HLS output directory %s: %s", safe_dir_path, strerror(errno));
418418
return -1;
419419
}
420420

src/video/packet_buffer.c

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -265,7 +265,13 @@ packet_buffer_t* create_packet_buffer(const char *stream_name, int buffer_second
265265
if (config) {
266266
snprintf(buffer->disk_buffer_path, sizeof(buffer->disk_buffer_path),
267267
"%s/.packet_buffer_%s", config->storage_path, stream_name);
268-
ensure_dir(buffer->disk_buffer_path);
268+
if (ensure_dir(buffer->disk_buffer_path)) {
269+
log_error("Failed to create disk buffer directory %s: %s", buffer->disk_buffer_path, strerror(errno));
270+
pthread_mutex_destroy(&buffer->mutex);
271+
buffer->mutex_initialized = false;
272+
pthread_mutex_unlock(&buffer_pool.pool_mutex);
273+
return NULL;
274+
}
269275
}
270276
}
271277

src/video/unified_detection_thread.c

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -507,7 +507,12 @@ int start_unified_detection_thread(const char *stream_name, const char *model_pa
507507

508508
snprintf(ctx->output_dir, sizeof(ctx->output_dir), "%s/%s",
509509
global_cfg->storage_path, stream_path);
510-
ensure_dir(ctx->output_dir);
510+
if (ensure_dir(ctx->output_dir)) {
511+
log_error("Failed to create output directory %s: %s", ctx->output_dir, strerror(errno));
512+
free(ctx);
513+
pthread_mutex_unlock(&contexts_mutex);
514+
return -1;
515+
}
511516
}
512517

513518
// If using built-in motion detection, enable the motion stream now so that

src/web/api_handlers_system.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1442,7 +1442,11 @@ void handle_post_system_backup(const http_request_t *req, http_response_t *res)
14421442
snprintf(backup_path, sizeof(backup_path), "%s/backups", g_config.web_root);
14431443

14441444
// Create backups directory if it doesn't exist
1445-
ensure_dir(backup_path);
1445+
if (ensure_dir(backup_path)) {
1446+
log_error("Failed to create backup directory %s: %s", backup_path, strerror(errno));
1447+
http_response_set_json_error(res, 500, "Failed to create backup directory");
1448+
return;
1449+
}
14461450

14471451
// Append filename to path
14481452
snprintf(backup_path, sizeof(backup_path), "%s/backups/%s", g_config.web_root, backup_filename);

src/web/api_handlers_timeline.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -587,7 +587,10 @@ int create_timeline_manifest(const timeline_segment_t *segments, int segment_cou
587587
snprintf(temp_dir, sizeof(temp_dir), "%s/timeline_manifests", g_config.storage_path);
588588

589589
// Create directory if it doesn't exist
590-
ensure_dir(temp_dir);
590+
if (ensure_dir(temp_dir)) {
591+
log_error("Could not create directory for timeline manifest %s: %s", temp_dir, strerror(errno));
592+
return -1;
593+
}
591594

592595
// Generate a unique manifest filename
593596
char manifest_filename[MAX_PATH_LENGTH];

0 commit comments

Comments
 (0)