Skip to content

Commit

Permalink
[GR-19346] Backport: hotfix FastR working.dir vs java user.dir issues.
Browse files Browse the repository at this point in the history
PullRequest: fastr/2223
  • Loading branch information
steve-s committed Nov 8, 2019
2 parents d93857d + 51facf1 commit dd6db7f
Show file tree
Hide file tree
Showing 7 changed files with 239 additions and 46 deletions.
2 changes: 1 addition & 1 deletion ci_common/common.hocon
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
java7 : {name : oraclejdk, version : "7", platformspecific: true}
# java 8 must be a jvmci enabled variant
java8 : {name : oraclejdk, version : "8u231-jvmci-19.3-b04", platformspecific: true}
java11 : {name : labsjdk, version : "ce-11.0.5+9-jvmci-19.3-b03", platformspecific: true}
java11 : {name : labsjdk, version : "ce-11.0.5+10-jvmci-19.3-b04", platformspecific: true}

java8Downloads : {
downloads : {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -824,6 +824,9 @@ protected RStringVector listDirs(RAbstractStringVector paths, boolean fullNames,
}
while (iter.hasNext()) {
TruffleFile dir = iter.next();
if (!recursive && dir.equals(root)) {
continue;
}
if (!fullNames) {
dir = vecPath.relativize(dir);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,6 @@
import com.oracle.truffle.api.TruffleFile;
import com.oracle.truffle.api.TruffleLanguage.Env;
import com.oracle.truffle.r.runtime.RError.Message;
import java.nio.file.NoSuchFileException;
import java.util.logging.Level;

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

public static TruffleFile getSafeTruffleFile(Env env, String path) {
TruffleFile origFile = env.getInternalTruffleFile(path);

TruffleFile f = origFile;
try {
origFile = env.getInternalTruffleFile(path);
if (origFile.exists()) {
try {
f = origFile.getCanonicalFile();
} catch (NoSuchFileException e) {
// absolute path "exists", but cannonical does not
// happens e.g. during install.packages: file.exists("/{rHome}/inst/..")
// lets optimistically FALLBACK on absolute path
}
}
} catch (IOException e) {
RLogger.getLogger(RLogger.LOGGER_FILE_ACCEESS).log(Level.SEVERE, "Unable to access file " + path + " " + e.getMessage(), e);
throw RError.error(RError.SHOW_CALLER, Message.FILE_OPEN_ERROR);
}

final TruffleFile home = REnvVars.getRHomeTruffleFile(env);
if (f.startsWith(home) && isLibraryFile(home.relativize(f))) {
return origFile;
} else {
try {
return env.getPublicTruffleFile(path);
} catch (SecurityException e) {
RLogger.getLogger(RLogger.LOGGER_FILE_ACCEESS).log(Level.SEVERE, "Unable to access file " + path + " " + e.getMessage(), e);
throw RError.error(RError.SHOW_CALLER, Message.FILE_OPEN_ERROR);
}
}
// TODO: HOTFIX the commented in code bellow does not work well with the FastR
// 'working.dir' vs java 'user.dir' dichotomy
return env.getInternalTruffleFile(path);

// TruffleFile origFile = env.getInternalTruffleFile(path);
//
// TruffleFile f = origFile;
// try {
// origFile = env.getInternalTruffleFile(path);
// if (origFile.exists()) {
// try {
// f = origFile.getCanonicalFile();
// } catch (NoSuchFileException e) {
// // absolute path "exists", but cannonical does not
// // happens e.g. during install.packages: file.exists("/{rHome}/inst/..")
// // lets optimistically FALLBACK on absolute path
// }
// }
// } catch (IOException e) {
// RLogger.getLogger(RLogger.LOGGER_FILE_ACCEESS).log(Level.SEVERE, "Unable to access file "
// + path + " " + e.getMessage(), e);
// throw RError.error(RError.SHOW_CALLER, Message.FILE_OPEN_ERROR);
// }
//
// final TruffleFile home = REnvVars.getRHomeTruffleFile(env);
// if (f.startsWith(home) && isLibraryFile(home.relativize(f))) {
// return origFile;
// } else {
// try {
// return env.getPublicTruffleFile(path);
// } catch (SecurityException e) {
// RLogger.getLogger(RLogger.LOGGER_FILE_ACCEESS).log(Level.SEVERE, "Unable to access file "
// + path + " " + e.getMessage(), e);
// throw RError.error(RError.SHOW_CALLER, Message.FILE_OPEN_ERROR);
// }
// }
}

private static boolean isLibraryFile(TruffleFile relativePathFromHome) {
final String fileName = relativePathFromHome.getName();
if (fileName == null) {
return false;
}
return relativePathFromHome.startsWith("library");
}
// private static boolean isLibraryFile(TruffleFile relativePathFromHome) {
// final String fileName = relativePathFromHome.getName();
// if (fileName == null) {
// return false;
// }
// return relativePathFromHome.startsWith("library");
// }

}
Original file line number Diff line number Diff line change
Expand Up @@ -914,4 +914,11 @@ public static String wildcardToRegex(String wildcard) {
return (s.toString());
}

/**
* Testing purposes only.
*/
public static void resetWD() {
wdState().current = wdState().initial;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -33933,6 +33933,10 @@ integer(0)
#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]]))
[1] 1

##com.oracle.truffle.r.test.builtins.TestBuiltin_grepRaw.testFixedEmpty#
#grepRaw('adf', '', fixed=TRUE)
integer(0)

##com.oracle.truffle.r.test.builtins.TestBuiltin_grepRaw.testFixedIgnoreInvert#
#grepRaw('no match', 'XXXXXXXX', fixed=T, invert=T, value=F)
integer(0)
Expand Down Expand Up @@ -41993,10 +41997,47 @@ attr(,"class")
NULL


##com.oracle.truffle.r.test.builtins.TestBuiltin_listdirs.testlistdirs1#Ignored.SideEffects#
#argv <- list('/home/lzhao/hg/r-instrumented/library/rpart/doc', TRUE, FALSE); .Internal(list.dirs(argv[[1]], argv[[2]], argv[[3]]))
##com.oracle.truffle.r.test.builtins.TestBuiltin_listdirs.testlistdirs1#
#list.dirs('com.oracle.truffle.r.test/src/com/oracle/truffle/r/test/simple/data', full.names=F, recursive=F)
[1] "tree1" "tree2"

##com.oracle.truffle.r.test.builtins.TestBuiltin_listdirs.testlistdirs1#
#list.dirs('com.oracle.truffle.r.test/src/com/oracle/truffle/r/test/simple/data', full.names=F, recursive=T)
[1] "" "tree1" "tree1/subdir" "tree2"

##com.oracle.truffle.r.test.builtins.TestBuiltin_listdirs.testlistdirs1#
#list.dirs('com.oracle.truffle.r.test/src/com/oracle/truffle/r/test/simple/data', full.names=T, recursive=F)
[1] "com.oracle.truffle.r.test/src/com/oracle/truffle/r/test/simple/data/tree1"
[2] "com.oracle.truffle.r.test/src/com/oracle/truffle/r/test/simple/data/tree2"

##com.oracle.truffle.r.test.builtins.TestBuiltin_listdirs.testlistdirs1#
#list.dirs('com.oracle.truffle.r.test/src/com/oracle/truffle/r/test/simple/data', full.names=T, recursive=T)
[1] "com.oracle.truffle.r.test/src/com/oracle/truffle/r/test/simple/data"
[2] "com.oracle.truffle.r.test/src/com/oracle/truffle/r/test/simple/data/tree1"
[3] "com.oracle.truffle.r.test/src/com/oracle/truffle/r/test/simple/data/tree1/subdir"
[4] "com.oracle.truffle.r.test/src/com/oracle/truffle/r/test/simple/data/tree2"

##com.oracle.truffle.r.test.builtins.TestBuiltin_listdirs.testlistdirs1#
#list.dirs('does-not-exist', full.names=F, recursive=F)
character(0)

##com.oracle.truffle.r.test.builtins.TestBuiltin_listdirs.testlistdirs1#
#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)
[1] "tree1" "tree2"

##com.oracle.truffle.r.test.builtins.TestBuiltin_listdirs.testlistdirs1#
#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)
[1] "" "tree1" "tree1/subdir" "tree2"

##com.oracle.truffle.r.test.builtins.TestBuiltin_listdirs.testlistdirs1#
#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)
[1] "data/tree1" "data/tree2"

##com.oracle.truffle.r.test.builtins.TestBuiltin_listdirs.testlistdirs1#
#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)
[1] "data" "data/tree1" "data/tree1/subdir"
[4] "data/tree2"

##com.oracle.truffle.r.test.builtins.TestBuiltin_listfiles.testEmptyDir#
#{ list.files("com.oracle.truffle.r.test/src/com/oracle/truffle/r/test/simple/empty_dir", all.files=TRUE, recursive=TRUE, no..=FALSE) }
character(0)
Expand Down Expand Up @@ -76351,6 +76392,38 @@ In set.seed("hello world") : NAs introduced by coercion
#.Internal(shortRowNames(42, -2))
Error: invalid 'type' argument

##com.oracle.truffle.r.test.builtins.TestBuiltin_shortRowNames.testArgCasts#
#.Internal(shortRowNames(42, 0))
NULL

##com.oracle.truffle.r.test.builtins.TestBuiltin_shortRowNames.testArgCasts#
#.Internal(shortRowNames(42, 1))
[1] 0

##com.oracle.truffle.r.test.builtins.TestBuiltin_shortRowNames.testArgCasts#
#.Internal(shortRowNames(42, 2))
[1] 0

##com.oracle.truffle.r.test.builtins.TestBuiltin_shortRowNames.testArgCasts#
#.Internal(shortRowNames(NULL, '0'))
NULL

##com.oracle.truffle.r.test.builtins.TestBuiltin_shortRowNames.testArgCasts#
#.Internal(shortRowNames(NULL, '1'))
[1] 0

##com.oracle.truffle.r.test.builtins.TestBuiltin_shortRowNames.testArgCasts#
#.Internal(shortRowNames(NULL, 0))
NULL

##com.oracle.truffle.r.test.builtins.TestBuiltin_shortRowNames.testArgCasts#
#.Internal(shortRowNames(NULL, 1))
[1] 0

##com.oracle.truffle.r.test.builtins.TestBuiltin_shortRowNames.testArgCasts#
#.Internal(shortRowNames(NULL, 2))
[1] 0

##com.oracle.truffle.r.test.builtins.TestBuiltin_shortRowNames.testshortRowNames1#
#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]]))
[1] -3
Expand Down Expand Up @@ -170291,7 +170364,7 @@ attr(,"assign")
attr(,"assign")
[1] 0 1

##com.oracle.truffle.r.test.library.stats.TestFormulae.testModelMatrix#Ignored.NewRVersionMigration#
##com.oracle.truffle.r.test.library.stats.TestFormulae.testModelMatrix#
#{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))) }
(Intercept)
attr(,"assign")
Expand Down Expand Up @@ -170627,7 +170700,7 @@ attr(,"contrasts")$z
[1] "contr.treatment"


##com.oracle.truffle.r.test.library.stats.TestFormulae.testModelMatrix#Ignored.NewRVersionMigration#
##com.oracle.truffle.r.test.library.stats.TestFormulae.testModelMatrix#
#{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))) }
(Intercept)
attr(,"assign")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* Copyright (c) 2014, Purdue University
* Copyright (c) 2014, 2018, Oracle and/or its affiliates
* Copyright (c) 2014, 2019, Oracle and/or its affiliates
*
* All rights reserved.
*/
Expand All @@ -27,8 +27,21 @@
// Checkstyle: stop line length check
public class TestBuiltin_listdirs extends TestBase {

private static String dirPath0 = "com.oracle.truffle.r.test/src/com/oracle/truffle/r/test/simple";
private static String dirPath1 = "com.oracle.truffle.r.test/src/com/oracle/truffle/r/test/simple/data";

@Test
public void testlistdirs1() {
assertEval(Ignored.SideEffects, "argv <- list('/home/lzhao/hg/r-instrumented/library/rpart/doc', TRUE, FALSE); .Internal(list.dirs(argv[[1]], argv[[2]], argv[[3]]))");
assertEval("list.dirs('" + dirPath1 + "', full.names=T, recursive=T)");
assertEval("list.dirs('" + dirPath1 + "', full.names=T, recursive=F)");
assertEval("list.dirs('" + dirPath1 + "', full.names=F, recursive=T)");
assertEval("list.dirs('" + dirPath1 + "', full.names=F, recursive=F)");

assertEval("wd <- getwd(); setwd('" + dirPath0 + "'); list.dirs('data', full.names=T, recursive=T); setwd(wd)");
assertEval("wd <- getwd(); setwd('" + dirPath0 + "'); list.dirs('data', full.names=T, recursive=F); setwd(wd)");
assertEval("wd <- getwd(); setwd('" + dirPath0 + "'); list.dirs('data', full.names=F, recursive=T); setwd(wd)");
assertEval("wd <- getwd(); setwd('" + dirPath0 + "'); list.dirs('data', full.names=F, recursive=F); setwd(wd)");

assertEval("list.dirs('does-not-exist', full.names=F, recursive=F)");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
/*
* Copyright (c) 2016, 2019, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 3 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 3 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 3 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/

package com.oracle.truffle.r.test.builtins;

import com.oracle.truffle.r.runtime.Utils;
import org.junit.Test;

import com.oracle.truffle.r.test.TestBase;
import java.io.File;
import java.io.IOException;
import org.junit.After;
import static org.junit.Assert.assertTrue;

public class TestBuiltin_setwd extends TestBase {

@After
public void resetWD() {
Utils.resetWD();
}

private static final String testWDDir = "com.oracle.truffle.r.test/src/com/oracle/truffle/r/test/builtins/data/";

@Test
public void testSetWDvsUserDir() throws IOException {
String wd = gewtWD();

// 1.) current java user.dir is {fastr_home} and it contains a file README.md
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());
// 2.) lets create a test folder containg a directory with the same name - README.md
new File(wd + "com.oracle.truffle.r.test/src/com/oracle/truffle/r/test/builtins/data/README.md").mkdirs();
File testFile = new File(wd + "com.oracle.truffle.r.test/src/com/oracle/truffle/r/test/builtins/data/README.md/test");
if (!testFile.exists()) {
testFile.createNewFile();
}
// 3.) and setwd to {testdir} and try to access that README.md/test
assertEval("setwd('" + wd + testWDDir + "'); .Call(tools:::C_Rmd5, 'README.md/test'); setwd('" + wd + "')");

// 1.) current java user.dir is {fastr_home} and it contains a dir 'bin' containig a file R
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());
// 2.) lets create a test folder containg a path 'bin/R/test';
// note that {fastr_home}/bin/R points to a file, while {testdir}/bin/R is a directory
new File(wd + "com.oracle.truffle.r.test/src/com/oracle/truffle/r/test/builtins/data/bin/R").mkdirs();
testFile = new File(wd + "com.oracle.truffle.r.test/src/com/oracle/truffle/r/test/builtins/data/bin/R/test");
if (!testFile.exists()) {
testFile.createNewFile();
}
// 3.) and setwd to {testdir} and try to access that bin/R/test
assertEval("setwd('" + wd + testWDDir + "'); .Call(tools:::C_Rmd5, 'bin/R/test'); setwd('" + wd + "')");
}

private static String dirPathWD = "com.oracle.truffle.r.test/src/com/oracle/truffle/r/test/simple/data";

@Test
public void testSetWD() {
String wd = gewtWD();

assertEval("{ setwd(); getwd(); }");
assertEval("{ setwd(''); getwd(); }");

assertEval(Ignored.ImplementationError, "{ setwd('" + wd + dirPathWD + "'); setwd(''); getwd(); setwd('" + wd + "') }");

assertEval("{ setwd('" + wd + dirPathWD + "'); getwd(); setwd('" + wd + "') }");
assertEval("{ setwd('" + wd + dirPathWD + "'); setwd('tree1'); getwd(); setwd('" + wd + "') }");
assertEval("{ setwd('" + wd + dirPathWD + "'); setwd('does-not-exist'); getwd(); setwd('" + wd + "') }");
assertEval("{ setwd('" + wd + dirPathWD + "/tree1/..'); getwd(); setwd('" + wd + "') }");
}

private static String gewtWD() {
return System.getProperty("user.dir") + "/";
}

}

0 comments on commit dd6db7f

Please sign in to comment.