SAME_INODE comparison
In the context of the preceding message, I saw code using memcmp to compare two stat buffers to see if they refer to the same inode.
Any objection to using this macro,
# define SAME_INODE(Stat_buf_1, Stat_buf_2) \
((Stat_buf_1).st_ino == (Stat_buf_2).st_ino \
&& (Stat_buf_1).st_dev == (Stat_buf_2).st_dev)in the three places where that is done?
if (!memcmp(&info.st_ino, &info2.st_ino, sizeof(ino_t)))
!memcmp(&buf1.st_ino, &buf2.st_ino, sizeof(ino_t)))
!memcmp(&buf1.st_ino, &buf2.st_ino, sizeof(ino_t)))Using the macro makes the code more readable, and also adds the st_dev comparison, so that if by some bizarre chance (or mischief) you get two files with the same st_ino value, but from *different* devices, it won't get a false positive. E.g., the above would become
if (SAME_INODE(info, info2)
SAME_INODE(&buf1, &buf2))
SAME_INODE(buf1, buf2))FYI, those uses are from these two files:
lib/filters/filter-persistent.c
lib/locking/file_locking.c
Here are the two .h files directly included by both of those .c files:
#include "lib.h"
#include "config.h"So, I'm inclined to put the #define (or an equivalent static inline function) in e.g., lib/misc/misc.h and let lib/misc/lib.h include misc.h.
Alternative: put the macro/function directly in lib.h.
Preferences?
Quoted 8 lines · Jim Meyering
In the context of the preceding message, I saw code using memcmp to compare two stat buffers to see if they refer to the same inode.
Any objection to using this macro,
# define SAME_INODE(Stat_buf_1, Stat_buf_2) \ ((Stat_buf_1).st_ino == (Stat_buf_2).st_ino \ && (Stat_buf_1).st_dev == (Stat_buf_2).st_dev)
In case you want something concrete, here's the proposed patch:
When testing same-stat-buffer, test st_dev in addition to st_ino.
* lib/misc/lib.h (SAME_INODE): Define.
* lib/filters/filter-persistent.c (persistent_filter_dump):
Use SAME_INODE in place of a direct st_ino-only comparison.
* lib/locking/file_locking.c (_release_lock, _lock_file): Likewise.
* WHATS_NEW: Mention this.
--- WHATS_NEW | 1 + lib/filters/filter-persistent.c | 2 +- lib/locking/file_locking.c | 4 ++-- lib/misc/lib.h | 4 ++++ 4 files changed, 8 insertions(+), 3 deletions(-)
WHATS_NEW +1 -0
@@ -1,5 +1,6 @@ Version 2.02.27 - ================================ + When testing same-stat-buffer, test st_dev in addition to st_ino. Fix configure libdevmapper.h check when --with-dmdir is used. Turn _add_pv_to_vg() into external library function add_pv_to_vg(). Add pv_by_path() external library function.
lib/filters/filter-persistent.c +1 -1
@@ -208,7 +208,7 @@ int persistent_filter_dump(struct dev_filter *f) goto out; } -if (!memcmp (&info.st_ino, &info2.st_ino,sizeof (ino_t))) +if (SAME_INODE (info, info2))break ;fcntl_unlock_file (lockfd);
lib/locking/file_locking.c +2 -2
@@ -64,7 +64,7 @@ static int _release_lock(const char *file, int unlock)if (!flock (ll->lf,LOCK_NB |LOCK_EX ) && !stat (ll->res, &buf1) && !fstat (ll->lf, &buf2) && - !memcmp (&buf1.st_ino, &buf2.st_ino,sizeof (ino_t))) +SAME_INODE (buf1, buf2))if (unlink (ll->res))log_sys_error ("unlink" , ll->res);
@@ -189,7 +189,7 @@ static int _lock_file(const char *file, int flags) }if (!stat (ll->res, &buf1) && !fstat (ll->lf, &buf2) && - !memcmp (&buf1.st_ino, &buf2.st_ino,sizeof (ino_t))) +SAME_INODE (buf1, buf2))break ; }while (!(flags &LCK_NONBLOCK ));
lib/misc/lib.h +4 -0
@@ -32,4 +32,8 @@#include <libdevmapper.h> +#define SAME_INODE (Stat_buf_1, Stat_buf_2) \ + ((Stat_buf_1).st_ino == (Stat_buf_2).st_ino \ + && (Stat_buf_1).st_dev == (Stat_buf_2).st_dev) +#endif
Signature · 1 line
1.5.3.rc1.27.ga5e40