18#ifndef USHER_PATH_UTILS_H
19#define USHER_PATH_UTILS_H
49 ssize_t length = readlink(
"/proc/self/exe", executable,
sizeof(executable) - 1);
50 if (length <= 0 || (
size_t)length >=
sizeof(executable) - 1) {
53 executable[length] =
'\0';
54 char *separator = strrchr(executable,
'/');
55 if (separator == NULL) {
59 if (separator == executable) {
64 return chdir(executable);
79 if (path[0] ==
'~' && (path[1] ==
'/' || path[1] ==
'\0')) {
80 const char *home = getenv(
"HOME");
81 if (home != NULL && home[0] !=
'\0') {
82 int length = snprintf(buffer, buffer_size,
"%s%s", home, path + 1);
83 if (length > 0 && (
size_t)length < buffer_size) {
99 if (path == NULL || path[0] ==
'\0') {
104 if (target == NULL) {
110 while (target[length] !=
'\0' && length <
sizeof(temp) - 1) {
111 temp[length] = target[length];
114 if (target[length] !=
'\0') {
119 for (
size_t index = 1; index <= length; index++) {
120 if (temp[index] ==
'/' || temp[index] ==
'\0') {
121 char character = temp[index];
123 if (temp[0] !=
'\0' && mkdir(temp, 0755) != 0 && errno != EEXIST) {
126 temp[index] = character;
131 if (stat(target, &status) != 0 || !S_ISDIR(status.st_mode) || access(target, W_OK) != 0) {
145 if (!filename || !
capacity) { errno = EINVAL;
return -1; }
146 const size_t length = strnlen(filename,
capacity);
147 const char *separator = length <
capacity ? strrchr(filename,
'/') : NULL;
148 const char staging[] =
".pending/";
149 const size_t extra =
sizeof(staging) - 1;
150 if (!separator || !separator[1] || length >=
PATH_MAX - extra || length + extra >=
capacity) {
151 errno = ENAMETOOLONG;
return -1;
154 if (lstat(filename, &status) == 0 || errno != ENOENT) { errno = EEXIST;
return -1; }
155 const size_t parent_length = (size_t)(separator - filename);
157 const int count = snprintf(directory,
sizeof(directory),
"%.*s/.pending", (
int)parent_length, filename);
158 if (
count < 0 || (
size_t)
count >=
sizeof(directory)) { errno = ENAMETOOLONG;
return -1; }
159 if ((mkdir(directory, 0700) != 0 && errno != EEXIST) || lstat(directory, &status) != 0)
return -1;
160 if (!S_ISDIR(status.st_mode) || status.st_uid != geteuid() || (status.st_mode & 077)) { errno = EACCES;
return -1; }
161 for (
size_t index = length + 1; index > parent_length + 1; --index)
162 filename[index - 1 + extra] = filename[index - 1];
163 for (
size_t index = 0; index < extra; ++index) filename[parent_length + 1 + index] = staging[index];
164 if (lstat(filename, &status) == 0 || errno != ENOENT) { errno = EEXIST;
return -1; }
178 if (!source || !destination || !source[0] || !destination[0]) { errno = EINVAL;
return -1; }
180 const char *basename = strrchr(destination,
'/');
181 const size_t length = basename ? (size_t)(basename - destination) : 0;
182 if (length >=
sizeof(parent)) { errno = ENAMETOOLONG;
return -1; }
184 for (
size_t index = 0; index < length; ++index) parent[index] = destination[index];
185 parent[length ? length : 1] =
'\0';
186 if (!length) parent[0] =
'/';
189 parent[0] =
'.'; parent[1] =
'\0'; basename = destination;
191 if (!basename[0]) { errno = EINVAL;
return -1; }
192 int directory =
open(parent, O_RDONLY | O_DIRECTORY | O_NOFOLLOW | O_CLOEXEC);
193 if (directory < 0)
return -1;
194 int input =
open(source, O_RDONLY | O_NOFOLLOW | O_NONBLOCK | O_CLOEXEC);
197 if (input >= 0 && fstat(input, &status) == 0) {
198 if (!S_ISREG(status.st_mode) || status.st_size <= 0 || status.st_nlink != 1) errno = EINVAL;
199 else if (fsync(input) == 0 && linkat(AT_FDCWD, source, directory, basename, 0) == 0) {
201 if (fsync(directory) == 0 && unlink(source) == 0)
result = 0;
204 const int saved_errno = errno;
205 if (input >= 0)
close(input);
218 if (!filename || !
capacity) { errno = EINVAL;
return -1; }
219 const size_t length = strnlen(filename,
capacity);
220 const char *basename = length <
capacity ? strrchr(filename,
'/') : NULL;
221 const char staging[] =
"/.pending";
222 const size_t extra =
sizeof(staging) - 1;
223 if (!basename || (
size_t)(basename - filename) < extra || length >=
PATH_MAX
224 || strncmp(basename - extra, staging, extra)) { errno = EINVAL;
return -1; }
226 const size_t parent_length = (size_t)(basename - filename) - extra;
227 for (
size_t index = 0; index < parent_length; ++index) destination[index] = filename[index];
228 for (
size_t index = parent_length; index <= length - extra; ++index) destination[index] = filename[index + extra];
230 if (
result >= 0)
for (
size_t index = 0; index <= length - extra; ++index) filename[index] = destination[index];
static unsigned capacity[COVERAGE_CAMERAS]
void(* close)(struct usbfs_handle *)
int(* open)(const struct usbfs_device *, struct usbfs_handle *)
static int usher_complete_photo(char *filename, size_t capacity)
Publish a staged photo and return its immutable public path.
static int usher_prepare_photo(char *filename, size_t capacity)
Redirect an intended public photo into its same-filesystem private staging directory.
static int usher_enter_executable_directory(void)
Change to the directory containing the running USHER executable.
static const char * usher_resolve_path(const char *path, char *buffer, size_t buffer_size)
Expand a leading home-directory shorthand without allocating memory.
static int usher_publish_photo(const char *source, const char *destination)
Publish completed immutable bytes atomically without copying or replacing an existing file.
static int usher_ensure_directory(const char *path)
Create missing directory components and verify the final directory is writable.
struct usher_control_result result