/** * @param file * @return lastModified attribute for given file, not following symbolic * links * @throws IOException */ static long lastModified(File file) throws IOException { return Files.getLastModifiedTime(toPath(file), LinkOption.NOFOLLOW_LINKS) .toMillis(); }
/** * @param file * @return {@code true} if the given file is hidden * @throws IOException */ static boolean isHidden(File file) throws IOException { return Files.isHidden(toPath(file)); }
/** * Set a file hidden (on Windows) * * @param file * a {@link java.io.File} object. * @param hidden * a boolean. * @throws java.io.IOException * @since 4.1 */ public static void setHidden(File file, boolean hidden) throws IOException { Files.setAttribute(toPath(file), "dos:hidden", Boolean.valueOf(hidden), //$NON-NLS-1$ LinkOption.NOFOLLOW_LINKS); }
/** * Get file length * * @param file * a {@link java.io.File}. * @return length of the given file * @throws java.io.IOException * @since 4.1 */ public static long getLength(File file) throws IOException { Path nioPath = toPath(file); if (Files.isSymbolicLink(nioPath)) return Files.readSymbolicLink(nioPath).toString() .getBytes(UTF_8).length; return Files.size(nioPath); }
/** * @param file * @param time * @throws IOException */ static void setLastModified(File file, long time) throws IOException { Files.setLastModifiedTime(toPath(file), FileTime.fromMillis(time)); }
private boolean autoGcBlockedByOldLockFile() { try { FileTime lastModified = Files.getLastModifiedTime(FileUtils.toPath(logFile)); if (lastModified.toInstant().compareTo(getLogExpiry()) > 0) { // There is an existing log file, which is too recent to ignore return true; } } catch (NoSuchFileException e) { // No existing log file, OK. } catch (IOException | ParseException e) { throw new JGitInternalException(e.getMessage(), e); } return false; }
/** * Read target path of the symlink. * * @param path * a {@link java.io.File} object. * @return target path of the symlink, or null if it is not a symbolic link * @throws java.io.IOException * @since 3.0 */ public static String readSymLink(File path) throws IOException { Path nioPath = toPath(path); Path target = Files.readSymbolicLink(nioPath); String targetString = target.toString(); if (SystemReader.getInstance().isWindows()) { targetString = targetString.replace('\\', '/'); } else if (SystemReader.getInstance().isMacOS()) { targetString = Normalizer.normalize(targetString, Form.NFC); } return targetString; }
/** * Create a symbolic link * * @param path * the path of the symbolic link to create * @param target * the target of the symbolic link * @return the path to the symbolic link * @throws java.io.IOException * @since 4.2 */ public static Path createSymLink(File path, String target) throws IOException { Path nioPath = toPath(path); if (Files.exists(nioPath, LinkOption.NOFOLLOW_LINKS)) { BasicFileAttributes attrs = Files.readAttributes(nioPath, BasicFileAttributes.class, LinkOption.NOFOLLOW_LINKS); if (attrs.isRegularFile() || attrs.isSymbolicLink()) { delete(path); } else { delete(path, EMPTY_DIRECTORIES_ONLY | RECURSIVE); } } if (SystemReader.getInstance().isWindows()) { target = target.replace('/', '\\'); } Path nioTarget = toPath(new File(target)); return Files.createSymbolicLink(nioPath, nioTarget); }
/** {@inheritDoc} */ @Override public boolean setExecute(File f, boolean canExecute) { if (!isFile(f)) return false; if (!canExecute) return f.setExecutable(false, false); try { Path path = FileUtils.toPath(f); Set<PosixFilePermission> pset = Files.getPosixFilePermissions(path); // owner (user) is always allowed to execute. pset.add(PosixFilePermission.OWNER_EXECUTE); int mask = umask(); apply(pset, mask, PosixFilePermission.GROUP_EXECUTE, 1 << 3); apply(pset, mask, PosixFilePermission.OTHERS_EXECUTE, 1); Files.setPosixFilePermissions(path, pset); return true; } catch (IOException e) { // The interface doesn't allow to throw IOException final boolean debug = Boolean.parseBoolean(SystemReader .getInstance().getProperty("jgit.fs.debug")); //$NON-NLS-1$ if (debug) System.err.println(e); return false; } }
Path nioPath = toPath(file); PosixFileAttributes readAttributes = nioPath .getFileSystem()
/** * @param fs * @param file * @return non null attributes object */ static Attributes getFileAttributesBasic(FS fs, File file) { try { Path nioPath = toPath(file); BasicFileAttributes readAttributes = nioPath .getFileSystem() .provider() .getFileAttributeView(nioPath, BasicFileAttributeView.class, LinkOption.NOFOLLOW_LINKS).readAttributes(); Attributes attributes = new Attributes(fs, file, true, readAttributes.isDirectory(), fs.supportsExecute() ? file.canExecute() : false, readAttributes.isSymbolicLink(), readAttributes.isRegularFile(), // readAttributes.creationTime().toMillis(), // readAttributes.lastModifiedTime().toMillis(), readAttributes.isSymbolicLink() ? Constants .encode(readSymLink(file)).length : readAttributes.size()); return attributes; } catch (IOException e) { return new Attributes(file, fs); } }
/** * @param file * @return lastModified attribute for given file, not following symbolic * links * @throws IOException */ static long lastModified(File file) throws IOException { return Files.getLastModifiedTime(toPath(file), LinkOption.NOFOLLOW_LINKS) .toMillis(); }
/** * @param file * @return {@code true} if the given file is hidden * @throws IOException */ static boolean isHidden(File file) throws IOException { return Files.isHidden(toPath(file)); }
/** * Set a file hidden (on Windows) * * @param file * a {@link java.io.File} object. * @param hidden * a boolean. * @throws java.io.IOException * @since 4.1 */ public static void setHidden(File file, boolean hidden) throws IOException { Files.setAttribute(toPath(file), "dos:hidden", Boolean.valueOf(hidden), //$NON-NLS-1$ LinkOption.NOFOLLOW_LINKS); }
/** * Get file length * * @param file * a {@link java.io.File}. * @return length of the given file * @throws java.io.IOException * @since 4.1 */ public static long getLength(File file) throws IOException { Path nioPath = toPath(file); if (Files.isSymbolicLink(nioPath)) return Files.readSymbolicLink(nioPath).toString() .getBytes(UTF_8).length; return Files.size(nioPath); }
/** * @param file * @param time * @throws IOException */ static void setLastModified(File file, long time) throws IOException { Files.setLastModifiedTime(toPath(file), FileTime.fromMillis(time)); }
private boolean autoGcBlockedByOldLockFile() { try { FileTime lastModified = Files.getLastModifiedTime(FileUtils.toPath(logFile)); if (lastModified.toInstant().compareTo(getLogExpiry()) > 0) { // There is an existing log file, which is too recent to ignore return true; } } catch (NoSuchFileException e) { // No existing log file, OK. } catch (IOException | ParseException e) { throw new JGitInternalException(e.getMessage(), e); } return false; }
/** * Read target path of the symlink. * * @param path * a {@link java.io.File} object. * @return target path of the symlink, or null if it is not a symbolic link * @throws java.io.IOException * @since 3.0 */ public static String readSymLink(File path) throws IOException { Path nioPath = toPath(path); Path target = Files.readSymbolicLink(nioPath); String targetString = target.toString(); if (SystemReader.getInstance().isWindows()) { targetString = targetString.replace('\\', '/'); } else if (SystemReader.getInstance().isMacOS()) { targetString = Normalizer.normalize(targetString, Form.NFC); } return targetString; }