, Object> NULL_VALUES = new HashMap<>();
static {
NULL_VALUES.put(boolean.class, false);
NULL_VALUES.put(byte.class, (byte) 0);
NULL_VALUES.put(short.class, (short) 0);
NULL_VALUES.put(char.class, '\0');
NULL_VALUES.put(int.class, 0);
NULL_VALUES.put(long.class, 0L);
NULL_VALUES.put(float.class, 0.0f);
NULL_VALUES.put(double.class, 0.0d);
}
/*
* Run uname with specified arguments.
*/
public static OutputAnalyzer uname(String... args) throws Throwable {
String[] cmds = new String[args.length + 1];
cmds[0] = "uname";
System.arraycopy(args, 0, cmds, 1, args.length);
return ProcessTools.executeCommand(cmds);
}
/**
* Creates an empty file in "user.dir" if the property set.
*
* This method is meant as a replacement for {@link Files#createTempFile(String, String, FileAttribute...)}
* that doesn't leave files behind in /tmp directory of the test machine
*
* If the property "user.dir" is not set, "." will be used.
*
* @param prefix the prefix string to be used in generating the file's name;
* may be null
* @param suffix the suffix string to be used in generating the file's name;
* may be null, in which case ".tmp" is used
* @param attrs an optional list of file attributes to set atomically when creating the file
* @return the path to the newly created file that did not exist before this
* method was invoked
* @throws IOException if an I/O error occurs or dir does not exist
*
* @see Files#createTempFile(String, String, FileAttribute...)
*/
public static Path createTempFile(String prefix, String suffix, FileAttribute... attrs) throws IOException {
Path dir = Paths.get(System.getProperty("user.dir", "."));
return Files.createTempFile(dir, prefix, suffix, attrs);
}
/**
* Creates an empty directory in "user.dir" or "."
*
* This method is meant as a replacement for {@link Files#createTempDirectory(Path, String, FileAttribute...)}
* that doesn't leave files behind in /tmp directory of the test machine
*
* If the property "user.dir" is not set, "." will be used.
*
* @param prefix the prefix string to be used in generating the directory's name; may be null
* @param attrs an optional list of file attributes to set atomically when creating the directory
* @return the path to the newly created directory
* @throws IOException if an I/O error occurs or dir does not exist
*
* @see Files#createTempDirectory(Path, String, FileAttribute...)
*/
public static Path createTempDirectory(String prefix, FileAttribute... attrs) throws IOException {
Path dir = Paths.get(System.getProperty("user.dir", "."));
return Files.createTempDirectory(dir, prefix, attrs);
}
/**
* Converts slashes in a pathname to backslashes
* if slashes is not the file separator.
*/
public static String convertPath(String path) {
// No need to do the conversion if file separator is '/'
if (FILE_SEPARATOR.length() == 1 && FILE_SEPARATOR.charAt(0) == '/') {
return path;
}
char[] cs = path.toCharArray();
for (int i = 0; i < cs.length; i++) {
if (cs[i] == '/') {
cs[i] = '\\';
}
}
String newPath = new String(cs);
return newPath;
}
/**
* Return file directories that satisfy the specified filter.
*
* @param searchDirectory the base directory to search
* @param filter a filename filter
* @return file directories
*/
public static List findFiles(Path searchDirectory,
FilenameFilter filter) {
return Arrays.stream(searchDirectory.toFile().listFiles(filter))
.map(f -> f.toPath())
.collect(Collectors.toList());
}
/**
* Copy files to the target path.
*
* @param source the paths to the files to copy
* @param target the path to the target files
* @param filenameMapper mapper function applied to filenames
* @param options options specifying how the copy should be done
* @return the paths to the target files
* @throws IOException if error occurs
*/
public static List copyFiles(List source, Path target,
Function filenameMapper,
CopyOption... options) throws IOException {
List result = new ArrayList<>();
if (!target.toFile().exists()) {
Files.createDirectory(target);
}
for (Path file : source) {
if (!file.toFile().exists()) {
continue;
}
String baseName = file.getFileName().toString();
Path targetFile = target.resolve(filenameMapper.apply(baseName));
Files.copy(file, targetFile, options);
result.add(targetFile);
}
return result;
}
/**
* Copy files to the target path.
*
* @param source the paths to the files to copy
* @param target the path to the target files
* @param options options specifying how the copy should be done
* @return the paths to the target files
* @throws IOException if error occurs
*/
public static List copyFiles(List source, Path target,
CopyOption... options) throws IOException {
return copyFiles(source, target, (s) -> s, options);
}
/**
* Replace file string by applying the given mapper function.
*
* @param source the file to read
* @param contentMapper the mapper function applied to file's content
* @throws IOException if an I/O error occurs
*/
public static void replaceFileString(Path source,
Function contentMapper) throws IOException {
StringBuilder sb = new StringBuilder();
String lineSep = System.getProperty("line.separator");
try (BufferedReader reader =
new BufferedReader(new FileReader(source.toFile()))) {
String line;
// read all and replace all at once??
while ((line = reader.readLine()) != null) {
sb.append(contentMapper.apply(line))
.append(lineSep);
}
}
try (FileWriter writer = new FileWriter(source.toFile())) {
writer.write(sb.toString());
}
}
/**
* Replace files' string by applying the given mapper function.
*
* @param source the file to read
* @param contentMapper the mapper function applied to files' content
* @throws IOException if an I/O error occurs
*/
public static void replaceFilesString(List source,
Function contentMapper) throws IOException {
for (Path file : source) {
replaceFileString(file, contentMapper);
}
}
/**
* Grant file user access or full access to everyone.
*
* @param file file to grant access
* @param userOnly true for user access, otherwise full access to everyone
* @throws IOException if error occurs
*/
public static void grantFileAccess(Path file, boolean userOnly)
throws IOException {
Set attr = file.getFileSystem().supportedFileAttributeViews();
if (attr.contains("posix")) {
String perms = userOnly ? "rwx------" : "rwxrwxrwx";
Files.setPosixFilePermissions(file, PosixFilePermissions.fromString(perms));
} else if (attr.contains("acl")) {
AclFileAttributeView view =
Files.getFileAttributeView(file, AclFileAttributeView.class);
List acl = new ArrayList<>();
for (AclEntry thisEntry : view.getAcl()) {
if (userOnly) {
if (thisEntry.principal().getName()
.equals(view.getOwner().getName())) {
acl.add(allowAccess(thisEntry));
} else if (thisEntry.type() == AclEntryType.ALLOW) {
acl.add(revokeAccess(thisEntry));
} else {
acl.add(thisEntry);
}
} else {
if (thisEntry.type() != AclEntryType.ALLOW) {
acl.add(allowAccess(thisEntry));
} else {
acl.add(thisEntry);
}
}
}
view.setAcl(acl);
} else {
throw new RuntimeException("Unsupported file attributes: " + attr);
}
}
/**
* Return an ACL entry that revokes owner access.
*
* @param acl original ACL entry to build from
* @return an ACL entry that revokes all access
*/
public static AclEntry revokeAccess(AclEntry acl) {
return buildAclEntry(acl, AclEntryType.DENY);
}
/**
* Return an ACL entry that allow owner access.
* @param acl original ACL entry to build from
* @return an ACL entry that allows all access
*/
public static AclEntry allowAccess(AclEntry acl) {
return buildAclEntry(acl, AclEntryType.ALLOW);
}
/**
* Build an ACL entry with a given ACL entry type.
*
* @param acl original ACL entry to build from
* @return an ACL entry with a given ACL entry type
*/
public static AclEntry buildAclEntry(AclEntry acl, AclEntryType type) {
return AclEntry.newBuilder(acl)
.setType(type)
.build();
}
/**
* Grant file user access.
*
* @param file file to grant access
* @throws IOException if error occurs
*/
public static void userAccess(Path file) throws IOException {
grantFileAccess(file, true);
}
/**
* Grant file full access to everyone.
*
* @param file file to grant access
* @throws IOException if error occurs
*/
public static void fullAccess(Path file) throws IOException {
grantFileAccess(file, false);
}
}