86 static bool rename_file(
const char*
const oldpath,
const char*
const newpath) {
88 if(rename(oldpath, newpath) == -1) {
89 SAH_TRACEZ_ERROR(
ME,
"Failed to rename %s to %s: %s", oldpath, newpath,
93 if((fd = open(newpath, O_RDWR)) == -1) {
94 SAH_TRACEZ_ERROR(
ME,
"Failed to open %s: %s", newpath, strerror(errno));
97 if(fdatasync(fd) == -1) {
98 SAH_TRACEZ_ERROR(
ME,
"Failed to sync %s: %s", newpath, strerror(errno));
122 value, strlen(value));
124 const size_t line_len = strlen(line);
125 const size_t value_len = strlen(value);
126 if(line_len != value_len) {
131 if(strncmp(line, value, strlen(value)) == 0) {
154 bool write_file(
const char*
const path,
const char*
const value) {
158 const size_t value_len = strlen(value);
160 SAH_TRACEZ_ERROR(
ME,
"strlen(value) = %zd >= %d", value_len,
LINE_LEN);
167 if(access(path, F_OK) == 0) {
169 bool has_value =
false;
176 if(snprintf(tmpfile, 256,
"%s.tmp", path) < 0) {
177 SAH_TRACEZ_ERROR(
ME,
"%s: failed to create path with .tmp", path);
181 FILE* f = fopen(tmpfile,
"w");
183 SAH_TRACEZ_ERROR(
ME,
"Failed to open %s: %s", tmpfile, strerror(errno));
188 if(fputs(value, f) == EOF) {
189 SAH_TRACEZ_ERROR(
ME,
"Failed to write to %s", tmpfile);
193 if(fclose(f) == EOF) {
194 SAH_TRACEZ_ERROR(
ME,
"Failed to close %s: %s", tmpfile, strerror(errno));
213 FILE* f = fopen(path,
"r");
215 SAH_TRACEZ_ERROR(
ME,
"Failed to open %s: %s", path, strerror(errno));
219 if(!fgets(line, (
int) len, f)) {
220 SAH_TRACEZ_ERROR(
ME,
"Failed to read from %s", path);
223 if(fclose(f) == EOF) {
224 SAH_TRACEZ_ERROR(
ME,
"Failed to close %s: %s", path, strerror(errno));
static bool check_file_has_value(const char *const path, const char *const value, bool *has_value)
bool write_file(const char *const path, const char *const value)
bool read_first_line_from_file(const char *const path, char *line, size_t len)
static bool rename_file(const char *const oldpath, const char *const newpath)
#define SAH_TRACEZ_DEBUG2(zone, format,...)