lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame^] | 1 | /* Distilled from issue found with tar and symlinks. |
| 2 | * Make sure that the whole stat struct between runs |
| 3 | * is agreeable. |
| 4 | */ |
| 5 | |
| 6 | #ifndef _GNU_SOURCE |
| 7 | # define _GNU_SOURCE |
| 8 | #endif |
| 9 | |
| 10 | #include <stdio.h> |
| 11 | #include <stdlib.h> |
| 12 | #include <string.h> |
| 13 | #include <errno.h> |
| 14 | #include <sys/types.h> |
| 15 | #include <sys/stat.h> |
| 16 | #include <fcntl.h> |
| 17 | #include <unistd.h> |
| 18 | #include <assert.h> |
| 19 | #include <time.h> |
| 20 | |
| 21 | static void show_stat(struct stat *st) |
| 22 | { |
| 23 | printf( |
| 24 | "------------------\n" |
| 25 | "st_dev = %li\n" |
| 26 | "st_ino = %li\n" |
| 27 | "st_mode = %li\n" |
| 28 | "st_nlink = %li\n" |
| 29 | "st_uid = %li\n" |
| 30 | "st_gid = %li\n" |
| 31 | "st_rdev = %li\n" |
| 32 | "st_size = %li\n" |
| 33 | "st_blksize = %li\n" |
| 34 | "st_blocks = %li\n" |
| 35 | "st_atime = %li\n" |
| 36 | "st_ansec = %li\n" |
| 37 | "st_mtime = %li\n" |
| 38 | "st_mnsec = %li\n" |
| 39 | "st_ctime = %li\n" |
| 40 | "st_cnsec = %li\n", |
| 41 | (long int)st->st_dev, |
| 42 | (long int)st->st_ino, |
| 43 | (long int)st->st_mode, |
| 44 | (long int)st->st_nlink, |
| 45 | (long int)st->st_uid, |
| 46 | (long int)st->st_gid, |
| 47 | (long int)st->st_rdev, |
| 48 | (long int)st->st_size, |
| 49 | (long int)st->st_blksize, |
| 50 | (long int)st->st_blocks, |
| 51 | #if !defined(__UCLIBC__) || defined(__USE_MISC) |
| 52 | (long int)st->st_atime, |
| 53 | (long int)st->st_atim.tv_nsec, |
| 54 | (long int)st->st_mtime, |
| 55 | (long int)st->st_mtim.tv_nsec, |
| 56 | (long int)st->st_ctime, |
| 57 | (long int)st->st_ctim.tv_nsec |
| 58 | #else |
| 59 | (long int)st->st_atime, |
| 60 | (long int)st->st_atimensec, |
| 61 | (long int)st->st_mtime, |
| 62 | (long int)st->st_mtimensec, |
| 63 | (long int)st->st_ctime, |
| 64 | (long int)st->st_ctimensec |
| 65 | #endif |
| 66 | ); |
| 67 | } |
| 68 | |
| 69 | int main(void) |
| 70 | { |
| 71 | int ret; |
| 72 | int fd; |
| 73 | struct stat fst, st; |
| 74 | |
| 75 | memset(&fst, 0xAA, sizeof(fst)); |
| 76 | memset(&st, 0x55, sizeof(st)); |
| 77 | |
| 78 | unlink(".testfile"); |
| 79 | fd = open(".testfile", O_WRONLY | O_CREAT | O_EXCL, 0); |
| 80 | if (fd < 0) { |
| 81 | perror("open(.testfile) failed"); |
| 82 | return 1; |
| 83 | } |
| 84 | ret = fstat(fd, &fst); |
| 85 | if (ret != 0) { |
| 86 | perror("fstat(.testfile) failed"); |
| 87 | return 1; |
| 88 | } |
| 89 | close(fd); |
| 90 | |
| 91 | ret = stat(".testfile", &st); |
| 92 | if (ret != 0) { |
| 93 | perror("stat(.testfile) failed"); |
| 94 | return 1; |
| 95 | } |
| 96 | |
| 97 | ret = memcmp(&fst, &st, sizeof(fst)); |
| 98 | if (ret != 0) { |
| 99 | printf("FAILED: memcmp() = %i\n", ret); |
| 100 | show_stat(&fst); |
| 101 | show_stat(&st); |
| 102 | } |
| 103 | |
| 104 | unlink(".testfile"); |
| 105 | |
| 106 | return ret; |
| 107 | } |