2022-09-25 19:35:51 -04:00
|
|
|
#include <fcntl.h>
|
|
|
|
#include <sys/file.h>
|
|
|
|
|
2022-03-14 19:09:25 -04:00
|
|
|
#include <libreborn/util.h>
|
|
|
|
|
|
|
|
// Safe Version Of pipe()
|
|
|
|
void safe_pipe2(int pipefd[2], int flags) {
|
|
|
|
if (pipe2(pipefd, flags) != 0) {
|
|
|
|
ERR("Unable To Create Pipe: %s", strerror(errno));
|
|
|
|
}
|
|
|
|
}
|
2022-05-29 18:44:27 -04:00
|
|
|
|
|
|
|
// Check If Two Percentages Are Different Enough To Be Logged
|
|
|
|
#define SIGNIFICANT_PROGRESS 5
|
|
|
|
int is_progress_difference_significant(int32_t new_val, int32_t old_val) {
|
|
|
|
if (new_val != old_val) {
|
|
|
|
if (new_val == -1 || old_val == -1) {
|
|
|
|
return 1;
|
|
|
|
} else if (new_val == 0 || new_val == 100) {
|
|
|
|
return 1;
|
|
|
|
} else {
|
|
|
|
return new_val - old_val >= SIGNIFICANT_PROGRESS;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
2022-09-25 19:35:51 -04:00
|
|
|
|
|
|
|
// Lock File
|
|
|
|
int lock_file(const char *file) {
|
2022-09-25 20:56:45 -04:00
|
|
|
int fd = open(file, O_WRONLY | O_CREAT, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH);
|
2022-09-25 19:35:51 -04:00
|
|
|
if (fd == -1) {
|
|
|
|
ERR("Unable To Open Lock File: %s: %s", file, strerror(errno));
|
|
|
|
}
|
|
|
|
if (flock(fd, LOCK_EX) == -1) {
|
|
|
|
ERR("Unable To Lock File: %s: %s", file, strerror(errno));
|
|
|
|
}
|
|
|
|
return fd;
|
|
|
|
}
|
|
|
|
void unlock_file(const char *file, int fd) {
|
|
|
|
if (flock(fd, LOCK_UN) == -1) {
|
|
|
|
ERR("Unable To Unlock File: %s: %s", file, strerror(errno));
|
|
|
|
}
|
|
|
|
close(fd);
|
|
|
|
}
|