Alex Barylo on 20 Feb 2007 14:19:19 -0000


[Date Prev] [Date Next] [Thread Prev] [Thread Next] [Date Index] [Thread Index]

fcntl call to check if a file is open - help needed


Hi mongers,

I need to copy a file and I should not do it if there is another
process writing to it. To complicate things further, the file is
located on a CIFS share. I'm in the process of checking if fcntl
can do it for a network drive but I'm having problems using it even
on a local file.

Consider the script below. After reading some man and perldoc on
fcntl, open, etc, I'm still doing something wrong - it gives me
the same result regardless whether a file I'm checking is open or
not.

My system: RHEL 2.1, kernel 2.4.9-e.59enterprise, perl v5.6.1 

  [root@willow tmp]# lsof disk.txt
  [root@willow tmp]# ./fcntl_test disk.txt
  flags: 4294964736
  O_TRUNC
  O_APPEND
  O_SYNC
  O_NOFOLLOW
  O_DIRECTORY
  O_DIRECT
  O_ASYNC
  O_LARGEFILE

Now a run when the file is open for writing:

  [root@willow tmp]# lsof disk.txt
  COMMAND   PID    USER   FD   TYPE DEVICE  SIZE   NODE NAME
  perl    22877   oracle  4w   REG  104,6 18480 508036 disk.txt
  [root@willow tmp]# ./fcntl_test disk.txt
  flags: 4294964736
  O_TRUNC
  O_APPEND
  O_SYNC
  O_NOFOLLOW
  O_DIRECTORY
  O_DIRECT
  O_ASYNC
  O_LARGEFILE

Here is the script. Any pointers will be greatly appreciated.
=============================================
  #!/usr/bin/perl -w

  use Fcntl;
  use strict;
  use Symbol;

  my $fh = gensym;
  sysopen $fh, $ARGV[0], O_RDONLY or die "$ARGV[0]: Cannot open
($!)
\n";
  my $buf = '';
  fcntl($fh, F_GETFL, $buf) or die "Cannot run fcntl\n";
  my $flags = unpack 's', $buf;
  printf "flags: %u\n", $flags;
  for my $f (qw[O_CREAT O_EXCL O_NOCTTY O_TRUNC O_APPEND O_NONBLOCK
                O_SYNC O_NOFOLLOW O_DIRECTORY O_DIRECT O_ASYNC
                O_LARGEFILE]) {
    no strict;
    print "$f\n" if ($flags & &$f) == &$f;
  }
============================================= 

Thanks,
Alex.


--
Before the accident, I could not even spell UNIX
-
**Majordomo list services provided by PANIX <URL:http://www.panix.com>**
**To Unsubscribe, send "unsubscribe phl" to majordomo@lists.pm.org**