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>
tags/v4.10.0.201712302008-r
Marc Strapetz 6 years ago
parent
commit
9bb126d12d

+ 1
- 1
org.eclipse.jgit.lfs/src/org/eclipse/jgit/lfs/CleanFilter.java View File

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());

+ 2
- 1
org.eclipse.jgit.lfs/src/org/eclipse/jgit/lfs/SmudgeFilter.java View File

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
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());

+ 2
- 1
org.eclipse.jgit.test/exttst/org/eclipse/jgit/ignore/CGitVsJGitRandomIgnorePatternTest.java View File

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;


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,

+ 1
- 1
org.eclipse.jgit/src/org/eclipse/jgit/internal/storage/file/GC.java View File

} 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);
} }

+ 3
- 2
org.eclipse.jgit/src/org/eclipse/jgit/internal/storage/file/GcLog.java View File

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;




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);

+ 2
- 2
org.eclipse.jgit/src/org/eclipse/jgit/internal/storage/file/ObjectDirectory.java View File

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);
// //
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);

+ 1
- 1
org.eclipse.jgit/src/org/eclipse/jgit/util/FS_POSIX.java View File

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.

+ 32
- 12
org.eclipse.jgit/src/org/eclipse/jgit/util/FileUtils.java View File

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;
*/ */
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
* *
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;
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
*/ */
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);
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);
} }


* @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()) {
* @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();
} }


* @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));
} }


/** /**
* @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));
} }


/** /**
* @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);
} }


* @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;
*/ */
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()
*/ */
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