All Downloads are FREE. Search and download functionalities are using the official Maven repository.
Please wait. This can take some minutes ...
Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance.
Project price only 1 $
You can buy this project and download/modify it how often you want.
org.whitesource.utils.files.FilesUtils Maven / Gradle / Ivy
/**
* The project is licensed under the WhiteSource Software End User License Agreement .
* Copyright (C) 2015 WhiteSource Ltd.
* Licensed under the WhiteSource Software End User License Agreement;
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* https://s3.amazonaws.com/unified-agent/LICENSE.txt
*/
package org.whitesource.utils.files;
import org.apache.commons.io.FileUtils;
import org.slf4j.Logger;
import org.whitesource.utils.Constants;
import org.whitesource.utils.logger.LoggerFactory;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.attribute.BasicFileAttributes;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;
/**
* @author eugen.horovitz
*/
public class FilesUtils {
/* --- Static members --- */
private static final Logger logger = LoggerFactory.getLogger(FilesUtils.class);
private final String JAVA_TEMP_DIR = System.getProperty("java.io.tmpdir");
public String createTmpFolder(boolean addCharToEndOfUrl, String nameOfFolder) {
String result = getTempDirPackages(addCharToEndOfUrl, nameOfFolder);
try {
FileUtils.forceMkdir(new File(result));
} catch (IOException e) {
logger.warn("Failed to create temp folder : " + e.getMessage());
result = null;
}
return result;
}
private String getTempDirPackages(boolean addCharToEndOfUrl, String nameOfFolder) {
String creationDate = new SimpleDateFormat("yyyyMMddHHmmss").format(new Date());
String tempFolder = JAVA_TEMP_DIR.endsWith(File.separator) ? JAVA_TEMP_DIR + nameOfFolder + File.separator + creationDate :
JAVA_TEMP_DIR + File.separator + nameOfFolder + File.separator + creationDate;
if (addCharToEndOfUrl) {
tempFolder = tempFolder + "1";
}
return tempFolder;
}
public List getSubDirectories(String directory, String[] includes, String[] excludesExtended, boolean followSymlinks, boolean globCaseSensitive) {
String[] files;
FilesScanner filesScanner = new FilesScanner();
try {
files = filesScanner.getDirectoryContent(directory, includes, excludesExtended, followSymlinks, globCaseSensitive, true);
} catch (Exception ex) {
logger.info("Error getting sub directories from: " + directory, ex.getMessage());
files = new String[0];
}
return Arrays.stream(files).map(subDir -> Paths.get(directory, subDir)).collect(Collectors.toList());
}
public Map> fillFilesMap(Collection pathsToScan, String[] includes, String[] excludesExtended,
boolean followSymlinks, boolean globCaseSensitive, String failErrorLevel) {
Map> fileMap = new HashMap<>();
for (String scannerBaseDir : pathsToScan) {
File file = new File(scannerBaseDir);
logger.debug("Scanning {}", file.getAbsolutePath());
if (file.exists()) {
FilesScanner filesScanner = new FilesScanner();
if (file.isDirectory()) {
File basedir = new File(scannerBaseDir);
String[] fileNames = filesScanner.getDirectoryContent(scannerBaseDir, includes, excludesExtended, followSymlinks, globCaseSensitive);
// convert array to list (don't use Arrays.asList, might be added to later)
List fileNameList = Arrays.stream(fileNames).collect(Collectors.toList());
fileMap.put(basedir, fileNameList);
} else {
// handle single file
boolean included = filesScanner.isIncluded(file, includes, excludesExtended, followSymlinks, globCaseSensitive);
if (included) {
Collection files = fileMap.get(file.getParentFile());
if (files == null) {
files = new ArrayList<>();
}
files.add(file.getName());
fileMap.put(file.getParentFile(), files);
}
}
} else {
logger.info("File {} doesn't exist", scannerBaseDir);
if (Constants.ALL.equals(failErrorLevel)) {
fileMap.put(null, Arrays.asList("Failed with File Doesn't Exist, path = " + scannerBaseDir));
return fileMap;
}
}
}
return fileMap;
}
/* --- Static methods --- */
public static void deleteDirectory(File directory) {
if (directory != null) {
try {
FileUtils.forceDelete(directory);
} catch (IOException e) {
// do nothing
}
}
}
public static String getFileExtension(String fileName) {
if (fileName == null) fileName = Constants.EMPTY_STRING;
String extension = Constants.EMPTY_STRING;
int i = fileName.lastIndexOf(Constants.DOT);
if (i > 0 && i < fileName.length() - 2) {
extension = fileName.substring(i + 1);
}
return extension;
}
public static String removeTempFiles(String rootDirectory, long creationTime) {
logger.debug("REMOVE TEMP FILES from {}, creation-time: {}", rootDirectory, new Date(creationTime).toString());
String errors = "";
File directory = new File(rootDirectory);
File[] fList = directory.listFiles();
if (fList != null) {
for (File file : fList) {
try {
BasicFileAttributes fileAttributes = Files.readAttributes(file.toPath(), BasicFileAttributes.class);
if (fileAttributes.creationTime().toMillis() > creationTime) {
logger.debug("removing {}, created: {}", file.getPath(), new Date(fileAttributes.creationTime().toMillis()).toString());
FileUtils.forceDelete(file);
} else if (file.isDirectory()) {
if (Files.isSymbolicLink(file.toPath())) {
FileUtils.forceDelete(file);
} else {
errors = errors.concat(removeTempFiles(file.getPath(), creationTime));
}
}
} catch (IOException e) {
errors = errors.concat("can't remove " + file.getPath() + ": " + e.getMessage() + '\n');
}
}
}
return errors;
}
}