Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

why does open ">..." sometimes touches the directory?

by Mark_Galeck (Novice)
on Feb 05, 2013 at 08:31 UTC ( [id://1017082]=perlquestion: print w/replies, xml ) Need Help??

Mark_Galeck has asked for the wisdom of the Perl Monks concerning the following question:

Hello Monks, when using
open(FILE, ">existing_file") ... close(FILE)
, sometimes the directory of the existing file gets touched; I am on Linux, that means, there was some file added and/or removed during the code above. Perhaps the file in question was removed first, then added. I can't tell. This only happens some of the time, no sure why or what triggers it. Can you explain it? And how to prevent it from ever happening? That is, I don't want the directory timestamp touched. Thank you, Mark

Replies are listed 'Best First'.
Re: why does open ">..." sometimes touches the directory?
by flexvault (Monsignor) on Feb 05, 2013 at 09:27 UTC

    Hello Mark_Galeck,

    Since you're on Linux, what you are seeing is "normal". Occasionally I get interrupted and forget where I put a file, I can go to the head of the file system and follow the updated directories to locate the file. ( Hint: all directories are 'touch'ed, and on a very active system this technique may not work. ) When installing your *nix, you can turn this feature off to get better performance. Usually flash drives and Live CD/DVDs are build this way.

    If you have administrative rights you can use the 'ln -s' command to bypass how you access the file, for example:

    /home/mark/bin/myscript ## This file exists ln -s /home/mark/bin /mark ## /mark is symbolic link to /home +/mark/bin touch /mark/myscript ## change timestamp on directories + to myscript

    The actual directories to 'myscript' will have their timestamps updated, but the symbolic link '/mark' will not change. ( If that is what you want ).

    However as a system admin, I would be very concerned about 'end users' not wanting to show that they added/updated a file.

    Good Luck...Ed

    Update: This tested correctly on AIX, but after reading dave_the_m comments, I tried it on a "debian" and a "suse" systems with mixed results.

    "Well done is better than well said." - Benjamin Franklin

Re: why does open ">..." sometimes touches the directory?
by kcott (Archbishop) on Feb 05, 2013 at 09:29 UTC

    G'day Mark,

    The directory is a file. You can view it in an editor. When you open a file for writing, even if you don't write anything, the information in the directory file will change and, accordingly, so will the timestamp. You can see this for yourself at the command line (i.e. without using perl) - here's a sample run:

    ken@ganymede: ~/tmp $ ls -al check_dir_touch ls: check_dir_touch: No such file or directory ken@ganymede: ~/tmp $ mkdir check_dir_touch ken@ganymede: ~/tmp $ ls -al check_dir_touch total 0 drwxr-xr-x 2 ken staff 68 5 Feb 20:12 . drwxr-xr-x 211 ken staff 7174 5 Feb 20:12 .. ken@ganymede: ~/tmp $ view check_dir_touch --------------------- Editor window shows various info with no files listed (exit editor without attempting to save anything) --------------------- ken@ganymede: ~/tmp $ > check_dir_touch/fred ken@ganymede: ~/tmp $ ls -al check_dir_touch total 0 drwxr-xr-x 3 ken staff 102 5 Feb 20:19 . drwxr-xr-x 210 ken staff 7140 5 Feb 20:18 .. -rw-r--r-- 1 ken staff 0 5 Feb 20:19 fred ken@ganymede: ~/tmp $ view check_dir_touch --------------------- Editor window now shows "fred" (exit editor without attempting to save anything) ---------------------

    I agree with vinoth.ree's comment about using the 3-argument form of open; however, that won't change the behaviour of the underlying operating system.

    -- Ken

Re: why does open ">..." sometimes touches the directory?
by vinoth.ree (Monsignor) on Feb 05, 2013 at 08:42 UTC

    In Linux '>' redirect symbol is used to create new file, if the file exists it removes the existing content and creates new file.

    Use Three-argument open(), to open a file its safer open my $fh, '>', $filename or die "Can't write to '$filename': $!\n"; Update:

    Shorthand Flags:

    Entities Definition
    < or r Read Only Access
    > or w Creates, Writes, and Truncates
    >> or a Writes, Appends, and Creates
    +< or r+ Reads and Writes
    +> or w+ Reads, Writes, Creates, and Truncates
    +>> or a+ Reads, Writes, Appends, and Creates
Re: why does open ">..." sometimes touches the directory?
by dave_the_m (Monsignor) on Feb 05, 2013 at 09:41 UTC
    A simple open(FILE, ">existing_file") will not update the directory's mtime. On linux, the open just gets translated to the OS call
    open("existing_file", O_WRONLY|O_CREAT|O_TRUNC, 0666)
    (as verified by strace). Either the open you're doing is not as straightforward as your example, or something else is modifying the directory.

    Dave.

      That's right. As I said, the "open" that I am doing, is only _sometimes_ modifying the directory. I don't know why.

      I would respectfully disagree with what some of the other Monks seem to be saying, is that overwriting a file, must touch the directory. As shown below.

      the question is, why do my Perl scripts sometimes do it and sometimes don't - and I checked, all they do explicitly, is open for writing, and write, and close, not remove anything

      The reason I care, is because I am a makefiles guy. As such, I don't want to touch _anything_ that does not need absolutely need to be touched, so that the system does not rebuild too much. Yes I do have dependencies on directories (meaning, their contents listing). When I do Perl scripts, sometimes, they will open an existing file for writing, and the directory gets touched. That's bad for me.

      mgaleck{262}: touch test/foobar mgaleck{263}: ls -ld test drwxr-xr-x 2 mgaleck sw-team 138 Feb 5 03:30 test mgaleck{264}: date Tue Feb 5 03:31:12 PST 2013 mgaleck{265}: echo foobar > test/foobar mgaleck@hq1-up-swe-07{266}: ls -ld test drwxr-xr-x 2 mgaleck sw-team 138 Feb 5 03:30 test

        Same does not happen on FreeBSD 8.3-STABLE when tried in zsh 4.3.10 & tcsh 6.18.01 (Astron) 2012-02-14 ...

        mkdir -p ./tmp/test ; ll -d ./tmp/test ; sleep 60 ; echo > ./tmp/test/ +file ; ll -d ./tmp/test drwx------ 2 parv people 512 Feb 5 01:48 ./tmp/test/ drwx------ 2 parv people 512 Feb 5 01:49 ./tmp/test/

        ... got the same result when directory was created in /tmp. I have to say I would be (rudely) surprised if the directory modification time did not change after a file was created.

        On my NTFS windows machine, create modifies directory, delete modifies directory, but not append or clobber

        However updating mtime after delete is not immediate, it is delayed (I assume a seeking optimization)

        the question is, why do my Perl scripts sometimes do it and sometimes don't - and I checked, all they do explicitly, is open for writing, and write, and close, not remove anything

        Maybe they use rename, maybe they use utime, maybe they call other functions or programs that do manage to change mtime, .... you seem to think open is responsible, but it probably isn't

Re: why does open ">..." sometimes touches the directory?
by Anonymous Monk on Feb 05, 2013 at 08:44 UTC

    That is, I don't want the directory timestamp touched.

    Why do you care? It is very weird to care, suspicious even, in any case, utime

Re: why does open ">..." sometimes touches the directory?
by sundialsvc4 (Abbot) on Feb 05, 2013 at 14:08 UTC

    Probably the safest assumption is simply to assume that “it might.”   Actual behavior can depend on the OS-type and, within Unix/Linux, exactly what filesystem is used and how that filesystem is configured.   All of which is really beyond your control.   So, don&rquo;t get upset if the mod-time of a directory changes, and, don’t use mod-time as a sentinel to tell you whether or not you need to dumpster-dive into a particular directory.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1017082]
Approved by moritz
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others contemplating the Monastery: (5)
As of 2024-04-18 05:38 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found