2019-04-21 00:46:08 +02:00
|
|
|
#include "util.h"
|
|
|
|
|
2019-09-03 15:47:12 +02:00
|
|
|
#include <execinfo.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
2019-04-21 00:46:08 +02:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
2019-09-03 15:47:12 +02:00
|
|
|
#define BT_BUF_SIZE 255
|
|
|
|
|
2019-04-26 08:39:45 +02:00
|
|
|
char *path_append(const char *path, const char *filename)
|
2019-04-21 00:46:08 +02:00
|
|
|
{
|
2019-04-26 08:39:45 +02:00
|
|
|
int needs_separator = 0;
|
2019-04-30 00:40:21 +02:00
|
|
|
if ((path[strnlen(path, MAX_PATH_LEN)-1] != '/') && (filename[0] != '/')) {
|
2019-04-26 08:39:45 +02:00
|
|
|
needs_separator = 1;
|
2019-04-21 12:10:18 +02:00
|
|
|
}
|
2019-04-26 08:39:45 +02:00
|
|
|
|
|
|
|
char *str;
|
|
|
|
size_t ul = strnlen(path, MAX_PATH_LEN);
|
|
|
|
size_t sl = strnlen(filename, MAX_FILENAME_LEN);
|
|
|
|
str = calloc(ul + sl + needs_separator + 1, sizeof(char));
|
|
|
|
if (!str) {
|
|
|
|
fprintf(stderr, "path_append(): calloc failure!\n");
|
2019-09-03 15:47:12 +02:00
|
|
|
exit_failure();
|
2019-04-21 00:46:08 +02:00
|
|
|
}
|
2019-04-26 08:39:45 +02:00
|
|
|
strncpy(str, path, ul);
|
|
|
|
if (needs_separator) {
|
|
|
|
str[ul] = '/';
|
|
|
|
}
|
|
|
|
strncat(str, filename, sl);
|
|
|
|
return str;
|
2019-04-21 00:46:08 +02:00
|
|
|
}
|
2019-04-25 00:58:26 +02:00
|
|
|
|
|
|
|
int64_t round_div(int64_t a, int64_t b)
|
|
|
|
{
|
|
|
|
return (a + (b / 2)) / b;
|
|
|
|
}
|
2019-09-01 22:36:58 +02:00
|
|
|
|
|
|
|
void PTHREAD_MUTEX_UNLOCK(pthread_mutex_t *x)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
i = pthread_mutex_unlock(x);
|
|
|
|
if (i) {
|
2019-09-03 15:59:30 +02:00
|
|
|
fprintf(stderr, "thread %lu: pthread_mutex_unlock() failed, %d, %s\n",
|
|
|
|
pthread_self(), i, strerror(i));
|
2019-09-03 15:47:12 +02:00
|
|
|
exit_failure();
|
2019-09-01 22:36:58 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void PTHREAD_MUTEX_LOCK(pthread_mutex_t *x)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
i = pthread_mutex_lock(x);
|
|
|
|
if (i) {
|
2019-09-03 15:59:30 +02:00
|
|
|
fprintf(stderr, "thread %lu: pthread_mutex_lock() failed, %d, %s\n",
|
|
|
|
pthread_self(), i, strerror(i));
|
2019-09-03 15:47:12 +02:00
|
|
|
exit_failure();
|
2019-09-01 22:36:58 +02:00
|
|
|
}
|
|
|
|
}
|
2019-09-03 15:47:12 +02:00
|
|
|
|
|
|
|
void exit_failure()
|
|
|
|
{
|
|
|
|
int nptrs;
|
|
|
|
void *buffer[BT_BUF_SIZE];
|
|
|
|
|
|
|
|
nptrs = backtrace(buffer, BT_BUF_SIZE);
|
|
|
|
fprintf(stderr, "\nOops! HTTPDirFS crashed! :(\n");
|
|
|
|
fprintf(stderr, "backtrace() returned the following %d addresses:\n",
|
|
|
|
nptrs);
|
|
|
|
backtrace_symbols_fd(buffer, nptrs, STDERR_FILENO);
|
|
|
|
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|