One way to modify content of a file is run mmap
with flag MAP_SHARED
and then write in memory region returned. For example:
struct data *data;
const int size = sizeof(struct data);
int fd = open("data_file", O_RDWR);
ftruncate(fd, size);
data = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
/* Access to 'data' members */
Let's consider I use a journalized filesystem (ext4
with data=ordered
or data=journal
). What precautions I should take in order to allow to recover data from data_file
after a power outage?
IMO, Linux guarantees that write operations will be ordered but it does not guarantee any atomicity. Therefore, application have to implement a kind of journal in order to recover data_file
(as most databases do). Do you confirm that?