104 lines
2.7 KiB
C
104 lines
2.7 KiB
C
/*
|
|
* This file is part of uIRCd. (https://git.redxen.eu/caskd/uIRCd)
|
|
* Copyright (c) 2019, 2020 Alex-David Denes
|
|
*
|
|
* uIRCd is free software: you can redistribute it and/or modify
|
|
* it under the terms of the GNU General Public License as published by
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
* any later version.
|
|
*
|
|
* uIRCd is distributed in the hope that it will be useful,
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
* GNU General Public License for more details.
|
|
*
|
|
* You should have received a copy of the GNU General Public License
|
|
* along with uIRCd. If not, see <https://www.gnu.org/licenses/>.
|
|
*/
|
|
|
|
#include "filesystem.h"
|
|
|
|
int
|
|
mkdir_bottomup(char* path)
|
|
{
|
|
if (path == NULL || *path == '\0') return 0;
|
|
for (char* x = path; x != NULL && *x;) {
|
|
if ((x = strchr(x, '/')) != NULL) {
|
|
char save = *(x + 1);
|
|
*(x + 1) = '\0';
|
|
if (mkdir(path, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH) != 0) {
|
|
if (errno != EEXIST) {
|
|
*(x + 1) = save;
|
|
LOG(LOG_ERROR, "Could not create directory \"%s\".", path);
|
|
return 0;
|
|
}
|
|
} else
|
|
LOG(LOG_DEBUG, "Created directory at: %s", path);
|
|
*(x + 1) = save;
|
|
x++;
|
|
}
|
|
}
|
|
return 1;
|
|
}
|
|
|
|
int
|
|
makeinput(char* path)
|
|
{
|
|
if (path == NULL) return -1;
|
|
if (mkfifo(path, S_IRUSR | S_IWUSR | S_IWGRP) == 0) {
|
|
LOG(LOG_VERBOSE, "Created a FIFO pipe for input at %s.", path);
|
|
} else if (errno != EEXIST) {
|
|
LOG(LOG_WARN, "Couldn't create FIFO at \"%s\" for input. " ERRNOFMT, path, strerror(errno), errno);
|
|
return -1;
|
|
}
|
|
int fd;
|
|
if ((fd = open(path, O_RDONLY | O_NONBLOCK)) == -1) {
|
|
LOG(LOG_WARN, "Couldn't open FIFO pipe \"%s\" for reading. " ERRNOFMT, path, strerror(errno), errno);
|
|
} else
|
|
LOG(LOG_DEBUG, "Opened \"%s\" for reading.", path);
|
|
return fd;
|
|
}
|
|
|
|
bool
|
|
write_log(char* path, char* message)
|
|
{
|
|
FILE* logfile;
|
|
if ((logfile = fopen(path, "a")) != NULL) {
|
|
fprintf(logfile, "%s", message);
|
|
fclose(logfile);
|
|
return 1;
|
|
} else
|
|
LOG(LOG_WARN, "Couldn't open file \"%s\" for appending.", path);
|
|
return 0;
|
|
}
|
|
|
|
bool
|
|
cleanup_path_names(char* name)
|
|
{
|
|
if (name == NULL) return 0;
|
|
if (!strcmp("..", name) || !strcmp(".", name)) *name = '_';
|
|
for (; *name; name++) {
|
|
if (isalpha(*name)) *name = (char) tolower(*name);
|
|
else if (strchr(".+-_#&", *name) == NULL && !isdigit(*name))
|
|
*name = '_';
|
|
}
|
|
return 1;
|
|
}
|
|
|
|
bool
|
|
add_socket_flags(int fd, int flags)
|
|
{
|
|
int cflgs;
|
|
if ((cflgs = fcntl(fd, F_GETFL)) != -1) {
|
|
if (fcntl(fd, F_SETFL, cflgs | flags) == -1) {
|
|
LOG(LOG_WARN, "Couldn't add socket flags. " ERRNOFMT, strerror(errno), errno);
|
|
return 0;
|
|
}
|
|
} else {
|
|
LOG(LOG_WARN, "Failed to get socket flags. " ERRNOFMT, strerror(errno), errno);
|
|
return 0;
|
|
}
|
|
return 1;
|
|
}
|
|
|