summaryrefslogtreecommitdiff
path: root/lib/astal/io/file.vala
blob: a1e773c0d7d59cc564076151fa1aa2793da64613 (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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
namespace AstalIO {
/**
 * Read the contents of a file synchronously.
 */
public string read_file(string path) {
    var str = "";
    try {
        FileUtils.get_contents(path, out str, null);
    } catch (Error error) {
        critical(error.message);
    }
    return str;
}

/**
 * Read the contents of a file asynchronously.
 */
public async string read_file_async(string path) throws Error {
    uint8[] content;
    yield File.new_for_path(path).load_contents_async(null, out content, null);
    return (string)content;
}

/**
 * Write content to a file synchronously.
 */
public void write_file(string path, string content) {
    try {
        var dir = Path.get_dirname(path);
        if (!FileUtils.test(dir, FileTest.IS_DIR)) {
            File.new_for_path(dir).make_directory_with_parents(null);
        }

        File.new_for_path(path).replace_contents(
            content.data,
            null,
            false,
            FileCreateFlags.NONE,
            null,
            null
        );
    } catch (Error error) {
        critical(error.message);
    }
}

/**
 * Write content to a file asynchronously.
 */
public async void write_file_async(string path, string content) throws Error {
    var dir = Path.get_dirname(path);
    if (!FileUtils.test(dir, FileTest.IS_DIR)) {
        File.new_for_path(dir).make_directory_with_parents(null);
    }

    yield File.new_for_path(path).replace_contents_async(
        content.data,
        null,
        false,
        FileCreateFlags.NONE,
        null,
        null
    );
}

/**
 * Monitor a file for changes. If the path is a directory, monitor it recursively.
 * The callback will be called passed two parameters: the path of the file
 * that changed and an [[email protected]] indicating the reason.
 */
public FileMonitor? monitor_file(string path, Closure callback) {
    try {
        var file = File.new_for_path(path);
        var mon = file.monitor(
            FileMonitorFlags.WATCH_HARD_LINKS |
            FileMonitorFlags.WATCH_MOUNTS |
            FileMonitorFlags.WATCH_MOVES
        );

        mon.changed.connect((file, _file, event) => {
            var f = Value(Type.STRING);
            var e = Value(Type.INT);
            var ret = Value(Type.POINTER);

            f.set_string(file.get_path());
            e.set_int(event);

            callback.invoke(ref ret, { f, e });
        });

        if (FileUtils.test(path, FileTest.IS_DIR)) {
            var enumerator = file.enumerate_children("standard::*",
                FileQueryInfoFlags.NONE, null);

            var i = enumerator.next_file(null);
            while (i != null) {
                if (i.get_file_type() == FileType.DIRECTORY) {
                    var filepath = file.get_child(i.get_name()).get_path();
                    if (filepath != null) {
                        var m = monitor_file(filepath, callback);
                        mon.notify["cancelled"].connect(() => {
                            m.cancel();
                        });
                    }
                }
                i = enumerator.next_file(null);
            }
        }

        mon.ref();
        mon.notify["cancelled"].connect(() => {
            mon.unref();
        });
        return mon;
    } catch (Error error) {
        critical(error.message);
        return null;
    }
}
}