Unlink command for opened files

Discussion in 'Perl Misc' started by Prasanth, Apr 16, 2008.

  1. Prasanth

    Prasanth Guest

    When a file is open does unlink automatically deletes the file or it
    generates an error. Because I wrote a program to delete a file and
    regenerate the file. if the file is not deleted the data simply gets
    appended to the existing file. which results in wrong results
     
    Prasanth, Apr 16, 2008
    #1
    1. Advertising

  2. Prasanth <> writes:

    > When a file is open does unlink automatically deletes the file or it
    > generates an error. Because I wrote a program to delete a file and
    > regenerate the file. if the file is not deleted the data simply gets
    > appended to the existing file. which results in wrong results


    Unlink removes inodes, and it should succeed in that even if the file is
    opened by any process. It does not empty files, if that's what you mean.


    --
    Joost Diepenmaat | blog: http://joost.zeekat.nl/ | work: http://zeekat.nl/
     
    Joost Diepenmaat, Apr 16, 2008
    #2
    1. Advertising

  3. Prasanth <> wrote in
    news:270a0c78-9d5e-4486-8b35-b429c4d61755
    @u36g2000prf.googlegroups.co
    m:

    > When a file is open does unlink automatically deletes the file or
    > it generates an error. Because I wrote a program to delete a file
    > and regenerate the file. if the file is not deleted the data
    > simply gets appended to the existing file. which results in wrong
    > results


    Hmmmm ... What is the question?

    If you script opened the file, close it before calling unlink.

    If the file is opened in exclusive mode by another process, I am not
    sure deleting it is the correct action in the first place.

    If you don't want your script to continue if unlink failed, you
    should check the return value of unlink.

    On the other hand, not that this will solve whatever problem you are
    having, I would not have messed with unlink at all:

    open my $out_h, '>', 'report.txt'
    or die "Cannot open 'report.txt': $!";

    That truncates the file if it can be opened and would die if it
    cannot. Beats appending junk to a file.

    There are just two many combinations of problems and solutions that
    might apply to your post. Please elaborate after reading the posting
    guidelines for this group and may be taking a look at the following
    page:

    http://blogs.msdn.com/oldnewthing/archive/2008/04/15/8397753.aspx

    Sinan

    --
    A. Sinan Unur <>
    (remove .invalid and reverse each component for email address)

    comp.lang.perl.misc guidelines on the WWW:
    http://www.rehabitation.com/clpmisc/
     
    A. Sinan Unur, Apr 16, 2008
    #3
  4. Joost Diepenmaat <> wrote in
    news::

    > Prasanth <> writes:
    >
    >> When a file is open does unlink automatically deletes the file or
    >> it generates an error. Because I wrote a program to delete a file
    >> and regenerate the file. if the file is not deleted the data
    >> simply gets appended to the existing file. which results in wrong
    >> results

    >
    > Unlink removes inodes, and it should succeed in that even if the
    > file is opened by any process. It does not empty files, if that's
    > what you mean.


    I am guessing the OP is on Windows where

    http://windowshelp.microsoft.com/Windows/en-US/help/47b3ce2b-3aec-401d-8be3-74434a1999831033.mspx

    Sinan

    --
    A. Sinan Unur <>
    (remove .invalid and reverse each component for email address)

    comp.lang.perl.misc guidelines on the WWW:
    http://www.rehabitation.com/clpmisc/
     
    A. Sinan Unur, Apr 16, 2008
    #4
  5. Prasanth

    Ben Morrow Guest

    Quoth Joost Diepenmaat <>:
    > Prasanth <> writes:
    >
    > > When a file is open does unlink automatically deletes the file or it
    > > generates an error. Because I wrote a program to delete a file and
    > > regenerate the file. if the file is not deleted the data simply gets
    > > appended to the existing file. which results in wrong results

    >
    > Unlink removes inodes, and it should succeed in that even if the file is
    > opened by any process. It does not empty files, if that's what you mean.


    Unlink removes *filenames*. Inodes are refcounted, and self-destruct
    when there are no refs to them (including open file descriptors).

    This only applies to Unixish filesystems, of course. Win32 won't let
    you unlink an open file (under normal circumstances).

    Ben
     
    Ben Morrow, Apr 16, 2008
    #5
  6. Prasanth <> wrote:
    >When a file is open does unlink automatically deletes the file or it
    >generates an error.


    That depends on the file system and/or operating system that you are
    using.

    jue
     
    Jürgen Exner, Apr 16, 2008
    #6
  7. Prasanth

    Guest

    Prasanth <> wrote:
    > When a file is open does unlink automatically deletes the file or it
    > generates an error. Because I wrote a program to delete a file and
    > regenerate the file. if the file is not deleted the data simply gets
    > appended to the existing file. which results in wrong results


    This depends on your OS.

    Xho

    --
    -------------------- http://NewsReader.Com/ --------------------
    The costs of publication of this article were defrayed in part by the
    payment of page charges. This article must therefore be hereby marked
    advertisement in accordance with 18 U.S.C. Section 1734 solely to indicate
    this fact.
     
    , Apr 16, 2008
    #7
  8. Chris Mattern <> wrote:
    >On 2008-04-16, Prasanth <> wrote:
    >> When a file is open does unlink automatically deletes the file or it
    >> generates an error. Because I wrote a program to delete a file and
    >> regenerate the file. if the file is not deleted the data simply gets
    >> appended to the existing file. which results in wrong results

    >
    >This is not a Perl question, this is a question about the filesystem
    >semantics for your particular operating system; the answer can differ
    >from OS to OS. I'll talk about Unix/Linux, because that's the one I
    >understand :). In Unix/Linux, when you delete the file, you actually
    >only delete the directory entry for the file.


    This is getting somewhat off topic, but now you triggered my curiousity.
    Does the behaviour really depend on the OS (as you seem to imply) or
    does it depend on the file system? Or on both?
    Example: FAT32 mounted on Linux. FAT32 does not have inodes or link
    counts and I would guess that Linux would not allow deletion of a file
    from a FAT32 partition as long as there is an open file descriptor
    dangling. Is this true or does Linux simulate an inode and link count
    structure on top of FAT?

    jue

    jue
     
    Jürgen Exner, Apr 16, 2008
    #8
  9. Prasanth

    Ben Morrow Guest

    Quoth Jürgen Exner <>:
    >
    > This is getting somewhat off topic, but now you triggered my curiousity.
    > Does the behaviour really depend on the OS (as you seem to imply) or
    > does it depend on the file system? Or on both?
    > Example: FAT32 mounted on Linux. FAT32 does not have inodes or link
    > counts and I would guess that Linux would not allow deletion of a file
    > from a FAT32 partition as long as there is an open file descriptor
    > dangling. Is this true or does Linux simulate an inode and link count
    > structure on top of FAT?


    For both FAT and NFS (which doesn't support close-behind either) Linux
    will fake the unlink by renaming the file to something 'unique', and
    really deleting it after it's closed. Grep the kernel source for
    'sillyrename'.

    I don't know if all OSen provide (or attempt to provide) consistent
    semantics across all filesystems they support.

    Ben
     
    Ben Morrow, Apr 16, 2008
    #9
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Paul Urbanus
    Replies:
    0
    Views:
    2,272
    Paul Urbanus
    Apr 7, 2006
  2. rbt
    Replies:
    4
    Views:
    2,229
  3. aryan

    ios::trunc vs unlink

    aryan, Jun 16, 2008, in forum: C++
    Replies:
    3
    Views:
    790
    James Kanze
    Jun 17, 2008
  4. Ruben van Engelenburg

    unlink and big files

    Ruben van Engelenburg, Nov 8, 2003, in forum: Perl Misc
    Replies:
    1
    Views:
    157
    Ruben van Engelenburg
    Nov 9, 2003
  5. Replies:
    3
    Views:
    124
    Peter Makholm
    Oct 31, 2008
Loading...

Share This Page