utils.h (1728B)
1 #ifndef GOUT_UTILS_H_ 2 #define GOUT_UTILS_H_ 3 4 #include <stdbool.h> 5 #include <stdio.h> 6 7 #include <stdint.h> 8 #include <sys/stat.h> 9 10 /* Diff suppression limits. */ 11 typedef struct DiffLimits { 12 uint64_t max_files; 13 uint64_t max_deltas; 14 uint64_t max_delta_lines; 15 } DiffLimits; 16 17 /* File system operations. */ 18 typedef struct FileSystem { 19 int (*mkdir)(const char* path, mode_t mode); 20 int (*mkdirp)(const char* path); 21 FILE* (*fopen)(const char* path, const char* mode); 22 FILE* (*fdopen)(int fd, const char* mode); 23 int (*fclose)(FILE* stream); 24 int (*close)(int fd); 25 int (*mkstemp)(char* template); 26 int (*rename)(const char* oldpath, const char* newpath); 27 int (*chmod)(const char* path, mode_t mode); 28 int (*access)(const char* path, int amode); 29 char* (*realpath)(const char* path, char* resolved_path); 30 } FileSystem; 31 32 /* Concatenates path parts to "p1/p2". Returns a dynamically allocated string. 33 * Exits on failure. */ 34 char* path_concat(const char* p1, const char* p2); 35 36 /* Behaves as calloc but exits on failure. */ 37 void* ecalloc(size_t count, size_t size); 38 39 /* Behaves as strdup but exits on failure. */ 40 char* estrdup(const char* s); 41 42 /* Behaves as strlcpy but exits on failure or truncation. */ 43 size_t estrlcpy(char* dst, const char* src, size_t dsize); 44 45 /* Validates that a path is safe to use. Returns true if safe. */ 46 bool is_safe_repo_path(const char* path); 47 48 /* Returns true if the URL has a safe scheme. */ 49 bool is_safe_url(const char* url); 50 51 /* Returns true if the email address is safe to use in a mailto: link. */ 52 bool is_safe_mailto(const char* email); 53 54 /* Computes a relative path of `../` components for a given directory path. */ 55 char* relpath_from_dir(const char* dir); 56 57 #endif // GOUT_UTILS_H_