Skip to content

Commit 76ad54d

Browse files
committed
Add File.lutime method
1 parent 61cf24b commit 76ad54d

File tree

5 files changed

+43
-6
lines changed

5 files changed

+43
-6
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ Compatibility:
4444
* Do not autosplat a proc that accepts a single positional argument and keywords (#3039, @andrykonchin).
4545
* Support passing anonymous * and ** parameters as method call arguments (#3039, @andrykonchin).
4646
* Handle either positional or keywords arguments by default in `Struct.new` (#3039, @rwstauner).
47+
* Add `File.lutime` method (@andrykonchin).
4748

4849
Performance:
4950

spec/tags/core/file/lutime_tags.txt

Lines changed: 0 additions & 2 deletions
This file was deleted.

src/main/c/truffleposix/truffleposix.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,18 @@ int truffleposix_poll_single_fd(int fd, int events, int timeout_ms) {
9999
return poll(&fds, 1, timeout_ms) >= 0 ? fds.revents : -1;
100100
}
101101

102+
int truffleposix_lutimes(const char *filename, long atime_sec, int atime_nsec,
103+
long mtime_sec, int mtime_nsec) {
104+
struct timespec timespecs[2];
105+
106+
timespecs[0].tv_sec = atime_sec;
107+
timespecs[0].tv_nsec = atime_nsec;
108+
timespecs[1].tv_sec = mtime_sec;
109+
timespecs[1].tv_nsec = mtime_nsec;
110+
111+
return utimensat(AT_FDCWD, filename, timespecs, AT_SYMLINK_NOFOLLOW);
112+
}
113+
102114
int truffleposix_utimes(const char *filename, long atime_sec, int atime_nsec,
103115
long mtime_sec, int mtime_nsec) {
104116
struct timespec timespecs[2];

src/main/ruby/truffleruby/core/file.rb

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -790,6 +790,31 @@ def self.lstat(path)
790790
Stat.lstat path
791791
end
792792

793+
##
794+
# Sets the access and modification times of each named file to the first
795+
# two arguments. If a file is a symlink, this method acts upon the link
796+
# itself as opposed to its referent; for the inverse behavior, see
797+
# File.utime. Returns the number of file names in the argument list.
798+
def self.lutime(atime, mtime, *paths)
799+
if !atime || !mtime
800+
now = Time.now
801+
atime ||= now
802+
mtime ||= now
803+
end
804+
805+
atime = Time.at(atime) unless Primitive.is_a?(atime, Time)
806+
mtime = Time.at(mtime) unless Primitive.is_a?(mtime, Time)
807+
808+
paths.each do |path|
809+
path = Truffle::Type.coerce_to_path(path)
810+
n = POSIX.truffleposix_lutimes(path, atime.to_i, atime.nsec,
811+
mtime.to_i, mtime.nsec)
812+
Errno.handle unless n == 0
813+
end
814+
815+
paths.size
816+
end
817+
793818
##
794819
# Returns the modification time for the named file as a Time object.
795820
#
@@ -1062,10 +1087,10 @@ def self.unlink(*paths)
10621087
end
10631088

10641089
##
1065-
# Sets the access and modification times of each named
1066-
# file to the first two arguments. Returns the number
1067-
# of file names in the argument list.
1068-
# #=> Integer
1090+
# Sets the access and modification times of each named file to the first
1091+
# two arguments. If a file is a symlink, this method acts upon its
1092+
# referent rather than the link itself; for the inverse behavior see
1093+
# File.lutime. Returns the number of file names in the argument list.
10691094
def self.utime(atime, mtime, *paths)
10701095
if !atime || !mtime
10711096
now = Time.now

src/main/ruby/truffleruby/core/posix.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,7 @@ def self.attach_function_eagerly(native_name, argument_types, return_type,
204204
attach_function :lseek, [:int, :off_t, :int], :off_t
205205
attach_function :truffleposix_lstat, [:string, :pointer], :int, LIBTRUFFLEPOSIX
206206
attach_function :truffleposix_lstat_mode, [:string], :mode_t, LIBTRUFFLEPOSIX
207+
attach_function :truffleposix_lutimes, [:string, :long, :int, :long, :int], :int, LIBTRUFFLEPOSIX
207208
attach_function :truffleposix_major, [:dev_t], :uint, LIBTRUFFLEPOSIX
208209
attach_function :truffleposix_minor, [:dev_t], :uint, LIBTRUFFLEPOSIX
209210
attach_function :mkdir, [:string, :mode_t], :int

0 commit comments

Comments
 (0)