diff options
author | Natanael Copa <ncopa@alpinelinux.org> | 2014-10-09 16:10:28 +0000 |
---|---|---|
committer | Natanael Copa <ncopa@alpinelinux.org> | 2014-10-09 16:10:28 +0000 |
commit | 0c5e302b3f9267c3a929531f848bf98398f48485 (patch) | |
tree | a5f2c3abc574e9c79b91dfb49ecf7a0e3f707cc9 /main/xen/xen-fd-is-file.c | |
parent | c2fa69955ac724d03c7c50f155e1a2dabc125c3c (diff) | |
download | aports-0c5e302b3f9267c3a929531f848bf98398f48485.tar.bz2 aports-0c5e302b3f9267c3a929531f848bf98398f48485.tar.xz |
main/xen: remove perl dependency
use a tiny C program to do the file/fd compare instead of perl
Diffstat (limited to 'main/xen/xen-fd-is-file.c')
-rw-r--r-- | main/xen/xen-fd-is-file.c | 62 |
1 files changed, 62 insertions, 0 deletions
diff --git a/main/xen/xen-fd-is-file.c b/main/xen/xen-fd-is-file.c new file mode 100644 index 0000000000..a1518e4df0 --- /dev/null +++ b/main/xen/xen-fd-is-file.c @@ -0,0 +1,62 @@ +#include <sys/stat.h> + +#include <stdio.h> +#include <fcntl.h> +#include <err.h> + +/* + +this is to be used as: + + while true; do + eval "exec $_lockfd<>$_lockfile" + flock -x $_lockfd || return $? + if xen-fd-is-file $_lockfd $_lockfile; then break; fi + eval "exec $_lockfd<&-" + done + +instead of: + + local rightfile + while true; do + eval "exec $_lockfd<>$_lockfile" + flock -x $_lockfd || return $? + # We can't just stat /dev/stdin or /proc/self/fd/$_lockfd or + # use bash's test -ef because those all go through what is + # actually a synthetic symlink in /proc and we aren't + # guaranteed that our stat(2) won't lose the race with an + # rm(1) between reading the synthetic link and traversing the + # file system to find the inum. Perl is very fast so use that. + rightfile=$( perl -e ' + open STDIN, "<&'$_lockfd'" or die $!; + my $fd_inum = (stat STDIN)[1]; die $! unless defined $fd_inum; + my $file_inum = (stat $ARGV[0])[1]; + print "y\n" if $fd_inum eq $file_inum; + ' "$_lockfile" ) + if [ x$rightfile = xy ]; then break; fi + # Some versions of bash appear to be buggy if the same + # $_lockfile is opened repeatedly. Close the current fd here. + eval "exec $_lockfd<&-" + done +*/ + +int main(int argc, char *argv[]) +{ + int lockfd; + const char *filename; + struct stat fdst, filest; + + if (argc <= 2) + errx(1, "usage: %s FDNUM FILENAME\n", argv[0]); + + lockfd = atoi(argv[1]); + filename = argv[2]; + + if (fstat(lockfd, &fdst) < 0) + err(2, "fstat(%i)", lockfd); + + if (stat(filename, &filest) < 0) + err(3, "stat(%s)", filename); + + return (fdst.st_ino != filest.st_ino); +} |