Browse Source

FileUtils.toPath to convert File to Path

When invoking File.toPath(), an (unchecked) InvalidPathException may be
thrown which should be converted to a checked IOException.

For now, we will replace File.toPath() by FileUtils.toPath() only for
code which can already handle IOExceptions.

Change-Id: I0f0c5fd2a11739e7a02071adae9a5550985d4df6
Signed-off-by: Marc Strapetz <marc.strapetz@syntevo.com>
stable-4.10
Marc Strapetz 7 years ago
parent
commit
9bb126d12d
  1. 2
      org.eclipse.jgit.lfs/src/org/eclipse/jgit/lfs/CleanFilter.java
  2. 3
      org.eclipse.jgit.lfs/src/org/eclipse/jgit/lfs/SmudgeFilter.java
  3. 3
      org.eclipse.jgit.test/exttst/org/eclipse/jgit/ignore/CGitVsJGitRandomIgnorePatternTest.java
  4. 2
      org.eclipse.jgit/src/org/eclipse/jgit/internal/storage/file/GC.java
  5. 5
      org.eclipse.jgit/src/org/eclipse/jgit/internal/storage/file/GcLog.java
  6. 4
      org.eclipse.jgit/src/org/eclipse/jgit/internal/storage/file/ObjectDirectory.java
  7. 2
      org.eclipse.jgit/src/org/eclipse/jgit/util/FS_POSIX.java
  8. 44
      org.eclipse.jgit/src/org/eclipse/jgit/util/FileUtils.java

2
org.eclipse.jgit.lfs/src/org/eclipse/jgit/lfs/CleanFilter.java

