|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | + |
| 20 | +package org.apache.iotdb.commons.utils; |
| 21 | + |
| 22 | +import org.apache.commons.lang3.SystemUtils; |
| 23 | + |
| 24 | +import java.util.Arrays; |
| 25 | +import java.util.HashSet; |
| 26 | +import java.util.Set; |
| 27 | + |
| 28 | +public class WindowsOSUtils { |
| 29 | + private static final String ILLEGAL_WINDOWS_CHARS = "\\/:*?\"<>|"; |
| 30 | + private static final Set<String> ILLEGAL_WINDOWS_NAMES = |
| 31 | + new HashSet<>(Arrays.asList("CON", "PRN", "AUX", "NUL", "COM1-COM9, LPT1-LPT9")); |
| 32 | + |
| 33 | + static { |
| 34 | + for (int i = 0; i < 10; ++i) { |
| 35 | + ILLEGAL_WINDOWS_NAMES.add("COM" + i); |
| 36 | + ILLEGAL_WINDOWS_NAMES.add("LPT" + i); |
| 37 | + } |
| 38 | + } |
| 39 | + |
| 40 | + public static final String OS_SEGMENT_ERROR = |
| 41 | + String.format( |
| 42 | + "In Windows System, the path shall not contains %s, equals one of %s, or ends with '.' or ' '.", |
| 43 | + ILLEGAL_WINDOWS_CHARS, ILLEGAL_WINDOWS_NAMES); |
| 44 | + |
| 45 | + public static boolean isLegalPathSegment4Windows(final String pathSegment) { |
| 46 | + if (!SystemUtils.IS_OS_WINDOWS) { |
| 47 | + return true; |
| 48 | + } |
| 49 | + for (final char illegalChar : ILLEGAL_WINDOWS_CHARS.toCharArray()) { |
| 50 | + if (pathSegment.indexOf(illegalChar) != -1) { |
| 51 | + return false; |
| 52 | + } |
| 53 | + } |
| 54 | + if (pathSegment.endsWith(".") || pathSegment.endsWith(" ")) { |
| 55 | + return false; |
| 56 | + } |
| 57 | + for (final String illegalName : ILLEGAL_WINDOWS_NAMES) { |
| 58 | + if (pathSegment.equalsIgnoreCase(illegalName)) { |
| 59 | + return false; |
| 60 | + } |
| 61 | + } |
| 62 | + return true; |
| 63 | + } |
| 64 | +} |
0 commit comments