return FALSE;
}
+ rspamd_file_lock (fd, FALSE);
+
if (fstat (fd, &st) == -1) {
+ rspamd_file_unlock (fd, FALSE);
close (fd);
msg_info_cache ("cannot stat file %s, error %d, %s", name,
errno, strerror (errno));
}
if (st.st_size < (gint)sizeof (*hdr)) {
+ rspamd_file_unlock (fd, FALSE);
close (fd);
errno = EINVAL;
msg_info_cache ("cannot use file %s, error %d, %s", name,
map = mmap (NULL, st.st_size, PROT_READ, MAP_SHARED, fd, 0);
if (map == MAP_FAILED) {
+ rspamd_file_unlock (fd, FALSE);
close (fd);
msg_info_cache ("cannot mmap file %s, error %d, %s", name,
errno, strerror (errno));
return FALSE;
}
- close (fd);
hdr = map;
if (memcmp (hdr->magic, rspamd_symbols_cache_magic,
sizeof (rspamd_symbols_cache_magic)) != 0) {
msg_info_cache ("cannot use file %s, bad magic", name);
munmap (map, st.st_size);
+ rspamd_file_unlock (fd, FALSE);
+ close (fd);
+
return FALSE;
}
ucl_parser_get_error (parser));
munmap (map, st.st_size);
ucl_parser_free (parser);
+ rspamd_file_unlock (fd, FALSE);
+ close (fd);
+
return FALSE;
}
top = ucl_parser_get_object (parser);
munmap (map, st.st_size);
+ rspamd_file_unlock (fd, FALSE);
+ close (fd);
ucl_parser_free (parser);
if (top == NULL || ucl_object_type (top) != UCL_OBJECT) {
struct ucl_emitter_functions *efunc;
gpointer k, v;
gint fd;
- FILE *f;
bool ret;
- fd = open (name, O_CREAT | O_TRUNC | O_WRONLY, 00644);
+ (void)unlink (name);
+ fd = open (name, O_CREAT | O_TRUNC | O_WRONLY | O_EXCL, 00644);
if (fd == -1) {
msg_info_cache ("cannot open file %s, error %d, %s", name,
return FALSE;
}
+ rspamd_file_lock (fd, FALSE);
+
memset (&hdr, 0, sizeof (hdr));
memcpy (hdr.magic, rspamd_symbols_cache_magic,
sizeof (rspamd_symbols_cache_magic));
if (write (fd, &hdr, sizeof (hdr)) == -1) {
msg_info_cache ("cannot write to file %s, error %d, %s", name,
errno, strerror (errno));
+ rspamd_file_unlock (fd, FALSE);
close (fd);
return FALSE;
ucl_object_insert_key (top, elt, k, 0, false);
}
- f = fdopen (fd, "a");
- g_assert (f != NULL);
-
- efunc = ucl_object_emit_file_funcs (f);
+ efunc = ucl_object_emit_fd_funcs (fd);
ret = ucl_object_emit_full (top, UCL_EMIT_JSON_COMPACT, efunc, NULL);
ucl_object_emit_funcs_free (efunc);
ucl_object_unref (top);
- fclose (f);
+ rspamd_file_unlock (fd, FALSE);
+ close (fd);
return ret;
}