Skip to content

Commit 91ba2e3

Browse files
committed
Implement Pathname#lutime, arity split File.lutime/utime
1 parent f6318a9 commit 91ba2e3

2 files changed

Lines changed: 85 additions & 22 deletions

File tree

core/src/main/java/org/jruby/RubyFile.java

Lines changed: 80 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,6 @@
9292
import static org.jruby.api.Define.defineClass;
9393
import static org.jruby.api.Error.argumentError;
9494
import static org.jruby.api.Error.runtimeError;
95-
import static org.jruby.runtime.ThreadContext.hasKeywords;
9695
import static org.jruby.runtime.Visibility.PRIVATE;
9796
import static org.jruby.util.StringSupport.*;
9897
import static org.jruby.util.io.EncodingUtils.vmode;
@@ -1127,6 +1126,31 @@ public static IRubyObject umask(ThreadContext context, IRubyObject recv, IRubyOb
11271126
return asFixnum(context, oldMask);
11281127
}
11291128

1129+
@JRubyMethod(meta = true)
1130+
public static IRubyObject lutime(ThreadContext context, IRubyObject recv, IRubyObject atime, IRubyObject mtime) {
1131+
if (atime != context.nil || mtime != context.nil) {
1132+
extractTimespec(context, atime);
1133+
extractTimespec(context, mtime);
1134+
}
1135+
1136+
return asFixnum(context, 0);
1137+
}
1138+
1139+
@JRubyMethod(meta = true)
1140+
public static IRubyObject lutime(ThreadContext context, IRubyObject recv, IRubyObject atime, IRubyObject mtime, IRubyObject path) {
1141+
long[] atimeval = null;
1142+
long[] mtimeval = null;
1143+
1144+
if (atime != context.nil || mtime != context.nil) {
1145+
atimeval = convertTimespecToTimeval(extractTimespec(context, atime));
1146+
mtimeval = convertTimespecToTimeval(extractTimespec(context, mtime));
1147+
}
1148+
1149+
lutime(context, path, atimeval, mtimeval);
1150+
1151+
return asFixnum(context, 1);
1152+
}
1153+
11301154
@JRubyMethod(required = 2, rest = true, checkArity = false, meta = true)
11311155
public static IRubyObject lutime(ThreadContext context, IRubyObject recv, IRubyObject[] args) {
11321156
int argc = Arity.checkArgumentCount(context, args, 2, -1);
@@ -1141,22 +1165,52 @@ public static IRubyObject lutime(ThreadContext context, IRubyObject recv, IRubyO
11411165
}
11421166

11431167
for (int i = 2, j = argc; i < j; i++) {
1144-
var filename = get_path(context, args[i]).toString();
1145-
JRubyFile fileToTouch = JRubyFile.create(runtime.getCurrentDirectory(), filename);
1146-
if (!fileToTouch.exists()) throw runtime.newErrnoENOENTError(filename);
1147-
1148-
int result = runtime.getPosix().lutimes(fileToTouch.getAbsolutePath(), atimeval, mtimeval);
1149-
if (result == -1) throw runtime.newErrnoFromInt(runtime.getPosix().errno());
1168+
lutime(context, args[i], atimeval, mtimeval);
11501169
}
11511170

11521171
return asFixnum(context, argc - 2);
11531172
}
11541173

