return 0;
}
-static int write_uint64(int fd, uint64_t p) {
- ssize_t k;
-
- k = write(fd, &p, sizeof(p));
- if (k < 0)
+static int write_uint64(FILE *fp, uint64_t p) {
+ if (fwrite(&p, sizeof(p), 1, fp) != 1)
return -errno;
- if (k != sizeof(p))
- return -EIO;
return 0;
}
bool entry_seqnum_set = false, entry_monotonic_set = false, entry_realtime_set = false, found_main_entry_array = false;
uint64_t n_weird = 0, n_objects = 0, n_entries = 0, n_data = 0, n_fields = 0, n_data_hash_tables = 0, n_field_hash_tables = 0, n_entry_arrays = 0, n_tags = 0;
usec_t last_usec = 0;
- int data_fd = -1, entry_fd = -1, entry_array_fd = -1;
+ _cleanup_close_ int data_fd = -1, entry_fd = -1, entry_array_fd = -1;
+ _cleanup_fclose_ FILE *data_fp = NULL, *entry_fp = NULL, *entry_array_fp = NULL;
MMapFileDescriptor *cache_data_fd = NULL, *cache_entry_fd = NULL, *cache_entry_array_fd = NULL;
unsigned i;
bool found_last = false;
goto fail;
}
+ r = take_fdopen_unlocked(&data_fd, "w+", &data_fp);
+ if (r < 0) {
+ log_error_errno(r, "Failed to open data file stream: %m");
+ goto fail;
+ }
+
+ r = take_fdopen_unlocked(&entry_fd, "w+", &entry_fp);
+ if (r < 0) {
+ log_error_errno(r, "Failed to open entry file stream: %m");
+ goto fail;
+ }
+
+ r = take_fdopen_unlocked(&entry_array_fd, "w+", &entry_array_fp);
+ if (r < 0) {
+ log_error_errno(r, "Failed to open entry array file stream: %m");
+ goto fail;
+ }
+
if (le32toh(f->header->compatible_flags) & ~HEADER_COMPATIBLE_SUPPORTED) {
log_error("Cannot verify file with unknown extensions.");
r = -EOPNOTSUPP;
switch (o->object.type) {
case OBJECT_DATA:
- r = write_uint64(data_fd, p);
+ r = write_uint64(data_fp, p);
if (r < 0)
goto fail;
goto fail;
}
- r = write_uint64(entry_fd, p);
+ r = write_uint64(entry_fp, p);
if (r < 0)
goto fail;
break;
case OBJECT_ENTRY_ARRAY:
- r = write_uint64(entry_array_fd, p);
+ r = write_uint64(entry_array_fp, p);
if (r < 0)
goto fail;
goto fail;
}
+ if (fflush(data_fp) != 0) {
+ r = log_error_errno(errno, "Failed to flush data file stream: %m");
+ goto fail;
+ }
+
+ if (fflush(entry_fp) != 0) {
+ r = log_error_errno(errno, "Failed to flush entry file stream: %m");
+ goto fail;
+ }
+
+ if (fflush(entry_array_fp) != 0) {
+ r = log_error_errno(errno, "Failed to flush entry array file stream: %m");
+ goto fail;
+ }
+
/* Second iteration: we follow all objects referenced from the
* two entry points: the object hash table and the entry
* array. We also check that everything referenced (directly
mmap_cache_free_fd(f->mmap, cache_entry_fd);
mmap_cache_free_fd(f->mmap, cache_entry_array_fd);
- safe_close(data_fd);
- safe_close(entry_fd);
- safe_close(entry_array_fd);
-
if (first_contained)
*first_contained = le64toh(f->header->head_entry_realtime);
if (last_validated)
(unsigned long long) f->last_stat.st_size,
100 * p / f->last_stat.st_size);
- if (data_fd >= 0)
- safe_close(data_fd);
-
- if (entry_fd >= 0)
- safe_close(entry_fd);
-
- if (entry_array_fd >= 0)
- safe_close(entry_array_fd);
-
if (cache_data_fd)
mmap_cache_free_fd(f->mmap, cache_data_fd);