Skip to content

Commit dd6db7f

Browse files
committed
[GR-19346] Backport: hotfix FastR working.dir vs java user.dir issues.
PullRequest: fastr/2223
2 parents d93857d + 51facf1 commit dd6db7f

File tree

7 files changed

+239
-46
lines changed

7 files changed

+239
-46
lines changed

ci_common/common.hocon

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
java7 : {name : oraclejdk, version : "7", platformspecific: true}
33
# java 8 must be a jvmci enabled variant
44
java8 : {name : oraclejdk, version : "8u231-jvmci-19.3-b04", platformspecific: true}
5-
java11 : {name : labsjdk, version : "ce-11.0.5+9-jvmci-19.3-b03", platformspecific: true}
5+
java11 : {name : labsjdk, version : "ce-11.0.5+10-jvmci-19.3-b04", platformspecific: true}
66

77
java8Downloads : {
88
downloads : {

com.oracle.truffle.r.nodes.builtin/src/com/oracle/truffle/r/nodes/builtin/base/FileFunctions.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -824,6 +824,9 @@ protected RStringVector listDirs(RAbstractStringVector paths, boolean fullNames,
824824
}
825825
while (iter.hasNext()) {
826826
TruffleFile dir = iter.next();
827+
if (!recursive && dir.equals(root)) {
828+
continue;
829+
}
827830
if (!fullNames) {
828831
dir = vecPath.relativize(dir);
829832
}

com.oracle.truffle.r.runtime/src/com/oracle/truffle/r/runtime/FileSystemUtils.java

Lines changed: 43 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,6 @@
5454
import com.oracle.truffle.api.TruffleFile;
5555
import com.oracle.truffle.api.TruffleLanguage.Env;
5656
import com.oracle.truffle.r.runtime.RError.Message;
57-
import java.nio.file.NoSuchFileException;
58-
import java.util.logging.Level;
5957

6058
public class FileSystemUtils {
6159
private static PosixFilePermission[] permissionValues = PosixFilePermission.values();
@@ -650,44 +648,50 @@ public static String toUnixRegexPattern(String globPattern) {
650648
}
651649

652650
public static TruffleFile getSafeTruffleFile(Env env, String path) {
653-
TruffleFile origFile = env.getInternalTruffleFile(path);
654-
655-
TruffleFile f = origFile;
656-
try {
657-
origFile = env.getInternalTruffleFile(path);
658-
if (origFile.exists()) {
659-
try {
660-
f = origFile.getCanonicalFile();
661-
} catch (NoSuchFileException e) {
662-
// absolute path "exists", but cannonical does not
663-
// happens e.g. during install.packages: file.exists("/{rHome}/inst/..")
664-
// lets optimistically FALLBACK on absolute path
665-
}
666-
}
667-
} catch (IOException e) {
668-
RLogger.getLogger(RLogger.LOGGER_FILE_ACCEESS).log(Level.SEVERE, "Unable to access file " + path + " " + e.getMessage(), e);
669-
throw RError.error(RError.SHOW_CALLER, Message.FILE_OPEN_ERROR);
670-
}
671-
672-
final TruffleFile home = REnvVars.getRHomeTruffleFile(env);
673-
if (f.startsWith(home) && isLibraryFile(home.relativize(f))) {
674-
return origFile;
675-
} else {
676-
try {
677-
return env.getPublicTruffleFile(path);
678-
} catch (SecurityException e) {
679-
RLogger.getLogger(RLogger.LOGGER_FILE_ACCEESS).log(Level.SEVERE, "Unable to access file " + path + " " + e.getMessage(), e);
680-
throw RError.error(RError.SHOW_CALLER, Message.FILE_OPEN_ERROR);
681-
}
682-
}
651+
// TODO: HOTFIX the commented in code bellow does not work well with the FastR
652+
// 'working.dir' vs java 'user.dir' dichotomy
653+
return env.getInternalTruffleFile(path);
654+
655+
// TruffleFile origFile = env.getInternalTruffleFile(path);
656+
//
657+
// TruffleFile f = origFile;
658+
// try {
659+
// origFile = env.getInternalTruffleFile(path);
660+
// if (origFile.exists()) {
661+
// try {
662+
// f = origFile.getCanonicalFile();
663+
// } catch (NoSuchFileException e) {
664+
// // absolute path "exists", but cannonical does not
665+
// // happens e.g. during install.packages: file.exists("/{rHome}/inst/..")
666+
// // lets optimistically FALLBACK on absolute path
667+
// }
668+
// }
669+
// } catch (IOException e) {
670+
// RLogger.getLogger(RLogger.LOGGER_FILE_ACCEESS).log(Level.SEVERE, "Unable to access file "
671+
// + path + " " + e.getMessage(), e);
672+
// throw RError.error(RError.SHOW_CALLER, Message.FILE_OPEN_ERROR);
673+
// }
674+
//
675+
// final TruffleFile home = REnvVars.getRHomeTruffleFile(env);
676+
// if (f.startsWith(home) && isLibraryFile(home.relativize(f))) {
677+
// return origFile;
678+
// } else {
679+
// try {
680+
// return env.getPublicTruffleFile(path);
681+
// } catch (SecurityException e) {
682+
// RLogger.getLogger(RLogger.LOGGER_FILE_ACCEESS).log(Level.SEVERE, "Unable to access file "
683+
// + path + " " + e.getMessage(), e);
684+
// throw RError.error(RError.SHOW_CALLER, Message.FILE_OPEN_ERROR);
685+
// }
686+
// }
683687
}
684688

685-
private static boolean isLibraryFile(TruffleFile relativePathFromHome) {
686-
final String fileName = relativePathFromHome.getName();
687-
if (fileName == null) {
688-
return false;
689-
}
690-
return relativePathFromHome.startsWith("library");
691-
}
689+
// private static boolean isLibraryFile(TruffleFile relativePathFromHome) {
690+
// final String fileName = relativePathFromHome.getName();
691+
// if (fileName == null) {
692+
// return false;
693+
// }
694+
// return relativePathFromHome.startsWith("library");
695+
// }
692696

693697
}

com.oracle.truffle.r.runtime/src/com/oracle/truffle/r/runtime/Utils.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -914,4 +914,11 @@ public static String wildcardToRegex(String wildcard) {
914914
return (s.toString());
915915
}
916916

917+
/**
918+
* Testing purposes only.
919+
*/
920+
public static void resetWD() {
921+
wdState().current = wdState().initial;
922+
}
923+
917924
}

com.oracle.truffle.r.test/src/com/oracle/truffle/r/test/ExpectedTestOutput.test

Lines changed: 77 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33933,6 +33933,10 @@ integer(0)
3393333933
#argv <- list('^[ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789]', 'all.R', FALSE, FALSE, FALSE, FALSE, FALSE, FALSE); .Internal(grep(argv[[1]], argv[[2]], argv[[3]], argv[[4]], argv[[5]], argv[[6]], argv[[7]], argv[[8]]))
3393433934
[1] 1
3393533935

33936+
##com.oracle.truffle.r.test.builtins.TestBuiltin_grepRaw.testFixedEmpty#
33937+
#grepRaw('adf', '', fixed=TRUE)
33938+
integer(0)
33939+
3393633940
##com.oracle.truffle.r.test.builtins.TestBuiltin_grepRaw.testFixedIgnoreInvert#
3393733941
#grepRaw('no match', 'XXXXXXXX', fixed=T, invert=T, value=F)
3393833942
integer(0)
@@ -41993,10 +41997,47 @@ attr(,"class")
4199341997
NULL
4199441998

4199541999

41996-
##com.oracle.truffle.r.test.builtins.TestBuiltin_listdirs.testlistdirs1#Ignored.SideEffects#
41997-
#argv <- list('/home/lzhao/hg/r-instrumented/library/rpart/doc', TRUE, FALSE); .Internal(list.dirs(argv[[1]], argv[[2]], argv[[3]]))
42000+
##com.oracle.truffle.r.test.builtins.TestBuiltin_listdirs.testlistdirs1#
42001+
#list.dirs('com.oracle.truffle.r.test/src/com/oracle/truffle/r/test/simple/data', full.names=F, recursive=F)
42002+
[1] "tree1" "tree2"
42003+
42004+
##com.oracle.truffle.r.test.builtins.TestBuiltin_listdirs.testlistdirs1#
42005+
#list.dirs('com.oracle.truffle.r.test/src/com/oracle/truffle/r/test/simple/data', full.names=F, recursive=T)
42006+
[1] "" "tree1" "tree1/subdir" "tree2"
42007+
42008+
##com.oracle.truffle.r.test.builtins.TestBuiltin_listdirs.testlistdirs1#
42009+
#list.dirs('com.oracle.truffle.r.test/src/com/oracle/truffle/r/test/simple/data', full.names=T, recursive=F)
42010+
[1] "com.oracle.truffle.r.test/src/com/oracle/truffle/r/test/simple/data/tree1"
42011+
[2] "com.oracle.truffle.r.test/src/com/oracle/truffle/r/test/simple/data/tree2"
42012+
42013+
##com.oracle.truffle.r.test.builtins.TestBuiltin_listdirs.testlistdirs1#
42014+
#list.dirs('com.oracle.truffle.r.test/src/com/oracle/truffle/r/test/simple/data', full.names=T, recursive=T)
42015+
[1] "com.oracle.truffle.r.test/src/com/oracle/truffle/r/test/simple/data"
42016+
[2] "com.oracle.truffle.r.test/src/com/oracle/truffle/r/test/simple/data/tree1"
42017+
[3] "com.oracle.truffle.r.test/src/com/oracle/truffle/r/test/simple/data/tree1/subdir"
42018+
[4] "com.oracle.truffle.r.test/src/com/oracle/truffle/r/test/simple/data/tree2"
42019+
42020+
##com.oracle.truffle.r.test.builtins.TestBuiltin_listdirs.testlistdirs1#
42021+
#list.dirs('does-not-exist', full.names=F, recursive=F)
4199842022
character(0)
4199942023

42024+
##com.oracle.truffle.r.test.builtins.TestBuiltin_listdirs.testlistdirs1#
42025+
#wd <- getwd(); setwd('com.oracle.truffle.r.test/src/com/oracle/truffle/r/test/simple'); list.dirs('data', full.names=F, recursive=F); setwd(wd)
42026+
[1] "tree1" "tree2"
42027+
42028+
##com.oracle.truffle.r.test.builtins.TestBuiltin_listdirs.testlistdirs1#
42029+
#wd <- getwd(); setwd('com.oracle.truffle.r.test/src/com/oracle/truffle/r/test/simple'); list.dirs('data', full.names=F, recursive=T); setwd(wd)
42030+
[1] "" "tree1" "tree1/subdir" "tree2"
42031+
42032+
##com.oracle.truffle.r.test.builtins.TestBuiltin_listdirs.testlistdirs1#
42033+
#wd <- getwd(); setwd('com.oracle.truffle.r.test/src/com/oracle/truffle/r/test/simple'); list.dirs('data', full.names=T, recursive=F); setwd(wd)
42034+
[1] "data/tree1" "data/tree2"
42035+
42036+
##com.oracle.truffle.r.test.builtins.TestBuiltin_listdirs.testlistdirs1#
42037+
#wd <- getwd(); setwd('com.oracle.truffle.r.test/src/com/oracle/truffle/r/test/simple'); list.dirs('data', full.names=T, recursive=T); setwd(wd)
42038+
[1] "data" "data/tree1" "data/tree1/subdir"
42039+
[4] "data/tree2"
42040+
4200042041
##com.oracle.truffle.r.test.builtins.TestBuiltin_listfiles.testEmptyDir#
4200142042
#{ list.files("com.oracle.truffle.r.test/src/com/oracle/truffle/r/test/simple/empty_dir", all.files=TRUE, recursive=TRUE, no..=FALSE) }
4200242043
character(0)
@@ -76351,6 +76392,38 @@ In set.seed("hello world") : NAs introduced by coercion
7635176392
#.Internal(shortRowNames(42, -2))
7635276393
Error: invalid 'type' argument
7635376394

76395+
##com.oracle.truffle.r.test.builtins.TestBuiltin_shortRowNames.testArgCasts#
76396+
#.Internal(shortRowNames(42, 0))
76397+
NULL
76398+
76399+
##com.oracle.truffle.r.test.builtins.TestBuiltin_shortRowNames.testArgCasts#
76400+
#.Internal(shortRowNames(42, 1))
76401+
[1] 0
76402+
76403+
##com.oracle.truffle.r.test.builtins.TestBuiltin_shortRowNames.testArgCasts#
76404+
#.Internal(shortRowNames(42, 2))
76405+
[1] 0
76406+
76407+
##com.oracle.truffle.r.test.builtins.TestBuiltin_shortRowNames.testArgCasts#
76408+
#.Internal(shortRowNames(NULL, '0'))
76409+
NULL
76410+
76411+
##com.oracle.truffle.r.test.builtins.TestBuiltin_shortRowNames.testArgCasts#
76412+
#.Internal(shortRowNames(NULL, '1'))
76413+
[1] 0
76414+
76415+
##com.oracle.truffle.r.test.builtins.TestBuiltin_shortRowNames.testArgCasts#
76416+
#.Internal(shortRowNames(NULL, 0))
76417+
NULL
76418+
76419+
##com.oracle.truffle.r.test.builtins.TestBuiltin_shortRowNames.testArgCasts#
76420+
#.Internal(shortRowNames(NULL, 1))
76421+
[1] 0
76422+
76423+
##com.oracle.truffle.r.test.builtins.TestBuiltin_shortRowNames.testArgCasts#
76424+
#.Internal(shortRowNames(NULL, 2))
76425+
[1] 0
76426+
7635476427
##com.oracle.truffle.r.test.builtins.TestBuiltin_shortRowNames.testshortRowNames1#
7635576428
#argv <- list(structure(list(c(8.44399377410362, 28.4640218366572, 12.2441566485997)), row.names = c(NA, -3L), class = 'data.frame'), 1L); .Internal(shortRowNames(argv[[1]], argv[[2]]))
7635676429
[1] -3
@@ -170291,7 +170364,7 @@ attr(,"assign")
170291170364
attr(,"assign")
170292170365
[1] 0 1
170293170366

170294-
##com.oracle.truffle.r.test.library.stats.TestFormulae.testModelMatrix#Ignored.NewRVersionMigration#
170367+
##com.oracle.truffle.r.test.library.stats.TestFormulae.testModelMatrix#
170295170368
#{y<-0:9;z<-1:10;k<-2:11;w<-3:12;m<-4:13;u<-5:14;v<-6:15;; model.matrix(model.frame(terms.formula(~1))) }
170296170369
(Intercept)
170297170370
attr(,"assign")
@@ -170627,7 +170700,7 @@ attr(,"contrasts")$z
170627170700
[1] "contr.treatment"
170628170701

170629170702

170630-
##com.oracle.truffle.r.test.library.stats.TestFormulae.testModelMatrix#Ignored.NewRVersionMigration#
170703+
##com.oracle.truffle.r.test.library.stats.TestFormulae.testModelMatrix#
170631170704
#{y<-0:9;z<-1:10;k<-2:11;w<-3:12;m<-4:13;u<-5:14;v<-6:15;k <- factor(rep(c('m', 'f'), 5));z <- factor(c(rep(c('a', 'b', 'c'), 3), 'c')); ; model.matrix(model.frame(terms.formula(~1))) }
170632170705
(Intercept)
170633170706
attr(,"assign")

com.oracle.truffle.r.test/src/com/oracle/truffle/r/test/builtins/TestBuiltin_listdirs.java

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
* Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
1515
*
1616
* Copyright (c) 2014, Purdue University
17-
* Copyright (c) 2014, 2018, Oracle and/or its affiliates
17+
* Copyright (c) 2014, 2019, Oracle and/or its affiliates
1818
*
1919
* All rights reserved.
2020
*/
@@ -27,8 +27,21 @@
2727
// Checkstyle: stop line length check
2828
public class TestBuiltin_listdirs extends TestBase {
2929

30+
private static String dirPath0 = "com.oracle.truffle.r.test/src/com/oracle/truffle/r/test/simple";
31+
private static String dirPath1 = "com.oracle.truffle.r.test/src/com/oracle/truffle/r/test/simple/data";
32+
3033
@Test
3134
public void testlistdirs1() {
32-
assertEval(Ignored.SideEffects, "argv <- list('/home/lzhao/hg/r-instrumented/library/rpart/doc', TRUE, FALSE); .Internal(list.dirs(argv[[1]], argv[[2]], argv[[3]]))");
35+
assertEval("list.dirs('" + dirPath1 + "', full.names=T, recursive=T)");
36+
assertEval("list.dirs('" + dirPath1 + "', full.names=T, recursive=F)");
37+
assertEval("list.dirs('" + dirPath1 + "', full.names=F, recursive=T)");
38+
assertEval("list.dirs('" + dirPath1 + "', full.names=F, recursive=F)");
39+
40+
assertEval("wd <- getwd(); setwd('" + dirPath0 + "'); list.dirs('data', full.names=T, recursive=T); setwd(wd)");
41+
assertEval("wd <- getwd(); setwd('" + dirPath0 + "'); list.dirs('data', full.names=T, recursive=F); setwd(wd)");
42+
assertEval("wd <- getwd(); setwd('" + dirPath0 + "'); list.dirs('data', full.names=F, recursive=T); setwd(wd)");
43+
assertEval("wd <- getwd(); setwd('" + dirPath0 + "'); list.dirs('data', full.names=F, recursive=F); setwd(wd)");
44+
45+
assertEval("list.dirs('does-not-exist', full.names=F, recursive=F)");
3346
}
3447
}
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
/*
2+
* Copyright (c) 2016, 2019, Oracle and/or its affiliates. All rights reserved.
3+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4+
*
5+
* This code is free software; you can redistribute it and/or modify it
6+
* under the terms of the GNU General Public License version 3 only, as
7+
* published by the Free Software Foundation.
8+
*
9+
* This code is distributed in the hope that it will be useful, but WITHOUT
10+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12+
* version 3 for more details (a copy is included in the LICENSE file that
13+
* accompanied this code).
14+
*
15+
* You should have received a copy of the GNU General Public License version
16+
* 3 along with this work; if not, write to the Free Software Foundation,
17+
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18+
*
19+
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20+
* or visit www.oracle.com if you need additional information or have any
21+
* questions.
22+
*/
23+
24+
package com.oracle.truffle.r.test.builtins;
25+
26+
import com.oracle.truffle.r.runtime.Utils;
27+
import org.junit.Test;
28+
29+
import com.oracle.truffle.r.test.TestBase;
30+
import java.io.File;
31+
import java.io.IOException;
32+
import org.junit.After;
33+
import static org.junit.Assert.assertTrue;
34+
35+
public class TestBuiltin_setwd extends TestBase {
36+
37+
@After
38+
public void resetWD() {
39+
Utils.resetWD();
40+
}
41+
42+
private static final String testWDDir = "com.oracle.truffle.r.test/src/com/oracle/truffle/r/test/builtins/data/";
43+
44+
@Test
45+
public void testSetWDvsUserDir() throws IOException {
46+
String wd = gewtWD();
47+
48+
// 1.) current java user.dir is {fastr_home} and it contains a file README.md
49+
assertTrue("no README.md file in the {fastr_home}, try to use some another file for this test then.", new File(wd + "README.md").exists());
50+
// 2.) lets create a test folder containg a directory with the same name - README.md
51+
new File(wd + "com.oracle.truffle.r.test/src/com/oracle/truffle/r/test/builtins/data/README.md").mkdirs();
52+
File testFile = new File(wd + "com.oracle.truffle.r.test/src/com/oracle/truffle/r/test/builtins/data/README.md/test");
53+
if (!testFile.exists()) {
54+
testFile.createNewFile();
55+
}
56+
// 3.) and setwd to {testdir} and try to access that README.md/test
57+
assertEval("setwd('" + wd + testWDDir + "'); .Call(tools:::C_Rmd5, 'README.md/test'); setwd('" + wd + "')");
58+
59+
// 1.) current java user.dir is {fastr_home} and it contains a dir 'bin' containig a file R
60+
assertTrue("no bin/R file in the {fastr_home}, try ot use some another file for this test then.", new File(wd + "bin/R").exists());
61+
// 2.) lets create a test folder containg a path 'bin/R/test';
62+
// note that {fastr_home}/bin/R points to a file, while {testdir}/bin/R is a directory
63+
new File(wd + "com.oracle.truffle.r.test/src/com/oracle/truffle/r/test/builtins/data/bin/R").mkdirs();
64+
testFile = new File(wd + "com.oracle.truffle.r.test/src/com/oracle/truffle/r/test/builtins/data/bin/R/test");
65+
if (!testFile.exists()) {
66+
testFile.createNewFile();
67+
}
68+
// 3.) and setwd to {testdir} and try to access that bin/R/test
69+
assertEval("setwd('" + wd + testWDDir + "'); .Call(tools:::C_Rmd5, 'bin/R/test'); setwd('" + wd + "')");
70+
}
71+
72+
private static String dirPathWD = "com.oracle.truffle.r.test/src/com/oracle/truffle/r/test/simple/data";
73+
74+
@Test
75+
public void testSetWD() {
76+
String wd = gewtWD();
77+
78+
assertEval("{ setwd(); getwd(); }");
79+
assertEval("{ setwd(''); getwd(); }");
80+
81+
assertEval(Ignored.ImplementationError, "{ setwd('" + wd + dirPathWD + "'); setwd(''); getwd(); setwd('" + wd + "') }");
82+
83+
assertEval("{ setwd('" + wd + dirPathWD + "'); getwd(); setwd('" + wd + "') }");
84+
assertEval("{ setwd('" + wd + dirPathWD + "'); setwd('tree1'); getwd(); setwd('" + wd + "') }");
85+
assertEval("{ setwd('" + wd + dirPathWD + "'); setwd('does-not-exist'); getwd(); setwd('" + wd + "') }");
86+
assertEval("{ setwd('" + wd + dirPathWD + "/tree1/..'); getwd(); setwd('" + wd + "') }");
87+
}
88+
89+
private static String gewtWD() {
90+
return System.getProperty("user.dir") + "/";
91+
}
92+
93+
}

0 commit comments

Comments
 (0)