Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

utime for symbolic links

by shamlin (Initiate)
on Apr 15, 2020 at 12:43 UTC ( #11115566=perlquestion: print w/replies, xml ) Need Help??

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

I am trying to explicitly specify the atime and mtime of a symbolic link to on linux. I see that utime works for files only. Also, File::Touch (which uses utime) works only with files. I know can use a system call to touch -h -t, but I was wondering if there is a perl way to do it.

Replies are listed 'Best First'.
Re: utime for symbolic links
by haukex (Bishop) on Apr 15, 2020 at 17:03 UTC

    Apparently, what touch(1) uses internally is utimensat(2). I haven't found anything on CPAN yet* that directly supports what you want to do, but in theory, you could use FFI::Platypus to link to it. Time::HiRes's utime does use utimensat, but doesn't support setting the AT_SYMLINK_NOFOLLOW flag needed to modify the symlink instead of the file it points to.

    * Update: See here.

Re: utime for symbolic links
by choroba (Archbishop) on Apr 15, 2020 at 13:48 UTC
    It seems such an option is not present in File::Touch. I can't find it in any other module, either (but my CPAN searching skills are below average, I usually can't even find a module I've used).

    But what you want is easy to implement using plain Perl functions: instead of touching the old symlink, create a new one and rename it over the old one:

    #! /usr/bin/perl use strict; use warnings; use File::Touch qw{ touch }; sub show_stat { system qw( stat file link ); } unlink qw( file link new_link ); touch('file'); symlink 'file', 'link'; show_stat(); sleep 1; symlink 'file', 'new_link'; rename new_link => 'link'; show_stat();

    map{substr$_->[0],$_->[1]||0,1}[\*||{},3],[[]],[ref qr-1,-,-1],[{}],[sub{}^*ARGV,3]
      Your example sets the atime/mtime to current time. I need to set the atime and mtime of the link to a specific value. I updated my question to be more clear.

        The documentation seems clear enough, no?

        atime => $time
        
            If defined, use this time (in epoch seconds) instead of current time for access time.
        
        mtime => $time
        
            If defined, use this time (in epoch seconds) instead of current time for modification time.
        
        
Re: utime for symbolic links
by Fletch (Chancellor) on Apr 15, 2020 at 16:43 UTC

    My guess is that touch -h is using the lutimes(3) system call (where available) which a quick ripgrep through the perl source doesn't show any reference to. You theoretically could wrap it directly yourself with Inline::C but I don't think you're otherwise going to have luck directly from perl.

    Edit: derp, wrong man section fixed

    The cake is a lie.
    The cake is a lie.
    The cake is a lie.

      My guess is that touch -h is using the lutimes(3) system call

      No (at least not in GNU coreutils), but that's an excellent hint nonetheless - a search on CPAN reveals File::lchown that does support lutimes!

Re: utime for symbolic links
by Anonymous Monk on Apr 15, 2020 at 13:13 UTC
    My distant memory is that if you check the time of a file that is referenced by a symbolic link, you get the time of the referenced file, not the link. I really don't know what happens if you try to modify the time, but you might need to de-reference it to the target file.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (5)
As of 2021-10-28 08:51 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    My first memorable Perl project was:







    Results (96 votes). Check out past polls.

    Notices?