|
3 | 3 | * Copyright (C) 2002 Roman Zippel <[email protected]>
|
4 | 4 | */
|
5 | 5 |
|
| 6 | +#include <sys/mman.h> |
6 | 7 | #include <sys/stat.h>
|
7 | 8 | #include <ctype.h>
|
8 | 9 | #include <errno.h>
|
@@ -36,6 +37,52 @@ static bool is_dir(const char *path)
|
36 | 37 | return S_ISDIR(st.st_mode);
|
37 | 38 | }
|
38 | 39 |
|
| 40 | +/* return true if the given two files are the same, false otherwise */ |
| 41 | +static bool is_same(const char *file1, const char *file2) |
| 42 | +{ |
| 43 | + int fd1, fd2; |
| 44 | + struct stat st1, st2; |
| 45 | + void *map1, *map2; |
| 46 | + bool ret = false; |
| 47 | + |
| 48 | + fd1 = open(file1, O_RDONLY); |
| 49 | + if (fd1 < 0) |
| 50 | + return ret; |
| 51 | + |
| 52 | + fd2 = open(file2, O_RDONLY); |
| 53 | + if (fd2 < 0) |
| 54 | + goto close1; |
| 55 | + |
| 56 | + ret = fstat(fd1, &st1); |
| 57 | + if (ret) |
| 58 | + goto close2; |
| 59 | + ret = fstat(fd2, &st2); |
| 60 | + if (ret) |
| 61 | + goto close2; |
| 62 | + |
| 63 | + if (st1.st_size != st2.st_size) |
| 64 | + goto close2; |
| 65 | + |
| 66 | + map1 = mmap(NULL, st1.st_size, PROT_READ, MAP_PRIVATE, fd1, 0); |
| 67 | + if (map1 == MAP_FAILED) |
| 68 | + goto close2; |
| 69 | + |
| 70 | + map2 = mmap(NULL, st2.st_size, PROT_READ, MAP_PRIVATE, fd2, 0); |
| 71 | + if (map2 == MAP_FAILED) |
| 72 | + goto close2; |
| 73 | + |
| 74 | + if (bcmp(map1, map2, st1.st_size)) |
| 75 | + goto close2; |
| 76 | + |
| 77 | + ret = true; |
| 78 | +close2: |
| 79 | + close(fd2); |
| 80 | +close1: |
| 81 | + close(fd1); |
| 82 | + |
| 83 | + return ret; |
| 84 | +} |
| 85 | + |
39 | 86 | /*
|
40 | 87 | * Create the parent directory of the given path.
|
41 | 88 | *
|
@@ -888,6 +935,13 @@ int conf_write(const char *name)
|
888 | 935 | fclose(out);
|
889 | 936 |
|
890 | 937 | if (*tmpname) {
|
| 938 | + if (is_same(name, tmpname)) { |
| 939 | + conf_message("No change to %s", name); |
| 940 | + unlink(tmpname); |
| 941 | + sym_set_change_count(0); |
| 942 | + return 0; |
| 943 | + } |
| 944 | + |
891 | 945 | snprintf(oldname, sizeof(oldname), "%s.old", name);
|
892 | 946 | rename(name, oldname);
|
893 | 947 | if (rename(tmpname, name))
|
|
0 commit comments