1174+
private static void lutime(ThreadContext context, IRubyObject _path, long[] atimeval, long[] mtimeval) {
1175+
Ruby runtime = context.runtime;
1176+
1177+
var filename = get_path(context, _path).toString();
1178+
1179+
JRubyFile fileToTouch = JRubyFile.create(runtime.getCurrentDirectory(), filename);
1180+
if (!fileToTouch.exists()) throw runtime.newErrnoENOENTError(filename);
1181+
1182+
int result = runtime.getPosix().lutimes(fileToTouch.getAbsolutePath(), atimeval, mtimeval);
1183+
if (result == -1) throw runtime.newErrnoFromInt(runtime.getPosix().errno());
1184+
}
1185+
1186+
@JRubyMethod(meta = true)
1187+
public static IRubyObject utime(ThreadContext context, IRubyObject recv, IRubyObject atime, IRubyObject mtime) {
1188+
if (atime != context.nil || mtime != context.nil) {
1189+
extractTimespec(context, atime);
1190+
extractTimespec(context, mtime);
1191+
}
1192+
1193+
return asFixnum(context, 0);
1194+
}
1195+
1196+
@JRubyMethod(meta = true)
1197+
public static IRubyObject utime(ThreadContext context, IRubyObject recv, IRubyObject atime, IRubyObject mtime, IRubyObject path) {
1198+
long[] atimespec = null;
1199+
long[] mtimespec = null;
1200+
if (atime != context.nil || mtime != context.nil) {
1201+
atimespec = extractTimespec(context, atime);
1202+
mtimespec = extractTimespec(context, mtime);
1203+
}
1204+
1205+
utime(context, atimespec, mtimespec, path);
1206+
1207+
return asFixnum(context, 1);
1208+
}
1209+
11551210
@JRubyMethod(required = 2, rest = true, checkArity = false, meta = true)
11561211
public static IRubyObject utime(ThreadContext context, IRubyObject recv, IRubyObject[] args) {
11571212
int argc = Arity.checkArgumentCount(context, args, 2, -1);
11581213

1159-
Ruby runtime = context.runtime;
11601214
long[] atimespec = null;
11611215
long[] mtimespec = null;
11621216

@@ -1166,26 +1220,30 @@ public static IRubyObject utime(ThreadContext context, IRubyObject recv, IRubyOb
11661220
}
11671221

11681222
for (int i = 2, j = argc; i < j; i++) {
1169-
var filename = get_path(context, args[i]).toString();
1170-
1171-
JRubyFile fileToTouch = JRubyFile.create(runtime.getCurrentDirectory(), filename);
1172-
if (!fileToTouch.exists()) throw runtime.newErrnoENOENTError(filename);
1173-
1174-
int result;
1175-
try {
1176-
result = runtime.getPosix().utimensat(0, fileToTouch.getAbsolutePath(), atimespec, mtimespec, 0);
1177-
} catch (NotImplementedError re) {
1178-
// fall back on utimes
1179-
result = runtime.getPosix().utimes(fileToTouch.getAbsolutePath(),
1180-
convertTimespecToTimeval(atimespec), convertTimespecToTimeval(mtimespec));
1181-
}
1223+
utime(context, atimespec, mtimespec, args[i]);
11821224

1183-
if (result == -1) throw runtime.newErrnoFromInt(runtime.getPosix().errno());
11841225
}
11851226

11861227
return asFixnum(context, argc - 2);
11871228
}
11881229

1230+
private static void utime(ThreadContext context, long[] atimespec, long[] mtimespec, IRubyObject args) {
1231+
Ruby runtime = context.runtime;
1232+
1233+
var filename = get_path(context, args).toString();
1234+
JRubyFile fileToTouch = JRubyFile.create(runtime.getCurrentDirectory(), filename);
1235+
if (!fileToTouch.exists()) throw runtime.newErrnoENOENTError(filename);
1236+
int result;
1237+
try {
1238+
result = runtime.getPosix().utimensat(0, fileToTouch.getAbsolutePath(), atimespec, mtimespec, 0);
1239+
} catch (NotImplementedError re) {
1240+
// fall back on utimes
1241+
result = runtime.getPosix().utimes(fileToTouch.getAbsolutePath(),
1242+
convertTimespecToTimeval(atimespec), convertTimespecToTimeval(mtimespec));
1243+
}
1244+
if (result == -1) throw runtime.newErrnoFromInt(runtime.getPosix().errno());
1245+
}
1246+
11891247
@JRubyMethod(rest = true, meta = true)
11901248
public static IRubyObject delete(ThreadContext context, IRubyObject recv, IRubyObject[] args) {
11911249
for (int i = 0; i < args.length; i++) {

core/src/main/java/org/jruby/ext/pathname/RubyPathname.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -378,6 +378,11 @@ public IRubyObject empty_p(ThreadContext context) {
378378
fileTest.callMethod(context, "empty?", getPath());
379379
}
380380

381+
@JRubyMethod(name = "lutime")
382+
public IRubyObject lutime(ThreadContext context, IRubyObject atime, IRubyObject mtime) {
383+
return RubyFile.lutime(context, context.runtime.getFile(), atime, mtime, getPath());
384+
}
385+
381386
/* Helpers */
382387

383388
private IRubyObject[] insertPath(IRubyObject[] args, int i) {

0 commit comments

Comments
 (0)