if (le64toh(f->header->tail_object_offset) > (le64toh(f->header->header_size) + le64toh(f->header->arena_size)))
return -ENODATA;
- if (!VALID64(f->header->data_hash_table_offset) ||
- !VALID64(f->header->field_hash_table_offset) ||
- !VALID64(f->header->tail_object_offset) ||
- !VALID64(f->header->entry_array_offset))
+ if (!VALID64(le64toh(f->header->data_hash_table_offset)) ||
+ !VALID64(le64toh(f->header->field_hash_table_offset)) ||
+ !VALID64(le64toh(f->header->tail_object_offset)) ||
+ !VALID64(le64toh(f->header->entry_array_offset)))
+ return -ENODATA;
+
+ if (le64toh(f->header->data_hash_table_offset) < le64toh(f->header->header_size) ||
+ le64toh(f->header->field_hash_table_offset) < le64toh(f->header->header_size) ||
+ le64toh(f->header->tail_object_offset) < le64toh(f->header->header_size) ||
+ le64toh(f->header->entry_array_offset) < le64toh(f->header->header_size))
return -ENODATA;
if (f->writable) {
f->compress = JOURNAL_HEADER_COMPRESSED(f->header);
- if (f->writable)
- f->seal = JOURNAL_HEADER_SEALED(f->header);
+ f->seal = JOURNAL_HEADER_SEALED(f->header);
return 0;
}
assert(f);
assert(ret);
+ if (size <= 0)
+ return -EINVAL;
+
/* Avoid SIGBUS on invalid accesses */
if (offset + size > (uint64_t) f->last_stat.st_size) {
/* Hmm, out of range? Let's refresh the fstat() data
assert(f);
assert(o);
assert(offset > 0);
- assert(o->object.type == OBJECT_DATA);
+
+ if (o->object.type != OBJECT_DATA)
+ return -EINVAL;
/* This might alter the window we are looking at */
if (r < 0)
return r;
-#ifdef HAVE_GCRYPT
- r = journal_file_hmac_put_object(f, OBJECT_DATA, p);
- if (r < 0)
- return r;
-#endif
-
/* The linking might have altered the window, so let's
* refresh our pointer */
r = journal_file_move_to_object(f, OBJECT_DATA, p, &o);
if (r < 0)
return r;
+#ifdef HAVE_GCRYPT
+ r = journal_file_hmac_put_object(f, OBJECT_DATA, o, p);
+ if (r < 0)
+ return r;
+#endif
+
if (ret)
*ret = o;
uint64_t journal_file_entry_n_items(Object *o) {
assert(o);
- assert(o->object.type == OBJECT_ENTRY);
+
+ if (o->object.type != OBJECT_ENTRY)
+ return 0;
return (le64toh(o->object.size) - offsetof(Object, entry.items)) / sizeof(EntryItem);
}
uint64_t journal_file_entry_array_n_items(Object *o) {
assert(o);
- assert(o->object.type == OBJECT_ENTRY_ARRAY);
+
+ if (o->object.type != OBJECT_ENTRY_ARRAY)
+ return 0;
return (le64toh(o->object.size) - offsetof(Object, entry_array.items)) / sizeof(uint64_t);
}
uint64_t journal_file_hash_table_n_items(Object *o) {
assert(o);
- assert(o->object.type == OBJECT_DATA_HASH_TABLE ||
- o->object.type == OBJECT_FIELD_HASH_TABLE);
+
+ if (o->object.type != OBJECT_DATA_HASH_TABLE &&
+ o->object.type != OBJECT_FIELD_HASH_TABLE)
+ return 0;
return (le64toh(o->object.size) - offsetof(Object, hash_table.items)) / sizeof(HashItem);
}
return r;
#ifdef HAVE_GCRYPT
- r = journal_file_hmac_put_object(f, OBJECT_ENTRY_ARRAY, q);
+ r = journal_file_hmac_put_object(f, OBJECT_ENTRY_ARRAY, o, q);
if (r < 0)
return r;
#endif
assert(f);
assert(o);
assert(offset > 0);
- assert(o->object.type == OBJECT_ENTRY);
+
+ if (o->object.type != OBJECT_ENTRY)
+ return -EINVAL;
__sync_synchronize();
o->entry.boot_id = f->header->boot_id;
#ifdef HAVE_GCRYPT
- r = journal_file_hmac_put_object(f, OBJECT_ENTRY, np);
+ r = journal_file_hmac_put_object(f, OBJECT_ENTRY, o, np);
if (r < 0)
return r;
#endif
__sync_synchronize();
if (ftruncate(f->fd, f->last_stat.st_size) < 0)
- log_error("Failed to to truncate file to its own size: %m");
+ log_error("Failed to truncate file to its own size: %m");
}
int journal_file_append_entry(JournalFile *f, const dual_timestamp *ts, const struct iovec iovec[], unsigned n_iovec, uint64_t *seqnum, Object **ret, uint64_t *offset) {
void journal_file_print_header(JournalFile *f) {
char a[33], b[33], c[33];
char x[FORMAT_TIMESTAMP_MAX], y[FORMAT_TIMESTAMP_MAX];
+ struct stat st;
+ char bytes[FORMAT_BYTES_MAX];
assert(f);
if (JOURNAL_HEADER_CONTAINS(f->header, n_entry_arrays))
printf("Entry Array Objects: %llu\n",
(unsigned long long) le64toh(f->header->n_entry_arrays));
+
+ if (fstat(f->fd, &st) >= 0)
+ printf("Disk usage: %s\n", format_bytes(bytes, sizeof(bytes), (off_t) st.st_blocks * 512ULL));
}
int journal_file_open(
bool newly_created = false;
assert(fname);
+ assert(ret);
if ((flags & O_ACCMODE) != O_RDONLY &&
(flags & O_ACCMODE) != O_RDWR)
f->flags = flags;
f->prot = prot_from_flags(flags);
f->writable = (flags & O_ACCMODE) != O_RDONLY;
+#ifdef HAVE_XZ
f->compress = compress;
+#endif
+#ifdef HAVE_GCRYPT
f->seal = seal;
+#endif
if (mmap_cache)
f->mmap = mmap_cache_ref(mmap_cache);
#ifdef HAVE_GCRYPT
/* Try to load the FSPRG state, and if we can't, then
* just don't do sealing */
- r = journal_file_fss_load(f);
- if (r < 0)
- f->seal = false;
+ if (f->seal) {
+ r = journal_file_fss_load(f);
+ if (r < 0)
+ f->seal = false;
+ }
#endif
r = journal_file_init_header(f, template);
if (r < 0)
goto fail;
- if (ret)
- *ret = f;
-
+ *ret = f;
return 0;
fail: