Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -38,22 +38,37 @@ public class AttributeUtils {
/*
Reads last-modified with proper failure handling if something goes wrong.
*/
public static long getLastModified(@Nonnull File file) {
public static long getLastModified(@Nonnull Path path) {
try {
BasicFileAttributes basicFileAttributes = Files.readAttributes(file.toPath(), BasicFileAttributes.class);
BasicFileAttributes basicFileAttributes = Files.readAttributes(path, BasicFileAttributes.class);
return basicFileAttributes.lastModifiedTime().toMillis();
} catch (IOException e) {
throw new RuntimeException(e);
}
}

public static void chmod(@Nonnull File file, int mode) throws IOException {
final Path path = file.toPath();
/**
* @deprecated Use {@link #getLastModified(Path)} instead
*/
@Deprecated
public static long getLastModified(@Nonnull File file) {
return getLastModified(file.toPath());
}

public static void chmod(@Nonnull Path path, int mode) throws IOException {
if (!Files.isSymbolicLink(path)) {
Files.setPosixFilePermissions(path, getPermissions(mode));
}
}

/**
* @deprecated Use {@link #chmod(Path, int)} instead
*/
@Deprecated
public static void chmod(@Nonnull File file, int mode) throws IOException {
chmod(file.toPath(), mode);
}

@Nonnull
public static Set<PosixFilePermission> getPermissions(int mode) {
Set<PosixFilePermission> perms = new HashSet<>();
Expand Down Expand Up @@ -90,14 +105,22 @@ public static Set<PosixFilePermission> getPermissions(int mode) {
return perms;
}

/**
* @deprecated Use {@link Files#readAttributes(Path, Class, java.nio.file.LinkOption...)} directly
*/
@Deprecated
@Nonnull
public static PosixFileAttributes getPosixFileAttributes(@Nonnull File file) throws IOException {
return Files.readAttributes(file.toPath(), PosixFileAttributes.class, LinkOption.NOFOLLOW_LINKS);
public static PosixFileAttributes getPosixFileAttributes(@Nonnull Path path) throws IOException {
return Files.readAttributes(path, PosixFileAttributes.class, LinkOption.NOFOLLOW_LINKS);
}

/**
* @deprecated Use {@link Files#readAttributes(Path, Class, java.nio.file.LinkOption...)} directly
*/
@Deprecated
@Nonnull
public static BasicFileAttributes getFileAttributes(@Nonnull File file) throws IOException {
return getFileAttributes(file.toPath());
public static PosixFileAttributes getPosixFileAttributes(@Nonnull File file) throws IOException {
return getPosixFileAttributes(file.toPath());
}

public static BasicFileAttributes getFileAttributes(Path path) throws IOException {
Expand All @@ -111,16 +134,38 @@ public static BasicFileAttributes getFileAttributes(Path path) throws IOExceptio
return Files.readAttributes(path, BasicFileAttributes.class, LinkOption.NOFOLLOW_LINKS);
}

/**
* @deprecated Use {@link #getFileAttributes(Path)} instead
*/
@Deprecated
@Nonnull
public static BasicFileAttributes getFileAttributes(@Nonnull File file) throws IOException {
return getFileAttributes(file.toPath());
}

public static boolean isUnix(Path path) {
return path.getFileSystem().supportedFileAttributeViews().contains("unix");
}

/**
* @deprecated Use {@link Files#getFileAttributeView(Path, Class, java.nio.file.LinkOption...)} directly
*/
@Deprecated
@Nullable
public static FileOwnerAttributeView getFileOwnershipInfo(@Nonnull File file) throws IOException {
public static FileOwnerAttributeView getFileOwnershipInfo(@Nonnull Path path) throws IOException {
try {
return Files.getFileAttributeView(file.toPath(), FileOwnerAttributeView.class, LinkOption.NOFOLLOW_LINKS);
return Files.getFileAttributeView(path, FileOwnerAttributeView.class, LinkOption.NOFOLLOW_LINKS);
} catch (UnsupportedOperationException e) {
return null;
}
}

/**
* @deprecated Use {@link Files#getFileAttributeView(Path, Class, java.nio.file.LinkOption...)} directly
*/
@Deprecated
@Nullable
public static FileOwnerAttributeView getFileOwnershipInfo(@Nonnull File file) throws IOException {
return getFileOwnershipInfo(file.toPath());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -86,14 +86,26 @@ public FileAttributes(
this(file);
}

/**
* @deprecated Use {@link #FileAttributes(Path)} instead
*/
@Deprecated
public FileAttributes(@Nonnull File file) throws IOException {
this(file.toPath(), false);
}

/**
* @deprecated Use {@link #FileAttributes(Path, boolean)} instead
*/
@Deprecated
public FileAttributes(@Nonnull File file, boolean followLinks) throws IOException {
this(file.toPath(), followLinks);
}

public FileAttributes(@Nonnull Path path) throws IOException {
this(path, false);
}

private static Map<Integer, String> getUserCache(FileSystem fs) {
return UIDS_CACHE.computeIfAbsent(fs, f -> new ConcurrentHashMap<>());
}
Expand Down Expand Up @@ -199,8 +211,16 @@ public FileAttributes(
this.lastModifiedTime = lastModifiedTime;
}

public static @Nonnull PlexusIoResourceAttributes uncached(@Nonnull Path path) throws IOException {
return new FileAttributes(path);
}

/**
* @deprecated Use {@link #uncached(Path)} instead
*/
@Deprecated
public static @Nonnull PlexusIoResourceAttributes uncached(@Nonnull File file) throws IOException {
return new FileAttributes(file);
return uncached(file.toPath());
}

@Nullable
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

import java.io.File;
import java.io.IOException;
import java.nio.file.Path;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.List;
Expand Down Expand Up @@ -118,36 +119,48 @@ public static boolean isOctalModeEnabled(int mode, int targetMode) {
return (mode & targetMode) != 0;
}

public static PlexusIoResourceAttributes getFileAttributes(Path path) throws IOException {
return getFileAttributes(path, false);
}

public static PlexusIoResourceAttributes getFileAttributes(Path path, boolean followLinks) throws IOException {
return new FileAttributes(path, followLinks);
}

/**
* @deprecated Use {@link #getFileAttributes(Path)} instead
*/
@Deprecated
public static PlexusIoResourceAttributes getFileAttributes(File file) throws IOException {
return getFileAttributes(file, false);
return getFileAttributes(file.toPath(), false);
}

/**
* @deprecated Use {@link #getFileAttributes(Path, boolean)} instead
*/
@Deprecated
public static PlexusIoResourceAttributes getFileAttributes(File file, boolean followLinks) throws IOException {
Map<String, PlexusIoResourceAttributes> byPath = getFileAttributesByPath(file, false, followLinks);
final PlexusIoResourceAttributes o = byPath.get(file.getAbsolutePath());
if (o == null) {
// We're on a crappy old java version (5) or the OS from hell. Just "fail".
return SimpleResourceAttributes.lastResortDummyAttributesForBrokenOS();
}
return o;
return getFileAttributes(file.toPath(), followLinks);
}

public static Map<String, PlexusIoResourceAttributes> getFileAttributesByPath(File dir) throws IOException {
public static @Nonnull Map<String, PlexusIoResourceAttributes> getFileAttributesByPath(Path dir)
throws IOException {
return getFileAttributesByPath(dir, true);
}

public static @Nonnull Map<String, PlexusIoResourceAttributes> getFileAttributesByPath(
@Nonnull File dir, boolean recursive) throws IOException {
@Nonnull Path dir, boolean recursive) throws IOException {
return getFileAttributesByPath(dir, recursive, false);
}

public static @Nonnull Map<String, PlexusIoResourceAttributes> getFileAttributesByPath(
@Nonnull File dir, boolean recursive, boolean followLinks) throws IOException {
@Nonnull Path dir, boolean recursive, boolean followLinks) throws IOException {
final List<String> fileAndDirectoryNames;
if (recursive && dir.isDirectory()) {
fileAndDirectoryNames = FileUtils.getFileAndDirectoryNames(dir, null, null, true, true, true, true);
File dirAsFile = dir.toFile();
if (recursive && java.nio.file.Files.isDirectory(dir)) {
fileAndDirectoryNames = FileUtils.getFileAndDirectoryNames(dirAsFile, null, null, true, true, true, true);
} else {
fileAndDirectoryNames = Collections.singletonList(dir.getAbsolutePath());
fileAndDirectoryNames = Collections.singletonList(dirAsFile.getAbsolutePath());
}

final Map<String, PlexusIoResourceAttributes> attributesByPath = new LinkedHashMap<>();
Expand All @@ -157,4 +170,30 @@ public static Map<String, PlexusIoResourceAttributes> getFileAttributesByPath(Fi
}
return attributesByPath;
}

/**
* @deprecated Use {@link #getFileAttributesByPath(Path)} instead
*/
@Deprecated
public static Map<String, PlexusIoResourceAttributes> getFileAttributesByPath(File dir) throws IOException {
return getFileAttributesByPath(dir.toPath(), true);
}

/**
* @deprecated Use {@link #getFileAttributesByPath(Path, boolean)} instead
*/
@Deprecated
public static @Nonnull Map<String, PlexusIoResourceAttributes> getFileAttributesByPath(
@Nonnull File dir, boolean recursive) throws IOException {
return getFileAttributesByPath(dir.toPath(), recursive, false);
}

/**
* @deprecated Use {@link #getFileAttributesByPath(Path, boolean, boolean)} instead
*/
@Deprecated
public static @Nonnull Map<String, PlexusIoResourceAttributes> getFileAttributesByPath(
@Nonnull File dir, boolean recursive, boolean followLinks) throws IOException {
return getFileAttributesByPath(dir.toPath(), recursive, followLinks);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,22 +27,48 @@
* @author Kristian Rosenvold
*/
public class SymlinkUtils {
/**
* Reads the target of the symbolic link
*
* @param symlink A path that is a symlink
* @return A path that is the target of the symlink
* @throws java.io.IOException
* @deprecated Use {@link Files#readSymbolicLink(Path)} directly
*/
@Deprecated
public static @Nonnull Path readSymbolicLink(@Nonnull Path symlink) throws IOException {
return Files.readSymbolicLink(symlink);
}

/**
* Reads the target of the symbolic link
*
* @param symlink A file that is a symlink
* @return A file that is the target of the symlink
* @throws java.io.IOException
* @deprecated Use {@link Files#readSymbolicLink(Path)} directly
*/
@Deprecated
public static @Nonnull File readSymbolicLink(@Nonnull File symlink) throws IOException {
return Files.readSymbolicLink(symlink.toPath()).toFile();
return readSymbolicLink(symlink.toPath()).toFile();
}

public static @Nonnull File createSymbolicLink(@Nonnull File symlink, File target) throws IOException {
Path link = symlink.toPath();
if (!Files.exists(link, LinkOption.NOFOLLOW_LINKS)) {
link = Files.createSymbolicLink(link, target.toPath());
/**
* @deprecated Use {@link Files#createSymbolicLink(Path, Path, java.nio.file.attribute.FileAttribute[])} directly
*/
@Deprecated
public static @Nonnull Path createSymbolicLink(@Nonnull Path symlink, Path target) throws IOException {
if (!Files.exists(symlink, LinkOption.NOFOLLOW_LINKS)) {
return Files.createSymbolicLink(symlink, target);
}
return link.toFile();
return symlink;
}

/**
* @deprecated Use {@link Files#createSymbolicLink(Path, Path, java.nio.file.attribute.FileAttribute[])} directly
*/
@Deprecated
public static @Nonnull File createSymbolicLink(@Nonnull File symlink, File target) throws IOException {
return createSymbolicLink(symlink.toPath(), target.toPath()).toFile();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,33 @@ public abstract class AbstractPlexusIoArchiveResourceCollection extends Abstract
protected AbstractPlexusIoArchiveResourceCollection() {}

/**
* Sets the zip file
* Sets the archive file
*/
public void setFile(java.nio.file.Path file) {
this.file = file.toFile();
}

/**
* Sets the archive file
* @deprecated Use {@link #setFile(java.nio.file.Path)} instead
*/
@Deprecated
public void setFile(File file) {
this.file = file;
}

/**
* Returns the zip file
* Returns the archive file as a Path
*/
public java.nio.file.Path getFileAsPath() {
return file != null ? file.toPath() : null;
}

/**
* Returns the archive file
* @deprecated Use {@link #getFileAsPath()} instead
*/
@Deprecated
public File getFile() {
return file;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,26 @@ public abstract class PlexusIoCompressedFileResourceCollection

private InputStreamTransformer streamTransformers = AbstractPlexusIoResourceCollection.identityTransformer;

public java.nio.file.Path getFileAsPath() {
return file != null ? file.toPath() : null;
}

/**
* @deprecated Use {@link #getFileAsPath()} instead
*/
@Deprecated
public File getFile() {
return file;
}

public void setFile(java.nio.file.Path file) {
this.file = file.toFile();
}

/**
* @deprecated Use {@link #setFile(java.nio.file.Path)} instead
*/
@Deprecated
public void setFile(File file) {
this.file = file;
}
Expand Down
Loading