| 1 | #include <stdio.h>
|
|---|
| 2 | #include <sys/types.h>
|
|---|
| 3 | #include <sys/time.h>
|
|---|
| 4 | #include <sys/stat.h>
|
|---|
| 5 | #include <fcntl.h>
|
|---|
| 6 | #include <unistd.h>
|
|---|
| 7 |
|
|---|
| 8 | #define TEST_TS 1199062800
|
|---|
| 9 |
|
|---|
| 10 | void TestIt(const char *fileName, int withWrite) {
|
|---|
| 11 | int h;
|
|---|
| 12 | struct timeval ts[2];
|
|---|
| 13 | struct stat buf;
|
|---|
| 14 | int dummy;
|
|---|
| 15 |
|
|---|
| 16 | printf("create %s\n", fileName);
|
|---|
| 17 | h = open(fileName, O_CREAT|O_RDWR|O_TRUNC);
|
|---|
| 18 | if (h == -1) {
|
|---|
| 19 | printf("unable to create %s\n", fileName);
|
|---|
| 20 | return;
|
|---|
| 21 | }
|
|---|
| 22 | if (withWrite) {
|
|---|
| 23 | dummy = 1234;
|
|---|
| 24 | printf("write some data\n");
|
|---|
| 25 | if (write(h, &dummy, sizeof(dummy)) != sizeof(dummy)) {
|
|---|
| 26 | printf("unable to write\n");
|
|---|
| 27 | }
|
|---|
| 28 | }
|
|---|
| 29 | fsync(h);
|
|---|
| 30 | ts[0].tv_sec = TEST_TS;
|
|---|
| 31 | ts[0].tv_usec = 0;
|
|---|
| 32 | ts[1].tv_sec = TEST_TS;
|
|---|
| 33 | ts[1].tv_usec = 0;
|
|---|
| 34 | if (futimes(h, ts) < 0) {
|
|---|
| 35 | printf("unable to futimes\n");
|
|---|
| 36 | }
|
|---|
| 37 | close(h);
|
|---|
| 38 | if (stat(fileName, &buf) < 0) {
|
|---|
| 39 | printf("unable to stat %s\n", fileName);
|
|---|
| 40 | return;
|
|---|
| 41 | }
|
|---|
| 42 | printf("reftime=%d atime=%d mtime=%d\n", TEST_TS, (int)buf.st_atime, (int)buf.st_mtime);
|
|---|
| 43 | if (buf.st_mtime != TEST_TS) {
|
|---|
| 44 | printf("wrong mtime !!!\n");
|
|---|
| 45 | }
|
|---|
| 46 | }
|
|---|
| 47 |
|
|---|
| 48 | int main() {
|
|---|
| 49 | TestIt("./t1.bin", 0);
|
|---|
| 50 | TestIt("./t2.bin", 1);
|
|---|
| 51 | }
|
|---|