Jordfräs
Jordfräs

Reputation: 9163

How to synchronize a file move to storage?

I am working with a C/C++ system in a Linux environment where I am moving files using the rename() function available in stdio.h.

After the move I am need of functionality to sync this to the underlying storage to make the change permanent. If I had a file descriptor I would be able to use fsync() or fdatasync().

Is there an elegant way of doing this? Or do I have to do something like this:

rename(old_path, new_path);
int fd = open(new_path, O_APPEND | O_WRONLY);
fdatasync(fd);
close(fd);

Will that even work?

Upvotes: 2

Views: 524

Answers (1)

deimus
deimus

Reputation: 9883

Probably you are looking for void sync(void); function.

The sync function simply queues all the modified block buffers for writing and returns, it does not wait for the disk writes to take place.

The function sync is normally called periodically (usually every 30 seconds) from a system daemon, often called update.

This guarantees regular flushing of the kernel’s block buffers.

Upvotes: 1

Related Questions