78
78
import java .util .ArrayList ;
79
79
import java .util .Arrays ;
80
80
import java .util .Collection ;
81
+ import java .util .HashSet ;
81
82
import java .util .List ;
83
+ import java .util .Set ;
82
84
import java .util .logging .Level ;
83
85
84
86
import org .graalvm .nativeimage .ImageInfo ;
@@ -1021,15 +1023,19 @@ Object list(TruffleString dirPath, TruffleString filesListPath) {
1021
1023
}
1022
1024
1023
1025
try (BufferedWriter bw = new BufferedWriter (new OutputStreamWriter (new FileOutputStream (filesListPath .toJavaStringUncached ())))) {
1024
- getContext ().getPublicTruffleFileRelaxed (filesListPath ).getParent ().createDirectories ();
1025
- List <String > ret = list (dir );
1026
- String parentPathString = dir .getParent ().getAbsoluteFile ().getPath ();
1027
- for (String f : ret ) {
1028
- String tt = f .substring (parentPathString .length ());
1029
- if (tt .charAt (0 ) == '\\' ) {
1030
- tt = tt .replace ("\\ " , "/" );
1026
+ TruffleFile p = getContext ().getPublicTruffleFileRelaxed (filesListPath ).getParent ();
1027
+ if (!p .exists ()) {
1028
+ getContext ().getPublicTruffleFileRelaxed (filesListPath ).getParent ().createDirectories ();
1029
+ }
1030
+ Set <String > ret = list (dir , null );
1031
+ String [] a = ret .toArray (new String [ret .size ()]);
1032
+ Arrays .sort (a );
1033
+ for (String f : a ) {
1034
+ if (f .charAt (0 ) == '\\' ) {
1035
+ f = f .replace ("\\ " , "/" );
1031
1036
}
1032
- bw .write (tt );
1037
+
1038
+ bw .write (f );
1033
1039
bw .write ("\n " );
1034
1040
}
1035
1041
} catch (IOException e ) {
@@ -1039,26 +1045,46 @@ Object list(TruffleString dirPath, TruffleString filesListPath) {
1039
1045
return PNone .NONE ;
1040
1046
}
1041
1047
1042
- private static List <String > list (TruffleFile dir ) throws IOException {
1043
- List <String > ret = new ArrayList <>();
1048
+ private static Set <String > list (TruffleFile dir , TruffleFile rd ) throws IOException {
1049
+ HashSet <String > ret = new HashSet <>();
1044
1050
Collection <TruffleFile > files = dir .list ();
1045
- String dirPath = dir .getAbsoluteFile ().getPath ();
1046
- if (!dirPath .endsWith ("/" )) {
1047
- dirPath = dirPath + "/" ;
1051
+ String dirPath = makeDirPath (dir .getAbsoluteFile ().getPath ());
1052
+
1053
+ // add dir
1054
+ TruffleFile rootDir = rd == null ? dir : rd ;
1055
+ String rootPath = makeDirPath (rootDir .getAbsoluteFile ().getPath ());
1056
+ int rootEndIdx = rootPath .lastIndexOf ("/" , rootPath .lastIndexOf ("/" ) - 1 );
1057
+ ret .add (dirPath .substring (rootEndIdx ));
1058
+
1059
+ // add parents up to root
1060
+ TruffleFile parent = dir ;
1061
+ while (!parent .equals (rootDir )) {
1062
+ String p = makeDirPath (parent .getAbsoluteFile ().getPath ());
1063
+ p = p .substring (rootEndIdx );
1064
+ ret .add (p );
1065
+ parent = parent .getParent ();
1048
1066
}
1049
- ret .add (dirPath );
1067
+
1068
+ // add children
1050
1069
if (files != null ) {
1051
1070
for (TruffleFile f : files ) {
1052
1071
if (f .isRegularFile ()) {
1053
- ret .add (f .getAbsoluteFile ().getPath ());
1072
+ ret .add (f .getAbsoluteFile ().getPath (). substring ( rootEndIdx ) );
1054
1073
} else {
1055
- ret .addAll (list (f ));
1074
+ ret .addAll (list (f , rootDir ));
1056
1075
}
1057
1076
}
1058
1077
}
1059
1078
return ret ;
1060
1079
}
1061
1080
1081
+ private static String makeDirPath (String p ) {
1082
+ if (!p .endsWith ("/" )) {
1083
+ p = p + "/" ;
1084
+ }
1085
+ return p ;
1086
+ }
1087
+
1062
1088
private void print (OutputStream out , String msg ) {
1063
1089
try {
1064
1090
out .write (String .format ("%s: %s" , getContext ().getOption (PythonOptions .Executable ), msg ).getBytes (StandardCharsets .UTF_8 ));
0 commit comments