static void checkBasicAttributes(BasicFileAttributes attrs1, BasicFileAttributes attrs2) { // check file type assertTrue(attrs1.isRegularFile() == attrs2.isRegularFile()); assertTrue(attrs1.isDirectory() == attrs2.isDirectory()); assertTrue(attrs1.isSymbolicLink() == attrs2.isSymbolicLink()); assertTrue(attrs1.isOther() == attrs2.isOther()); // check last modified time long time1 = attrs1.lastModifiedTime().toMillis(); long time2 = attrs2.lastModifiedTime().toMillis(); assertTrue(time1 == time2); // check size if (attrs1.isRegularFile()) assertTrue(attrs1.size() == attrs2.size()); }
private ZonedDateTime getLastModifiedDate(Path resourcePath) { if (!Files.isReadable(resourcePath)) { throw new ResourceNotFoundException( "Static resource file '" + resourcePath + "' is not readable."); } BasicFileAttributes fileAttributes; try { fileAttributes = Files.readAttributes(resourcePath, BasicFileAttributes.class); } catch (NoSuchFileException | FileNotFoundException e) { // This shouldn't be happening because we checked the file's readability before. But just in // case. throw new ResourceNotFoundException( "Static resource file '" + resourcePath + "' does not exists.", e); } catch (Exception e) { // UnsupportedOperationException, IOException or any other Exception that might occur. throw new FileOperationException( "Cannot read file attributes from static resource file '" + resourcePath + "'.", e); } if (fileAttributes.isRegularFile()) { return ZonedDateTime.ofInstant(fileAttributes.lastModifiedTime().toInstant(), GMT_TIME_ZONE); } else { /* * From book "OCP: Oracle Certified Professional Java SE 8 Programmer II Study Guide" page 478: * Java defines a regular file as one that contains content, as opposed to a symbolic link, * directory, resource (e.g. port, pipe), or other non-regular files that may be present in some * operating systems. [...] It is possible for isRegularFile() to return true for a symbolic link, * as long as the link resolves to a regular file. * Hence, checking 'isRegularFile' of a file is enough to determine its existence and not being a directory. */ throw new ResourceNotFoundException( "Static resource file '" + resourcePath + "' is not a regular file."); } }
public final FileTime lastModified() throws IOException { if (exists()) { BasicFileAttributes attrs = Files.readAttributes(getPath(), BasicFileAttributes.class, options); return attrs.lastModifiedTime(); } else { return null; } }
private boolean isCacheEntryExpired(Path cacheEntryPath, long durationToExpireMs) throws IOException { BasicFileAttributes attr = Files.readAttributes(cacheEntryPath, BasicFileAttributes.class); long modTime = attr.lastModifiedTime().toMillis(); long age = System.currentTimeMillis() - modTime; if (age > durationToExpireMs) { return true; } return false; }
static void checkBasicAttributes(BasicFileAttributes attrs1, BasicFileAttributes attrs2) { // check file type assertTrue(attrs1.isRegularFile() == attrs2.isRegularFile()); assertTrue(attrs1.isDirectory() == attrs2.isDirectory()); assertTrue(attrs1.isSymbolicLink() == attrs2.isSymbolicLink()); assertTrue(attrs1.isOther() == attrs2.isOther()); // check last modified time if not a symbolic link if (!attrs1.isSymbolicLink()) { long time1 = attrs1.lastModifiedTime().to(TimeUnit.SECONDS); long time2 = attrs2.lastModifiedTime().to(TimeUnit.SECONDS); if (time1 != time2) { System.err.format("File time for %s is %s\n", attrs1.fileKey(), attrs1.lastModifiedTime()); System.err.format("File time for %s is %s\n", attrs2.fileKey(), attrs2.lastModifiedTime()); assertTrue(false); } } // check size if (attrs1.isRegularFile()) assertTrue(attrs1.size() == attrs2.size()); }
@Override protected Long getDefaultPositionPerFile(File file) throws IOException { if (this.defaultTimestampIsFileUpdated) { BasicFileAttributeView fileAttributeView = Files.getFileAttributeView(file.toPath(), BasicFileAttributeView.class); BasicFileAttributes readAttributes = fileAttributeView.readAttributes(); return AbsoluteTimeGranularityUtil.asPosition( new Date(readAttributes.lastModifiedTime().toMillis())); } else if (this.defaultDate != null) { return this.defaultDate.getTime(); } else { return super.getDefaultPositionPerFile(file); } }
public final void update(File[] xml) { if (xml != null || xml.length != 0) { data = new String[xml.length][columnNames.length]; try { for (int i = 0; i < xml.length; i++) { if (xml[i].isFile()) { BasicFileAttributes attrs = Files.readAttributes(xml[i].toPath(), BasicFileAttributes.class); data[i][0] = xml[i].getName(); data[i][1] = String.format("%d B", attrs.size()); data[i][2] = String.format("%1$tY/%1$tm/%1$td", new Date(attrs.creationTime().toMillis())); data[i][3] = String.format("%1$tY/%1$tm/%1$td", new Date(attrs.lastModifiedTime().toMillis())); } } rows = xml.length; fireTableStructureChanged(); } catch (IOException ex) { System.err.println(ex.getMessage()); } } else JOptionPane.showMessageDialog(null, "No existen facturas de consumidor final"); }
public static synchronized FileInfo getFileInfo(String name) { int version = getCurrentVersion(name, null); Path dataFilePath = Paths.get(Config.cachePath, getDataFileName(name, version)); Path headerFilePath = Paths.get(Config.cachePath, getHeadersFileName(name, version)); Date fileLastModifiedTime; BasicFileAttributes attrs; try { attrs = Files.readAttributes(dataFilePath, BasicFileAttributes.class); fileLastModifiedTime = new Date(attrs.lastModifiedTime().toMillis()); } catch (Exception e) { Log.error("Cannot read file attributes", e.getCause() + ": " + e.getMessage()); e.printStackTrace(); return null; } // read headers ArrayList<String> headers = loadHeaders(headerFilePath.toFile()); String url = null; String etag = null; for (String header : headers) { if (header.startsWith(HttpConstants.HEADER_FILE_URL + ":")) { url = header.split(":", 2)[1].trim(); } else if (header.startsWith(HttpConstants.HEADER_ETAG + ":")) { etag = header.split(":", 2)[1].trim(); } } if (url != null) return new FileInfo(url, fileLastModifiedTime, etag); else return null; }
public FileAttrs getAttrs() throws IOException { FileAttrs result; BasicFileAttributes attr; DosFileAttributes dosAttr; PosixFileAttributes posixAttr; result = new FileAttrs(); attr = Files.readAttributes(this.path.toPath(), BasicFileAttributes.class); result.setCtime(attr.creationTime().toMillis()); result.setMtime(attr.lastModifiedTime().toMillis()); // result.append("symlink", attr.isSymbolicLink()); //Redundant result.setSize(attr.size()); if (System.getProperty("os.name").startsWith("Windows")) { dosAttr = Files.readAttributes(this.path.toPath(), DosFileAttributes.class); result.setDosArchive(dosAttr.isArchive()); result.setDosHidden(dosAttr.isHidden()); result.setDosReadonly(dosAttr.isReadOnly()); result.setDosSystem(dosAttr.isSystem()); } else { posixAttr = Files.readAttributes(this.path.toPath(), PosixFileAttributes.class); result.setPosixSymlink(this.isSymlink()); if (result.getPosixSymlink()) { result.setLinkTo(Files.readSymbolicLink(this.path.toPath()).toString()); } result.setPosixOwner(posixAttr.owner().getName()); result.setPosixGroup(posixAttr.group().getName()); result.setPosixPermission(PosixFilePermissions.toString(posixAttr.permissions())); } return result; }
/** * Delete older logs from disk. * * @param time The minimum age of the logs. */ public void recycle(long time) { List<Path> toRecycle = new ArrayList<>(); // Check if the log directory exists. If it doesn't // then there isn't anything to recycle. Path logDir = Paths.get(db.getLogPath()).toAbsolutePath(); if (!Files.exists(logDir)) { return; } try { DirectoryStream<Path> stream = Files.newDirectoryStream(logDir, "log_*"); for (Path entry : stream) { // System.out.printf("investigating log %s\n", entry); // Do not include the current block. if (logPath == null || !Files.isSameFile(entry, logPath)) { BasicFileAttributes view = Files.getFileAttributeView(entry, BasicFileAttributeView.class).readAttributes(); if (view.lastModifiedTime().toMillis() < time) { // We no longer need this entry. Add to our // recycling list. toRecycle.add(entry); } } } // Now delete all the values. for (Path p : toRecycle) { Files.delete(p); } } catch (IOException e) { e.printStackTrace(); } }
protected List<PluginInfo<T>> scan( LogSource logger, String hosterpath, final List<? extends LazyPlugin<T>> pluginCache, final AtomicLong lastFolderModification) throws Exception { DirectoryStream<Path> stream = null; final ArrayList<PluginInfo<T>> ret = new ArrayList<PluginInfo<T>>(); final long timeStamp = System.currentTimeMillis(); try { long lastModified = lastFolderModification != null ? lastFolderModification.get() : -1; final Path folder = Application.getRootByClass(jd.SecondLevelLaunch.class, hosterpath).toPath(); final long lastFolderModified = Files.readAttributes(folder, BasicFileAttributes.class).lastModifiedTime().toMillis(); if (lastModified > 0 && lastFolderModified == lastModified && pluginCache != null && pluginCache.size() > 0) { for (final LazyPlugin<T> lazyPlugin : pluginCache) { final PluginInfo<T> pluginInfo = new PluginInfo<T>(lazyPlugin.getLazyPluginClass(), null); pluginInfo.setLazyPlugin(lazyPlugin); ret.add(pluginInfo); } return ret; } PluginClassLoaderChild cl = null; MessageDigest md = null; final String pkg = hosterpath.replace("/", "."); final byte[] mdCache = new byte[32767]; final HashMap<String, List<LazyPlugin<T>>> lazyPluginClassMap; if (pluginCache != null && pluginCache.size() > 0) { lazyPluginClassMap = new HashMap<String, List<LazyPlugin<T>>>(); for (final LazyPlugin<T> lazyPlugin : pluginCache) { List<LazyPlugin<T>> list = lazyPluginClassMap.get(lazyPlugin.getLazyPluginClass().getClassName()); if (list == null) { list = new ArrayList<LazyPlugin<T>>(); lazyPluginClassMap.put(lazyPlugin.getLazyPluginClass().getClassName(), list); } list.add(lazyPlugin); } } else { lazyPluginClassMap = null; } if (lastFolderModification != null) { lastFolderModification.set(lastFolderModified); } stream = Files.newDirectoryStream(folder, "*.class"); for (final Path path : stream) { try { final String pathFileName = path.getFileName().toString(); final String className = pathFileName.substring(0, pathFileName.length() - 6); if (className.indexOf("$") < 0 && !PluginController.IGNORELIST.contains(className)) { byte[] sha256 = null; final BasicFileAttributes pathAttr = Files.readAttributes(path, BasicFileAttributes.class); if (lazyPluginClassMap != null) { final List<LazyPlugin<T>> lazyPlugins = lazyPluginClassMap.get(className); if (lazyPlugins != null && lazyPlugins.size() > 0) { final LazyPluginClass lazyPluginClass = lazyPlugins.get(0).getLazyPluginClass(); if (lazyPluginClass != null && (lazyPluginClass.getLastModified() == pathAttr.lastModifiedTime().toMillis() || ((md = MessageDigest.getInstance("SHA-256")) != null && (sha256 = PluginController.getFileHashBytes(path.toFile(), md, mdCache)) != null && Arrays.equals(sha256, lazyPluginClass.getSha256())))) { for (final LazyPlugin<T> lazyPlugin : lazyPlugins) { // logger.finer("Cached: " + className + "|" + lazyPlugin.getDisplayName() + "|" // + // lazyPluginClass.getRevision()); final PluginInfo<T> pluginInfo = new PluginInfo<T>(lazyPluginClass, null); pluginInfo.setLazyPlugin(lazyPlugin); ret.add(pluginInfo); } continue; } } } Class<T> pluginClass = null; long[] infos = null; try { if (cl == null) { cl = PluginClassLoader.getInstance().getChild(); } if (md == null) { md = MessageDigest.getInstance("SHA-256"); } pluginClass = (Class<T>) cl.loadClass(pkg + "." + className); if (!Modifier.isAbstract(pluginClass.getModifiers()) && Plugin.class.isAssignableFrom(pluginClass)) { infos = getPluginController().getInfos(pluginClass); if (infos == null) { continue; } } else { continue; } } catch (final Throwable e) { logger.finer("Failed: " + className); logger.log(e); continue; } if (sha256 == null) { sha256 = PluginController.getFileHashBytes(path.toFile(), md, mdCache); } // final LazyPluginClass lazyPluginClass = new LazyPluginClass( className, sha256, pathAttr.lastModifiedTime().toMillis(), (int) infos[0], infos[1]); final PluginInfo<T> pluginInfo = new PluginInfo<T>(lazyPluginClass, pluginClass); // logger.finer("Scaned: " + className + "|" + lazyPluginClass.getRevision()); ret.add(pluginInfo); } } catch (Throwable e) { logger.finer("Failed: " + path); logger.log(e); } } return ret; } finally { logger.info( "@PluginController(NIO): scan took " + (System.currentTimeMillis() - timeStamp) + "ms for " + ret.size()); if (stream != null) { stream.close(); } } }
@Override public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException { Path relFilePath = m_srcRoot.relativize(file); if (isSrcIgnored(relFilePath)) { m_progress.reportProgress(CloneProgressReporter.EventType.FILE_SKIP_IGNORE, file, null, null); return FileVisitResult.CONTINUE; } Path newPathCandidate = m_destRoot.resolve(relFilePath); Iterator<FileHandler> it = m_fileHandlers.iterator(); while (it.hasNext()) { FileHandler handler = it.next(); try { Path newPath = handler.canHandleFile(file, attrs, newPathCandidate); if (newPath != null) { // record the visited file even if we don't process it recordVisitedFile(file, newPath); // does the new path exist if (Files.exists(newPath, LinkOption.NOFOLLOW_LINKS)) { // if it's the same file, then skip if (Files.isSameFile(file, newPath)) { m_progress.reportProgress( CloneProgressReporter.EventType.FILE_SKIP_SAME, file, newPath, null); } else { // process file BasicFileAttributes targetAttrs = Files.readAttributes( newPath, BasicFileAttributes.class, LinkOption.NOFOLLOW_LINKS); if (targetAttrs.lastModifiedTime().compareTo(attrs.lastModifiedTime()) < 0) { m_progress.reportProgress( CloneProgressReporter.EventType.FILE_UPDATE_EXISTING_BEGIN, file, newPath, null); handler.processFile(file, attrs, newPathCandidate, m_runType); m_progress.reportProgress( CloneProgressReporter.EventType.FILE_UPDATE_EXISTING_END, file, newPath, null); } else { m_progress.reportProgress( CloneProgressReporter.EventType.FILE_SKIP_NEWER, file, newPath, null); } } } else { // otherwise process it m_progress.reportProgress( CloneProgressReporter.EventType.FILE_CREATE_NEW_BEGIN, file, newPath, null); handler.processFile(file, attrs, newPathCandidate, m_runType); m_progress.reportProgress( CloneProgressReporter.EventType.FILE_CREATE_NEW_END, file, newPath, null); } // file has been handled break; } } catch (FileCloningError e) { m_progress.reportProgress(CloneProgressReporter.EventType.ERROR, file, null, e); // TODO: should we continue and try next handler in list? } } return FileVisitResult.CONTINUE; }