-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfiles.c
More file actions
71 lines (59 loc) · 1.67 KB
/
files.c
File metadata and controls
71 lines (59 loc) · 1.67 KB
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
#include <errno.h>
#include <stdio.h>
#include <sys/stat.h>
#include "files.h"
#include "graph.h"
#include "list.h"
#include "string.h"
#ifndef STAT_MTIME_SEC
#define STAT_MTIME_SEC st_mtime
#endif
/*
* Not all systems implement sub-second precision modification time. The default
* is to read only the whole seconds part of the modification time. If the
* operating system supports sub-second precision, define the STAT_MTIME_NSEC
* macros as the relevant member of the stat structure. This can vary on
* different systems, but it is usually st_mtim.tv_nsec or st_mtimespec.tv_nsec.
*/
static void extract_mtime(const struct stat *stat, struct my_timespec *ts) {
ts->sec = stat->STAT_MTIME_SEC;
#ifdef STAT_MTIME_NSEC
ts->nsec = stat->STAT_MTIME_NSEC;
#else
ts->nsec = 0;
#endif
}
int update_file_info(struct graph_node *node) {
const struct string *name = graph_node_get_name(node);
const char *name_cstr = string_get_cstr(name);
struct stat stat_buf;
int ret;
ret = stat(name_cstr, &stat_buf);
if (ret == 0) {
struct my_timespec ts;
extract_mtime(&stat_buf, &ts);
graph_node_set_time(node, &ts);
} else if (errno == ENOENT || errno == ENOTDIR) {
/*
* If the file does not exist, it is not an error,
* just don't set the modification time.
*/
} else {
fprintf(stderr, "Could not stat file \"%s\"\n",
name_cstr);
return (-1);
}
return (0);
}
int update_all_files_info(struct graph *graph) {
struct list *nodes = graph_get_nodes(graph);
struct list_item *item = list_head(nodes);
while (item != NULL) {
struct graph_node *node = list_get_data(item);
if (update_file_info(node)) {
return (-1);
}
item = list_next(item);
}
return (0);
}