if (nfsFile.isDirectory()) { synchronized (this) { F metaDir = getMetaFile(nfsFile).getParentFile(); try { if (metaDir.exists()) { metaDir.delete(); nfsFile.delete(); } catch (IOException e) { log.warn("failed to delete directory {}", nfsFile); if ((deleteOlderThan > 0) && ((System.currentTimeMillis() - nfsFile.lastModified()) < deleteOlderThan)) { log.info("not deleting {}; it is not at least {} ms old", nfsFile, deleteOlderThan); } else { log.debug("deleting {}", nfsFile); nfsFile.delete();
public String getIdentifier(String relativePath, boolean directory) { try { return StringUtils.isBlank(relativePath) ? identifierBase : syncRootFile.getChildFile(relativePath).getAbsolutePath(); } catch (IOException e) { throw new RuntimeException(e); } }
/** * Create a symlink if needed. * * @param nfsFile the nfsFile * @param targetPath the symlink target * @throws IOException */ private void createSymLink(F nfsFile, String targetPath) throws IOException { boolean needNewSymLink = true; try { if ((nfsFile.getAttributes().getType() == NfsType.NFS_LNK) && targetPath.equals(nfsFile.readlink().getData())) { needNewSymLink = false; } else { nfsFile.delete(); } } catch (Throwable t) { // do nothing, this is normal if the nfsFile doesn't exist. } if (needNewSymLink) { nfsFile.symlink(targetPath, new NfsSetAttributes()); } }
/** * Get the nfsFile holding the metadata for this nfsFile. * * @param nfsFile the nfsFile * @return the nfsFile holding the metadata * @throws IOException */ private F getMetaFile(F nfsFile) throws IOException { try { if (!nfsFile.isDirectory()) { return nfsFile.getParentFile().newChildFile(ObjectMetadata.METADATA_DIR).newChildFile(nfsFile.getName()); } else { return nfsFile.newChildFile(ObjectMetadata.METADATA_DIR).newChildFile(ObjectMetadata.DIR_META_FILE); } } catch (IOException e) { throw new ConfigurationException(e); } }
/** * Create an ObjectSummary for the nfsFile. * * @param nfsFile the nfsFile * @return the summary */ private ObjectSummary createSummary(F nfsFile) { try { if (!nfsFile.exists()) { throw new ObjectNotFoundException(nfsFile.getPath()); } boolean link = isSymLink(nfsFile); boolean directory = nfsFile.isDirectory() && (config.isFollowLinks() || !link); long size = directory || link ? 0 : nfsFile.length(); return new ObjectSummary(nfsFile.getAbsolutePath(), directory, size); } catch (IOException e) { throw new ConfigurationException(e); } }
if (config.isStoreMetadata()) { F metaFile = getMetaFile(nfsFile); F metaDir = metaFile.getParentFile(); if (!metaDir.exists()) { metaDir.mkdirs(); nfsFile.setLastModified(mtime.getTime()); NfsSetAttributes atimeAttributes = new NfsSetAttributes(); atimeAttributes.setAtime(new NfsTime(atime.getTime())); nfsFile.setAttributes(atimeAttributes);
boolean directory = nfsFile.isDirectory(); NfsGetAttributes basicAttr = nfsFile.getattr().getAttributes(); NfsTime mtime = basicAttr.getMtime(); long mtimeInMillis = (mtime == null) ? 0 : mtime.getTimeInMillis(); metadata.setMetaChangeTime(new Date(ctimeInMillis)); metadata.setContentType(isLink ? TYPE_LINK : mimeMap.getContentType(nfsFile.getName())); if (isLink) metadata.setUserMetadataValue(META_LINK_TARGET, nfsFile.readlink().getData()); if (nfsFile.isFile() && !isLink) metadata.setContentLength(nfsFile.length()); else metadata.setContentLength(0);
testDirectory.mkdir(); sourceDirectory = testDirectory.getChildFile(sourceDirectoryName); sourceDirectory.mkdir(); sourceFile = sourceDirectory.getChildFile(testFileName); sourceFile.createNewFile(); int size = 100 * 1024; NfsFileOutputStream outputStream = new NfsFileOutputStream(sourceFile); targetDirectory = testDirectory.getChildFile(targetDirectoryName); targetDirectory.mkdir(); targetFile = targetDirectory.getChildFile(testFileName);
try { F mountPathRoot = createFileFromPath(""); if (!mountPathRoot.exists()) { throw new ConfigurationException("the mount " + mountPathRoot + " is unavailable."); identifierBase = syncRootFile.getAbsolutePath(); } catch (IOException e) { throw new ConfigurationException(e); if (!syncRootFile.exists()) { throw new ConfigurationException("the source " + syncRootFile + " does not exist.");
/** * Sync the nfsFile. * * @param nfsFile the nfsFile * @param object the SyncObject * @param streamData whether to sync data after nfsFile creation * @throws IOException */ private void writeFile(F nfsFile, SyncObject object, boolean streamData) throws IOException { // make sure parent directory exists mkdirs(nfsFile.getParentFile()); if (object.getMetadata().isDirectory()) { mkdirs(nfsFile); } else if (TYPE_LINK.equals(object.getMetadata().getContentType())) { // restore // a // sym // link String targetPath = object.getMetadata().getUserMetadataValue(META_LINK_TARGET); if (targetPath == null) { throw new RuntimeException("object appears to be a symbolic link, but no target path was found"); } log.info("re-creating symbolic link {} -> {}", object.getRelativePath(), targetPath); createSymLink(nfsFile, targetPath); } else if (streamData) { copyData(object.getDataStream(), nfsFile); } else { nfsFile.createNewFile(); } }
attributes.setUid(Long.parseLong(ownerName.substring(4))); } else { log.info("{} does not have a UID assigned", nfsFile.getPath()); log.info("{} does not have a GID assigned", nfsFile.getPath()); nfsFile.setAttributes(attributes); } catch (IOException e) { throw new RuntimeException("could not write nfsFile attributes for " + nfsFile.getPath(), e);
/** * Delete the nfsFile. * @param identifier the nfsFile identifier * @param deleteOlderThan the minimum age for deletion in milliseconds, or 0 if none. * @throws IOException */ public void delete(String identifier, long deleteOlderThan) throws IOException { F objectFile = createFile(identifier); F metaFile = getMetaFile(objectFile); if (metaFile.exists()) { delete(metaFile, deleteOlderThan); } delete(objectFile, deleteOlderThan); }
/** * Is the nfsFile a symbolic link? * * @param nfsFile the nfsFile * @return true if it is, false if not * @throws IOException */ private boolean isSymLink(F nfsFile) throws IOException { return nfsFile.getattr().getAttributes().getType() == NfsType.NFS_LNK; }
NfsGetAttributes attributes = null; try { attributes = nfsFile.getAttributes(); } catch (Throwable t) { throw new RuntimeException("could not read nfsFile ACL", t);