summaryrefslogtreecommitdiff
path: root/command/sync.c
blob: 797d7a583033d7a5f68c7ff6324a13b15ff0ff73 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#include "args.h"
#include "command.h"
#include "lslib.h"

#define _X_OPEN_SOURCE 500
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <string.h>

static struct {
    bool sync_fs;
    bool sync_data;
} flags;

static void help (void) {
    printf("Usage: sync [-df] [FILE]...\n\n");
    printf("Write all buffered blocks (in FILEs) to disk\n\n");
	printf("\t-d\tAvoid syncing metadata\n");
#ifdef _GNU_SOURCE
	printf("\t-f\tSync filesystems underlying FILEs\n");
#endif
}

static int short_arg (char c, char* next) {
    UNUSED(next);
    switch (c) {
        case 'd':
            flags.sync_data = true;
            break;
#ifdef _GNU_SOURCE
        case 'f':
            flags.sync_fs = true;
            break;
#endif
        default:
            return ARG_INVALID;
    }
    return ARG_UNUSED;
}

static bool sync_file(char* path) {
    int fd;
    int ret;

    fd = open(path, O_NOCTTY | O_RDONLY | O_NONBLOCK);
    if (fd < 0) {
        return false;
    }

    if (flags.sync_fs) {
#ifdef _GNU_SOURCE
        ret = syncfs(fd) >= 0;
#else 
        ret = 0;
#endif
    } else {
        ret = flags.sync_data ? fdatasync(fd) : fsync(fd);
        if (ret < 0) {
            error_s("failed to sync '%s': %s", path, strerror(errno));
        }
    }

    close(fd);
    return ret;
}

COMMAND(sync_cmd) {

    int start, i, ret = EXIT_SUCCESS;
    
    start = parse_args(argc, argv, help, short_arg, NULL);

    if (argc - start < 1) {
        sync();
        return EXIT_SUCCESS;
    }

    for (i = start; i < argc; i++) {
        if (!sync_file(argv[i])) ret = EXIT_FAILURE;
    }

    return ret;
}