| 1 | package net.minecraft.util;
|
| 2 |
|
| 3 | import com.mojang.serialization.DataResult;
|
| 4 | import java.io.File;
|
| 5 | import java.io.IOException;
|
| 6 | import java.nio.file.FileAlreadyExistsException;
|
| 7 | import java.nio.file.Files;
|
| 8 | import java.nio.file.Path;
|
| 9 | import java.util.ArrayList;
|
| 10 | import java.util.Arrays;
|
| 11 | import java.util.List;
|
| 12 | import java.util.regex.Matcher;
|
| 13 | import java.util.regex.Pattern;
|
| 14 | import net.minecraft.SharedConstants;
|
| 15 | import org.apache.commons.io.FilenameUtils;
|
| 16 |
|
| 17 | public class FileUtil {
|
| 18 | private static final Pattern COPY_COUNTER_PATTERN = Pattern.compile("(<name>.*) \\((<count>\\d*)\\)", 66);
|
| 19 | private static final int MAX_FILE_NAME = 255;
|
| 20 | private static final Pattern RESERVED_WINDOWS_FILENAMES = Pattern.compile(".*\\.|(?:COM|CLOCK\\$|CON|PRN|AUX|NUL|COM[1-9]|LPT[1-9])(?:\\..*)?", 2);
|
| 21 | private static final Pattern STRICT_PATH_SEGMENT_CHECK = Pattern.compile("[-._a-z0-9]+");
|
| 22 |
|
| 23 | public static String sanitizeName(String baseName) {
|
| 24 | for (char replacer : SharedConstants.ILLEGAL_FILE_CHARACTERS) {
|
| 25 | baseName = baseName.replace(replacer, '_');
|
| 26 | }
|
| 27 |
|
| 28 | return baseName.replaceAll("[./\"]", "_");
|
| 29 | }
|
| 30 |
|
| 31 | public static String findAvailableName(final Path baseDir, String baseName, final String suffix) throws IOException {
|
| 32 | baseName = sanitizeName(baseName);
|
| 33 | if (!isPathPartPortable(baseName)) {
|
| 34 | baseName = "_" + baseName + "_";
|
| 35 | }
|
| 36 |
|
| 37 | Matcher matcher = COPY_COUNTER_PATTERN.matcher(baseName);
|
| 38 | int count = 0;
|
| 39 | if (matcher.matches()) {
|
| 40 | baseName = matcher.group("name");
|
| 41 | count = Integer.parseInt(matcher.group("count"));
|
| 42 | }
|
| 43 |
|
| 44 | if (baseName.length() > 255 - suffix.length()) {
|
| 45 | baseName = baseName.substring(0, 255 - suffix.length());
|
| 46 | }
|
| 47 |
|
| 48 | while (true) {
|
| 49 | String nameToTest = baseName;
|
| 50 | if (count != 0) {
|
| 51 | String countSuffix = " (" + count + ")";
|
| 52 | int length = 255 - countSuffix.length();
|
| 53 | if (nameToTest.length() > length) {
|
| 54 | nameToTest = nameToTest.substring(0, length);
|
| 55 | }
|
| 56 |
|
| 57 | nameToTest = nameToTest + countSuffix;
|
| 58 | }
|
| 59 |
|
| 60 | nameToTest = nameToTest + suffix;
|
| 61 | Path fullPath = baseDir.resolve(nameToTest);
|
| 62 |
|
| 63 | try {
|
| 64 | Path created = Files.createDirectory(fullPath);
|
| 65 | Files.deleteIfExists(created);
|
| 66 | return baseDir.relativize(created).toString();
|
| 67 | } catch (FileAlreadyExistsException e) {
|
| 68 | count++;
|
| 69 | }
|
| 70 | }
|
| 71 | }
|
| 72 |
|
| 73 | public static boolean isPathPortable(final Path path) {
|
| 74 | for (Path part : path) {
|
| 75 | if (!isPathPartPortable(part.toString())) {
|
| 76 | return false;
|
| 77 | }
|
| 78 | }
|
| 79 |
|
| 80 | return true;
|
| 81 | }
|
| 82 |
|
| 83 | public static boolean isPathPartPortable(final String name) {
|
| 84 | return !RESERVED_WINDOWS_FILENAMES.matcher(name).matches();
|
| 85 | }
|
| 86 |
|
| 87 | public static String getFullResourcePath(final String filename) {
|
| 88 | return FilenameUtils.getFullPath(filename).replace(File.separator, "/");
|
| 89 | }
|
| 90 |
|
| 91 | public static String normalizeResourcePath(final String filename) {
|
| 92 | return FilenameUtils.normalize(filename).replace(File.separator, "/");
|
| 93 | }
|
| 94 |
|
| 95 | public static DataResult<List<String>> decomposePath(final String path) {
|
| 96 | int segmentEnd = path.indexOf(47);
|
| 97 | if (segmentEnd == -1) {
|
| 98 | return switch (path) {
|
| 99 | case "", ".", ".." -> DataResult.error(() -> "Invalid path '" + path + "'");
|
| 100 | default -> !containsAllowedCharactersOnly(path) ? DataResult.error(() -> "Invalid path '" + path + "'") : DataResult.success(List.of(path));
|
| 101 | };
|
| 102 | } else {
|
| 103 | List<String> result = new ArrayList<>();
|
| 104 | int segmentStart = 0;
|
| 105 | boolean lastSegment = false;
|
| 106 |
|
| 107 | while (true) {
|
| 108 | String segment = path.substring(segmentStart, segmentEnd);
|
| 109 | switch (segment) {
|
| 110 | case "":
|
| 111 | case ".":
|
| 112 | case "..":
|
| 113 | return DataResult.error(() -> "Invalid segment '" + segment + "' in path '" + path + "'");
|
| 114 | }
|
| 115 |
|
| 116 | if (!containsAllowedCharactersOnly(segment)) {
|
| 117 | return DataResult.error(() -> "Invalid segment '" + segment + "' in path '" + path + "'");
|
| 118 | }
|
| 119 |
|
| 120 | result.add(segment);
|
| 121 | if (lastSegment) {
|
| 122 | return DataResult.success(result);
|
| 123 | }
|
| 124 |
|
| 125 | segmentStart = segmentEnd + 1;
|
| 126 | segmentEnd = path.indexOf(47, segmentStart);
|
| 127 | if (segmentEnd == -1) {
|
| 128 | segmentEnd = path.length();
|
| 129 | lastSegment = true;
|
| 130 | }
|
| 131 | }
|
| 132 | }
|
| 133 | }
|
| 134 |
|
| 135 | public static Path resolvePath(final Path root, final List<String> segments) {
|
| 136 | int size = segments.size();
|
| 137 |
|
| 138 | return switch (size) {
|
| 139 | case 0 -> root;
|
| 140 | case 1 -> root.resolve(segments.get(0));
|
| 141 | default -> {
|
| 142 | String[] rest = new String[size - 1];
|
| 143 |
|
| 144 | for (int i = 1; i < size; i++) {
|
| 145 | rest[i - 1] = segments.get(i);
|
| 146 | }
|
| 147 |
|
| 148 | yield root.resolve(root.getFileSystem().getPath(segments.get(0), rest));
|
| 149 | }
|
| 150 | };
|
| 151 | }
|
| 152 |
|
| 153 | private static boolean containsAllowedCharactersOnly(final String segment) {
|
| 154 | return STRICT_PATH_SEGMENT_CHECK.matcher(segment).matches();
|
| 155 | }
|
| 156 |
|
| 157 | public static boolean isValidPathSegment(final String segment) {
|
| 158 | return !segment.equals("..") && !segment.equals(".") && containsAllowedCharactersOnly(segment);
|
| 159 | }
|
| 160 |
|
| 161 | public static void validatePath(final String... path) {
|
| 162 | if (path.length == 0) {
|
| 163 | throw new IllegalArgumentException("Path must have at least one element");
|
| 164 | }
|
| 165 |
|
| 166 | for (String segment : path) {
|
| 167 | if (!isValidPathSegment(segment)) {
|
| 168 | throw new IllegalArgumentException("Illegal segment " + segment + " in path " + Arrays.toString(path));
|
| 169 | }
|
| 170 | }
|
| 171 | }
|
| 172 |
|
| 173 | public static void createDirectoriesSafe(final Path dir) throws IOException {
|
| 174 | Files.createDirectories(Files.exists(dir) ? dir.toRealPath() : dir);
|
| 175 | }
|
| 176 |
|
| 177 | public static boolean isEmptyPath(final Path path) {
|
| 178 | return path.getNameCount() == 1 && path.getFileName().toString().isEmpty();
|
| 179 | }
|
| 180 | }
|
| 181 |
|