private String normalizePath( boolean isFile, String path ) { if (path == null) { return null; } if (isFile) { path = IoUtils.normalizeFilePath(path); } else { path = IoUtils.normalizeDirPath(path); } return path.replace("//", "/").replace("\\\\", "\\"); }
/** * Replaces any '\\' or '/' characters with the ones for this system * * @param source the file path * @return the properly formatted file path */ public static String normalizeFilePath( String source ) { return normalizeFilePath(source, OperatingSystemType.getCurrentOsType()); }
/** * 1. Replaces any '\\' or '/' characters with the ones for the target system * <p> * 2. Appends system specific file separator character at the end if not present * * @param source the directory path * @param osType the system to format for * @return the properly formatted directory path */ public static String normalizeDirPath( String source, OperatingSystemType osType ) { String fileSeparator = osType.isWindows() ? "\\" : "/"; source = normalizeFilePath(source, osType); if (source != null && !source.endsWith(fileSeparator)) { source = source + fileSeparator; } return source; }
/** * * @param userId user id * @param groupId group id * @param filename the file name * @throws FileSystemOperationException */ private void chown( long userId, long groupId, String filename ) { filename = IoUtils.normalizeFilePath(filename, osType); String[] command = new String[]{ "/bin/sh", "-c", "chown " + String.valueOf(userId) + ":" + String.valueOf(groupId) + " '" + filename + "'" }; String[] result = executeExternalProcess(command); if (!result[2].equals("0")) { throw new FileSystemOperationException("Could not update UID and GID for '" + filename + "': " + result[1]); } }
@Override public void setFilePermissions( String sourceFile, String permissions ) { sourceFile = IoUtils.normalizeFilePath(sourceFile, osType); File file = new File(sourceFile); checkFileExistence(file); checkAttributeOsSupport(FileAttributes.PERMISSIONS); try { Files.setPosixFilePermissions(new File(sourceFile).getCanonicalFile().toPath(), getPosixFilePermission(Integer.parseInt(permissions, 8))); } catch (IOException ioe) { throw new FileSystemOperationException("Could not update permissions for file '" + sourceFile + "'", ioe); } log.info("Successfully set permissions of file '" + sourceFile + "' to " + permissions); }
private Set<String> getFileAndDirectoryPathsIndex( File dir ) throws EnvironmentCleanupException { Set<String> fileAndDirectoryPaths = new HashSet<String>(); File[] files = dir.listFiles(); if (files != null) { for (File file : files) { if (file.isDirectory()) { fileAndDirectoryPaths.add(IoUtils.normalizeDirPath(getFileCanonicalPath(file))); fileAndDirectoryPaths.addAll(getFileAndDirectoryPathsIndex(file)); } else { fileAndDirectoryPaths.add(IoUtils.normalizeFilePath(getFileCanonicalPath(file))); } } } return fileAndDirectoryPaths; }
checkParamLengthForSocketTransfer(fileNameLength, "file name length"); String fileName = readString(dis, fileNameLength); fileName = IoUtils.normalizeFilePath(fileName, osType); // switch file separators according to the current OS File file = new File(fileName); if (fdType.equals(FILE_COPY_SOCKET_COMMAND)) {
private void backupAllFilesInDirectory( File origDir ) throws EnvironmentCleanupException { File[] files = origDir.listFiles(); if (files == null) { throw new EnvironmentCleanupException("No such directory '" + origDir + "'."); } for (File file : files) { if (file.isDirectory()) { backupAllFilesInDirectory(file); } else { String backupFileName = IoUtils.normalizeFilePath(getFileCanonicalPath(file)); // important that case matches incl. drive letter so origDirName and backupDir // should also come after getCanonicalPath() invocation backupFileName = backupFileName.replace(origDirName, getBackupDir()); new FileEnvironmentUnit(getFileCanonicalPath(file), IoUtils.getFilePath(backupFileName), file.getName()).backup(); } } // if the directory is empty - create the new empty directory in the backup folder if (files.length == 0) { String backupDir = IoUtils.normalizeDirPath(getFileCanonicalPath(origDir)) .replace(origDirName, getBackupDir()); new File(backupDir).mkdirs(); } }
} else { String backupFilePath = IoUtils.normalizeFilePath(getFileCanonicalPath(file)) .replace(origDirName, getBackupDir()); if (fileAndDirectoryPaths.contains(backupFilePath)) {
boolean hidden ) { sourceFile = IoUtils.normalizeFilePath(sourceFile, osType); checkFileExistence(new File(sourceFile));