http://www.perlmonks.org?node_id=1017093


in reply to why does open ">..." sometimes touches the directory?

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.

Replies are listed 'Best First'.
Re^2: why does open ">..." sometimes touches the directory?
by Mark_Galeck (Novice) on Feb 05, 2013 at 11:39 UTC

    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.

      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

      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)