[Markdown] 

Linux hints: syncfs (linux)

everybody knows what sometime it’s necessary to manually sync filesystems, for example before some risky actions, but not many know what linux have ability to sync just one of mounted filesystem which often can be really convenient, i still surprised what for all this years nobody does not created wrapper for this apis, so:

#define _GNU_SOURCE

#include < unistd.h>
#include < stdio.h>
#include < errno.h>
#include < string.h>

int main(int argc, char** argv)
{
    if(argc != 2)
    {
        printf("usage:\n\t%s < some valid path insied filesystem>\n", argv[0]);
        return -1;
    }
    FILE *f = fopen(argv[1], "r");
    if(!f)
    {
        printf("fopen failed %s\n", strerror(errno));
        return -1;
    }
    int fd = fileno(f);
    int err = syncfs(fd);
    if(err < 0)
    {
        printf("syncfs failed %s\n", strerror(errno));
    }
        return 0;
}