< prev index next >

src/jdk.internal.vm.compiler/share/classes/org.graalvm.compiler.debug/src/org/graalvm/compiler/debug/PathUtilities.java

Print this page




  67                     Paths.get(String.valueOf(c));
  68                     buf.append(c);
  69                     continue;
  70                 } catch (InvalidPathException e) {
  71                 }
  72             }
  73             buf.append('_');
  74         }
  75         return buf.toString();
  76     }
  77 
  78     /**
  79      * A maximum file name length supported by most file systems. There is no platform independent
  80      * way to get this in Java. Normally it is 255. But for AUFS it is 242. Refer AUFS_MAX_NAMELEN
  81      * in http://aufs.sourceforge.net/aufs3/man.html.
  82      */
  83     private static final int MAX_FILE_NAME_LENGTH = 242;
  84 
  85     private static final String ELLIPSIS = "...";
  86 
  87     static Path createUnique(OptionValues options, OptionKey<String> baseNameOption, String id, String label, String ext, boolean createDirectory) throws IOException {
  88         String uniqueTag = "";
  89         int dumpCounter = 1;
  90         String prefix;
  91         if (id == null) {
  92             prefix = baseNameOption.getValue(options);
  93             int slash = prefix.lastIndexOf(File.separatorChar);
  94             prefix = prefix.substring(slash + 1);
  95         } else {
  96             prefix = id;
  97         }
  98         for (;;) {
  99             int fileNameLengthWithoutLabel = uniqueTag.length() + ext.length() + prefix.length() + "[]".length();
 100             int labelLengthLimit = MAX_FILE_NAME_LENGTH - fileNameLengthWithoutLabel;
 101             String fileName;
 102             if (labelLengthLimit < ELLIPSIS.length()) {
 103                 // This means `id` is very long
 104                 String suffix = uniqueTag + ext;
 105                 int idLengthLimit = Math.min(MAX_FILE_NAME_LENGTH - suffix.length(), prefix.length());
 106                 fileName = sanitizeFileName(prefix.substring(0, idLengthLimit) + suffix);
 107             } else {
 108                 if (label == null) {
 109                     fileName = sanitizeFileName(prefix + uniqueTag + ext);
 110                 } else {
 111                     String adjustedLabel = label;
 112                     if (label.length() > labelLengthLimit) {
 113                         adjustedLabel = label.substring(0, labelLengthLimit - ELLIPSIS.length()) + ELLIPSIS;
 114                     }
 115                     fileName = sanitizeFileName(prefix + '[' + adjustedLabel + ']' + uniqueTag + ext);
 116                 }
 117             }
 118             Path dumpDir = DebugOptions.getDumpDirectory(options);
 119             Path result = Paths.get(dumpDir.toString(), fileName);
 120             try {
 121                 if (createDirectory) {
 122                     return Files.createDirectory(result);
 123                 } else {
 124                     try {
 125                         return Files.createFile(result);
 126                     } catch (AccessDeniedException e) {
 127                         /*
 128                          * Thrown on Windows if a directory with the same name already exists, so
 129                          * convert it to FileAlreadyExistsException if that's the case.
 130                          */
 131                         throw Files.isDirectory(result, NOFOLLOW_LINKS) ? new FileAlreadyExistsException(e.getFile()) : e;
 132                     }
 133                 }
 134             } catch (FileAlreadyExistsException e) {
 135                 uniqueTag = "_" + dumpCounter++;
 136             }
 137         }
 138     }
 139 
 140 }


  67                     Paths.get(String.valueOf(c));
  68                     buf.append(c);
  69                     continue;
  70                 } catch (InvalidPathException e) {
  71                 }
  72             }
  73             buf.append('_');
  74         }
  75         return buf.toString();
  76     }
  77 
  78     /**
  79      * A maximum file name length supported by most file systems. There is no platform independent
  80      * way to get this in Java. Normally it is 255. But for AUFS it is 242. Refer AUFS_MAX_NAMELEN
  81      * in http://aufs.sourceforge.net/aufs3/man.html.
  82      */
  83     private static final int MAX_FILE_NAME_LENGTH = 242;
  84 
  85     private static final String ELLIPSIS = "...";
  86 
  87     static Path createUnique(OptionValues options, OptionKey<String> baseNameOption, String id, String label, String ext, boolean createMissingDirectory) throws IOException {
  88         String uniqueTag = "";
  89         int dumpCounter = 1;
  90         String prefix;
  91         if (id == null) {
  92             prefix = baseNameOption.getValue(options);
  93             int slash = prefix.lastIndexOf(File.separatorChar);
  94             prefix = prefix.substring(slash + 1);
  95         } else {
  96             prefix = id;
  97         }
  98         for (;;) {
  99             int fileNameLengthWithoutLabel = uniqueTag.length() + ext.length() + prefix.length() + "[]".length();
 100             int labelLengthLimit = MAX_FILE_NAME_LENGTH - fileNameLengthWithoutLabel;
 101             String fileName;
 102             if (labelLengthLimit < ELLIPSIS.length()) {
 103                 // This means `id` is very long
 104                 String suffix = uniqueTag + ext;
 105                 int idLengthLimit = Math.min(MAX_FILE_NAME_LENGTH - suffix.length(), prefix.length());
 106                 fileName = sanitizeFileName(prefix.substring(0, idLengthLimit) + suffix);
 107             } else {
 108                 if (label == null) {
 109                     fileName = sanitizeFileName(prefix + uniqueTag + ext);
 110                 } else {
 111                     String adjustedLabel = label;
 112                     if (label.length() > labelLengthLimit) {
 113                         adjustedLabel = label.substring(0, labelLengthLimit - ELLIPSIS.length()) + ELLIPSIS;
 114                     }
 115                     fileName = sanitizeFileName(prefix + '[' + adjustedLabel + ']' + uniqueTag + ext);
 116                 }
 117             }
 118             Path dumpDir = DebugOptions.getDumpDirectory(options);
 119             Path result = Paths.get(dumpDir.toString(), fileName);
 120             try {
 121                 if (createMissingDirectory) {
 122                     return Files.createDirectory(result);
 123                 } else {
 124                     try {
 125                         return Files.createFile(result);
 126                     } catch (AccessDeniedException e) {
 127                         /*
 128                          * Thrown on Windows if a directory with the same name already exists, so
 129                          * convert it to FileAlreadyExistsException if that's the case.
 130                          */
 131                         throw Files.isDirectory(result, NOFOLLOW_LINKS) ? new FileAlreadyExistsException(e.getFile()) : e;
 132                     }
 133                 }
 134             } catch (FileAlreadyExistsException e) {
 135                 uniqueTag = "_" + dumpCounter++;
 136             }
 137         }
 138     }
 139 
 140 }
< prev index next >