@ -124,7 +124,7 @@ public class CleanFilter extends FilterCommand {
public CleanFilter(Repository db, InputStream in, OutputStream out) public CleanFilter(Repository db, InputStream in, OutputStream out)
throws IOException { throws IOException {
super(in, out); super(in, out);
lfsUtil = new Lfs(db.getDirectory().toPath().resolve("lfs")); //$NON-NLS-1$ lfsUtil = new Lfs(FileUtils.toPath(db.getDirectory()).resolve("lfs")); //$NON-NLS-1$
Files.createDirectories(lfsUtil.getLfsTmpDir()); Files.createDirectories(lfsUtil.getLfsTmpDir());
tmpFile = lfsUtil.createTmpFile(); tmpFile = lfsUtil.createTmpFile();
this.aOut = new AtomicObjectOutputStream(tmpFile.toAbsolutePath()); this.aOut = new AtomicObjectOutputStream(tmpFile.toAbsolutePath());

3
org.eclipse.jgit.lfs/src/org/eclipse/jgit/lfs/SmudgeFilter.java

@ -53,6 +53,7 @@ import org.eclipse.jgit.attributes.FilterCommandFactory;
import org.eclipse.jgit.attributes.FilterCommandRegistry; import org.eclipse.jgit.attributes.FilterCommandRegistry;
import org.eclipse.jgit.lfs.lib.Constants; import org.eclipse.jgit.lfs.lib.Constants;
import org.eclipse.jgit.lib.Repository; import org.eclipse.jgit.lib.Repository;
import org.eclipse.jgit.util.FileUtils;
/** /**
* Built-in LFS smudge filter * Built-in LFS smudge filter
@ -100,7 +101,7 @@ public class SmudgeFilter extends FilterCommand {
public SmudgeFilter(Repository db, InputStream in, OutputStream out) public SmudgeFilter(Repository db, InputStream in, OutputStream out)
throws IOException { throws IOException {
super(in, out); super(in, out);
lfs = new Lfs(db.getDirectory().toPath().resolve(Constants.LFS)); lfs = new Lfs(FileUtils.toPath(db.getDirectory()).resolve(Constants.LFS));
LfsPointer res = LfsPointer.parseLfsPointer(in); LfsPointer res = LfsPointer.parseLfsPointer(in);
if (res != null) { if (res != null) {
Path mediaFile = lfs.getMediaFile(res.getOid()); Path mediaFile = lfs.getMediaFile(res.getOid());

3
org.eclipse.jgit.test/exttst/org/eclipse/jgit/ignore/CGitVsJGitRandomIgnorePatternTest.java

@ -56,6 +56,7 @@ import java.util.List;
import java.util.Random; import java.util.Random;
import org.eclipse.jgit.api.Git; import org.eclipse.jgit.api.Git;
import org.eclipse.jgit.util.FileUtils;
import org.junit.Assert; import org.junit.Assert;
import org.junit.Test; import org.junit.Test;
@ -156,7 +157,7 @@ public class CGitVsJGitRandomIgnorePatternTest {
throws UnsupportedEncodingException, IOException { throws UnsupportedEncodingException, IOException {
this.gitDir = gitDir; this.gitDir = gitDir;
this.pattern = pattern; this.pattern = pattern;
Files.write(new File(gitDir, ".gitignore").toPath(), Files.write(FileUtils.toPath(new File(gitDir, ".gitignore")),
(pattern + "\n").getBytes("UTF-8"), (pattern + "\n").getBytes("UTF-8"),
StandardOpenOption.CREATE, StandardOpenOption.CREATE,
StandardOpenOption.TRUNCATE_EXISTING, StandardOpenOption.TRUNCATE_EXISTING,

2
org.eclipse.jgit/src/org/eclipse/jgit/internal/storage/file/GC.java

@ -950,7 +950,7 @@ public class GC {
} else { } else {
if (base == null || !n.startsWith(base)) { if (base == null || !n.startsWith(base)) {
try { try {
Files.delete(new File(packDir.toFile(), n).toPath()); Files.delete(FileUtils.toPath(new File(packDir.toFile(), n)));
} catch (IOException e) { } catch (IOException e) {
LOG.error(e.getMessage(), e); LOG.error(e.getMessage(), e);
} }

5
org.eclipse.jgit/src/org/eclipse/jgit/internal/storage/file/GcLog.java

@ -58,6 +58,7 @@ import java.time.Instant;
import org.eclipse.jgit.api.errors.JGitInternalException; import org.eclipse.jgit.api.errors.JGitInternalException;
import org.eclipse.jgit.internal.JGitText; import org.eclipse.jgit.internal.JGitText;
import org.eclipse.jgit.lib.ConfigConstants; import org.eclipse.jgit.lib.ConfigConstants;
import org.eclipse.jgit.util.FileUtils;
import org.eclipse.jgit.util.GitDateParser; import org.eclipse.jgit.util.GitDateParser;
import org.eclipse.jgit.util.SystemReader; import org.eclipse.jgit.util.SystemReader;
@ -105,12 +106,12 @@ class GcLog {
private boolean autoGcBlockedByOldLockFile(boolean background) { private boolean autoGcBlockedByOldLockFile(boolean background) {
try { try {
FileTime lastModified = Files.getLastModifiedTime(logFile.toPath()); FileTime lastModified = Files.getLastModifiedTime(FileUtils.toPath(logFile));
if (lastModified.toInstant().compareTo(getLogExpiry()) > 0) { if (lastModified.toInstant().compareTo(getLogExpiry()) > 0) {
// There is an existing log file, which is too recent to ignore // There is an existing log file, which is too recent to ignore
if (!background) { if (!background) {
try (BufferedReader reader = Files try (BufferedReader reader = Files
.newBufferedReader(logFile.toPath())) { .newBufferedReader(FileUtils.toPath(logFile))) {
char[] buf = new char[1000]; char[] buf = new char[1000];
int len = reader.read(buf, 0, 1000); int len = reader.read(buf, 0, 1000);
String oldError = new String(buf, 0, len); String oldError = new String(buf, 0, len);

4
org.eclipse.jgit/src/org/eclipse/jgit/internal/storage/file/ObjectDirectory.java

@ -715,7 +715,7 @@ public class ObjectDirectory extends FileObjectDatabase {
return InsertLooseObjectResult.EXISTS_LOOSE; return InsertLooseObjectResult.EXISTS_LOOSE;
} }
try { try {
Files.move(tmp.toPath(), dst.toPath(), Files.move(FileUtils.toPath(tmp), FileUtils.toPath(dst),
StandardCopyOption.ATOMIC_MOVE); StandardCopyOption.ATOMIC_MOVE);
dst.setReadOnly(); dst.setReadOnly();
unpackedObjectCache.add(id); unpackedObjectCache.add(id);
@ -732,7 +732,7 @@ public class ObjectDirectory extends FileObjectDatabase {
// //
FileUtils.mkdir(dst.getParentFile(), true); FileUtils.mkdir(dst.getParentFile(), true);
try { try {
Files.move(tmp.toPath(), dst.toPath(), Files.move(FileUtils.toPath(tmp), FileUtils.toPath(dst),
StandardCopyOption.ATOMIC_MOVE); StandardCopyOption.ATOMIC_MOVE);
dst.setReadOnly(); dst.setReadOnly();
unpackedObjectCache.add(id); unpackedObjectCache.add(id);

2
org.eclipse.jgit/src/org/eclipse/jgit/util/FS_POSIX.java

@ -191,7 +191,7 @@ public class FS_POSIX extends FS {
return f.setExecutable(false, false); return f.setExecutable(false, false);
try { try {
Path path = f.toPath(); Path path = FileUtils.toPath(f);
Set<PosixFilePermission> pset = Files.getPosixFilePermissions(path); Set<PosixFilePermission> pset = Files.getPosixFilePermissions(path);
// owner (user) is always allowed to execute. // owner (user) is always allowed to execute.

44
org.eclipse.jgit/src/org/eclipse/jgit/util/FileUtils.java

@ -51,6 +51,7 @@ import java.nio.channels.FileLock;
import java.nio.file.AtomicMoveNotSupportedException; import java.nio.file.AtomicMoveNotSupportedException;
import java.nio.file.CopyOption; import java.nio.file.CopyOption;
import java.nio.file.Files; import java.nio.file.Files;
import java.nio.file.InvalidPathException;
import java.nio.file.LinkOption; import java.nio.file.LinkOption;
import java.nio.file.Path; import java.nio.file.Path;
import java.nio.file.StandardCopyOption; import java.nio.file.StandardCopyOption;
@ -111,6 +112,25 @@ public class FileUtils {
*/ */
public static final int EMPTY_DIRECTORIES_ONLY = 16; public static final int EMPTY_DIRECTORIES_ONLY = 16;
/**
* Safe conversion from {@link java.io.File} to {@link java.nio.file.Path}.
*
* @param f
* {@code File} to be converted to {@code Path}
* @throws IOException
* in case the path represented by the file
* is not valid ({@link java.nio.file.InvalidPathException})
*
* @since 4.10
*/
public static Path toPath(final File f) throws IOException {
try {
return f.toPath();
} catch (InvalidPathException ex) {
throw new IOException(ex);
}
}
/** /**
* Delete file or empty folder * Delete file or empty folder
* *
@ -259,7 +279,7 @@ public class FileUtils {
int attempts = FS.DETECTED.retryFailedLockFileCommit() ? 10 : 1; int attempts = FS.DETECTED.retryFailedLockFileCommit() ? 10 : 1;
while (--attempts >= 0) { while (--attempts >= 0) {
try { try {
Files.move(src.toPath(), dst.toPath(), options); Files.move(toPath(src), toPath(dst), options);
return; return;
} catch (AtomicMoveNotSupportedException e) { } catch (AtomicMoveNotSupportedException e) {
throw e; throw e;
@ -269,7 +289,7 @@ public class FileUtils {
delete(dst, EMPTY_DIRECTORIES_ONLY | RECURSIVE); delete(dst, EMPTY_DIRECTORIES_ONLY | RECURSIVE);
} }
// On *nix there is no try, you do or do not // On *nix there is no try, you do or do not
Files.move(src.toPath(), dst.toPath(), options); Files.move(toPath(src), toPath(dst), options);
return; return;
} catch (IOException e2) { } catch (IOException e2) {
// ignore and continue retry // ignore and continue retry
@ -408,7 +428,7 @@ public class FileUtils {
*/ */
public static Path createSymLink(File path, String target) public static Path createSymLink(File path, String target)
throws IOException { throws IOException {
Path nioPath = path.toPath(); Path nioPath = toPath(path);
if (Files.exists(nioPath, LinkOption.NOFOLLOW_LINKS)) { if (Files.exists(nioPath, LinkOption.NOFOLLOW_LINKS)) {
BasicFileAttributes attrs = Files.readAttributes(nioPath, BasicFileAttributes attrs = Files.readAttributes(nioPath,
BasicFileAttributes.class, LinkOption.NOFOLLOW_LINKS); BasicFileAttributes.class, LinkOption.NOFOLLOW_LINKS);
@ -421,7 +441,7 @@ public class FileUtils {
if (SystemReader.getInstance().isWindows()) { if (SystemReader.getInstance().isWindows()) {
target = target.replace('/', '\\'); target = target.replace('/', '\\');
} }
Path nioTarget = new File(target).toPath(); Path nioTarget = toPath(new File(target));
return Files.createSymbolicLink(nioPath, nioTarget); return Files.createSymbolicLink(nioPath, nioTarget);
} }
@ -432,7 +452,7 @@ public class FileUtils {
* @since 3.0 * @since 3.0
*/ */
public static String readSymLink(File path) throws IOException { public static String readSymLink(File path) throws IOException {
Path nioPath = path.toPath(); Path nioPath = toPath(path);
Path target = Files.readSymbolicLink(nioPath); Path target = Files.readSymbolicLink(nioPath);
String targetString = target.toString(); String targetString = target.toString();
if (SystemReader.getInstance().isWindows()) { if (SystemReader.getInstance().isWindows()) {
@ -644,7 +664,7 @@ public class FileUtils {
* @throws IOException * @throws IOException
*/ */
static long lastModified(File file) throws IOException { static long lastModified(File file) throws IOException {
return Files.getLastModifiedTime(file.toPath(), LinkOption.NOFOLLOW_LINKS) return Files.getLastModifiedTime(toPath(file), LinkOption.NOFOLLOW_LINKS)
.toMillis(); .toMillis();
} }
@ -654,7 +674,7 @@ public class FileUtils {
* @throws IOException * @throws IOException
*/ */
static void setLastModified(File file, long time) throws IOException { static void setLastModified(File file, long time) throws IOException {
Files.setLastModifiedTime(file.toPath(), FileTime.fromMillis(time)); Files.setLastModifiedTime(toPath(file), FileTime.fromMillis(time));
} }
/** /**
@ -672,7 +692,7 @@ public class FileUtils {
* @throws IOException * @throws IOException
*/ */
static boolean isHidden(File file) throws IOException { static boolean isHidden(File file) throws IOException {
return Files.isHidden(file.toPath()); return Files.isHidden(toPath(file));
} }
/** /**
@ -682,7 +702,7 @@ public class FileUtils {
* @since 4.1 * @since 4.1
*/ */
public static void setHidden(File file, boolean hidden) throws IOException { public static void setHidden(File file, boolean hidden) throws IOException {
Files.setAttribute(file.toPath(), "dos:hidden", Boolean.valueOf(hidden), //$NON-NLS-1$ Files.setAttribute(toPath(file), "dos:hidden", Boolean.valueOf(hidden), //$NON-NLS-1$
LinkOption.NOFOLLOW_LINKS); LinkOption.NOFOLLOW_LINKS);
} }
@ -693,7 +713,7 @@ public class FileUtils {
* @since 4.1 * @since 4.1
*/ */
public static long getLength(File file) throws IOException { public static long getLength(File file) throws IOException {
Path nioPath = file.toPath(); Path nioPath = toPath(file);
if (Files.isSymbolicLink(nioPath)) if (Files.isSymbolicLink(nioPath))
return Files.readSymbolicLink(nioPath).toString() return Files.readSymbolicLink(nioPath).toString()
.getBytes(Constants.CHARSET).length; .getBytes(Constants.CHARSET).length;
@ -737,7 +757,7 @@ public class FileUtils {
*/ */
static Attributes getFileAttributesBasic(FS fs, File file) { static Attributes getFileAttributesBasic(FS fs, File file) {
try { try {
Path nioPath = file.toPath(); Path nioPath = toPath(file);
BasicFileAttributes readAttributes = nioPath BasicFileAttributes readAttributes = nioPath
.getFileSystem() .getFileSystem()
.provider() .provider()
@ -769,7 +789,7 @@ public class FileUtils {
*/ */
public static Attributes getFileAttributesPosix(FS fs, File file) { public static Attributes getFileAttributesPosix(FS fs, File file) {
try { try {
Path nioPath = file.toPath(); Path nioPath = toPath(file);
PosixFileAttributes readAttributes = nioPath PosixFileAttributes readAttributes = nioPath
.getFileSystem() .getFileSystem()
.provider() .provider()

Loading…
Cancel
Save