-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcrtime.c
208 lines (174 loc) · 4.75 KB
/
crtime.c
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
/*
* Derived from debugfs.c in e2fsprogs and inspired by:
* http://unix.stackexchange.com/questions/50177/birth-is-empty-on-ext4/50184
*
* Copyright (C) 1993 Theodore Ts'o. This file may be redistributed
* under the terms of the GNU Public License.
*
* Modifications by Robert Sanders <[email protected]>
*
* To build:
* apt-get install e2fslibs-dev # or similar
* gcc -o crtime crtime.c -lext2fs
*
* To let non-root users get creation times of files:
* chown root ./crtime
* chmod u+s ./crtime
*
* To run:
* target=/path/to/some/file
* fs=$(df "${target}" | tail -1 | awk '{print $1}');
* ./crtime ${fs} ${target}
*/
#include <ext2fs/ext2fs.h>
#include <stdio.h>
#include <sys/stat.h>
#include <unistd.h>
ext2_filsys current_fs;
static int open_filesystem(char *device)
{
int retval;
io_manager io_ptr = unix_io_manager;
retval = ext2fs_open(device, 0, 0, 0, io_ptr, ¤t_fs);
if (retval) {
fprintf(stderr, "Error while opening filesystem: %d\n", retval);
return retval;
}
return 0;
}
static int get_inode(char *f, ext2_ino_t *inode) {
struct stat sb;
if (stat(f, &sb) == -1) {
perror("stat");
return errno;
}
if ((sizeof(sb.st_ino) > sizeof(ext2_ino_t)) &&
(sb.st_ino > 0xffffffff)) {
fprintf(stderr,
"Inode of %s is bigger than what e2fslibs supports\n",
f);
return -1;
}
memcpy((void *)inode, (void *)&sb.st_ino, sizeof(ext2_ino_t));
return 0;
}
/* http://unix.stackexchange.com/questions/32795/what-is-the-maximum-allowed-filename-and-folder-size-with-ecryptfs */
#define MAX_PATH 4096
#define DF_COMMAND "/bin/df "
/* NB: caller must call free on **fs on success */
int get_fs_name(char *f, char **fs) {
FILE *fp = NULL;
char cmd[MAX_PATH + sizeof(DF_COMMAND) + 1];
int retval;
char *first_space;
*fs = NULL;
retval = snprintf(cmd, sizeof(cmd), DF_COMMAND"%s", f);
if (retval >= sizeof(cmd) - 1) {
fprintf(stderr, "%s too long!", f);
goto fail;
}
fp = popen(cmd, "r");
if (fp == NULL) {
fprintf(stderr, "Failed to run df command\n");
goto fail;
}
/* leave space for newline and space for null termination */
*fs = malloc(MAX_PATH + 2);
if (*fs == NULL) {
fprintf(stderr, "Failed to allocate memory for df output\n");
goto fail;
}
/* throw away the header */
if (fgets(*fs, MAX_PATH, fp) == NULL) {
fprintf(stderr, "Failed to read df header\n");
goto fail;
}
if (fgets(*fs, MAX_PATH, fp) == NULL) {
fprintf(stderr, "failed to read data from df\n");
goto fail;
}
/* assume the drive name has no spaces (i.e., the first space is a
* delimiter)
*/
first_space = strchr(*fs, ' ');
if (!first_space) {
fprintf(stderr, "Did not find newline in df output.\n");
goto fail;
}
*first_space = 0;
pclose(fp);
return 0;
fail:
if (*fs)
free(*fs);
if (fp)
pclose(fp);
return -1;
}
int check_permissions(char *f) {
int retval;
retval = access(f, R_OK);
if (retval) {
perror(f);
}
return retval;
}
int main(int argc, char *argv[]) {
ext2_ino_t inode;
struct ext2_inode *inode_buf;
struct ext2_inode_large *large_inode;
int retval;
char *fs;
if (argc != 2) {
fprintf(stderr, "Usage: crtime <file>\n");
return -1;
}
retval = check_permissions(argv[1]);
if (retval) {
return retval;
}
retval = get_inode(argv[1], &inode);
if (retval) {
return retval;
}
retval = get_fs_name(argv[1], &fs);
if (retval) {
return retval;
}
retval = open_filesystem(fs);
if (retval) {
free(fs);
return retval;
}
inode_buf = (struct ext2_inode *)malloc(EXT2_INODE_SIZE(current_fs->super));
if (!inode_buf) {
free(fs);
fprintf(stderr, "Failed to allocate memory\n");
return -1;
}
retval = ext2fs_read_inode_full(current_fs, inode, inode_buf, EXT2_INODE_SIZE(current_fs->super));
if (retval) {
fprintf(stderr, "Failed to read inode\n");
free(fs);
free(inode_buf);
return retval;
}
if (EXT2_INODE_SIZE(current_fs->super) <= EXT2_GOOD_OLD_INODE_SIZE) {
free(fs);
free(inode_buf);
fprintf(stderr, "Create time unavailable");
return -1;
}
large_inode = (struct ext2_inode_large *)inode_buf;
if (large_inode->i_extra_isize < 24) {
free(fs);
free(inode_buf);
fprintf(stderr, "Create time unavailable");
return -1;
}
printf("%d\n", large_inode->i_crtime);
free(fs);
free(inode_buf);
ext2fs_close(current_fs);
return 0;
}