private static Iterable<AttributeProvider> getProviders(Configuration configuration) { Map<String, AttributeProvider> result = new HashMap<>(); for (AttributeProvider provider : configuration.attributeProviders) { result.put(provider.name(), provider); } for (String view : configuration.attributeViews) { addStandardProvider(result, view); } addMissingProviders(result); return Collections.unmodifiableCollection(result.values()); }
/** * Creates a copy of the given file, copying its attributes as well according to the given * {@code attributeCopyOption}. */ File copyWithoutContent(File file, AttributeCopyOption attributeCopyOption) throws IOException { File copy = factory.copyWithoutContent(file); setInitialAttributes(copy); attributes.copyAttributes(file, copy, attributeCopyOption); return copy; }
private FileAttributeView getFileAttributeView( FileLookup lookup, Class<? extends FileAttributeView> viewType, Map<String, FileAttributeView> inheritedViews) { AttributeProvider provider = providersByViewType.get(viewType); createInheritedViews(lookup, provider, inheritedViews); return provider.view(lookup, ImmutableMap.copyOf(inheritedViews)); }
/** * Gets the value of the given attribute for the given file. {@code attribute} must be of the * form "view:attribute" or "attribute". */ public Object getAttribute(File file, String attribute) { String view = getViewName(attribute); String attr = getSingleAttribute(attribute); return getAttribute(file, view, attr); }
/** * Implements {@link Files#readAttributes(Path, String, LinkOption...)}. */ public ImmutableMap<String, Object> readAttributes(File file, String attributes) { String view = getViewName(attributes); List<String> attrs = getAttributeNames(attributes); if (attrs.size() > 1 && attrs.contains(ALL_ATTRIBUTES)) { // attrs contains * and other attributes throw new IllegalArgumentException("invalid attributes: " + attributes); } Map<String, Object> result = new HashMap<>(); if (attrs.size() == 1 && attrs.contains(ALL_ATTRIBUTES)) { // for 'view:*' format, get all keys for all providers for the view AttributeProvider provider = providersByName.get(view); readAll(file, provider, result); for (String inheritedView : provider.inherits()) { AttributeProvider inheritedProvider = providersByName.get(inheritedView); readAll(file, inheritedProvider, result); } } else { // for 'view:attr1,attr2,etc' for (String attr : attrs) { result.put(attr, getAttribute(file, view, attr)); } } return ImmutableMap.copyOf(result); }
/** * Creates the file store for the file system. */ private static JimfsFileStore createFileStore( Configuration config, PathService pathService, FileSystemState state) { AttributeService attributeService = new AttributeService(config); // TODO(cgdecker): Make disk values configurable HeapDisk disk = new HeapDisk(config); FileFactory fileFactory = new FileFactory(disk); Map<Name, Directory> roots = new HashMap<>(); // create roots for (String root : config.roots) { JimfsPath path = pathService.parsePath(root); if (!path.isAbsolute() && path.getNameCount() == 0) { throw new IllegalArgumentException("Invalid root path: " + root); } Name rootName = path.root(); Directory rootDir = fileFactory.createRootDirectory(rootName); attributeService.setInitialAttributes(rootDir); roots.put(rootName, rootDir); } return new JimfsFileStore( new FileTree(roots), fileFactory, disk, attributeService, config.supportedFeatures, state); }
/** * Returns an attribute view of the given type for the given file lookup callback, or * {@code null} if the view type is not supported. */ @Nullable <V extends FileAttributeView> V getFileAttributeView(FileLookup lookup, Class<V> type) { state.checkOpen(); return attributes.getFileAttributeView(lookup, type); }
/** * Gets the value of the given attribute for the given view and file. Neither view nor attribute * may have a ':' character. */ public Object getAttribute(File file, String view, String attribute) { Object value = getAttributeInternal(file, view, attribute); if (value == null) { throw new IllegalArgumentException("invalid attribute for view '" + view + "': " + attribute); } return value; }
private static String getSingleAttribute(String attribute) { ImmutableList<String> attributeNames = getAttributeNames(attribute); if (attributeNames.size() != 1 || ALL_ATTRIBUTES.equals(attributeNames.get(0))) { throw new IllegalArgumentException("must specify a single attribute: " + attribute); } return attributeNames.get(0); }
/** * Creates a new attribute service using the given configuration. */ public AttributeService(Configuration configuration) { this(getProviders(configuration), configuration.defaultAttributeValues); }
/** * Implements {@link Files#readAttributes(Path, String, LinkOption...)}. */ public ImmutableMap<String, Object> readAttributes(File file, String attributes) { String view = getViewName(attributes); List<String> attrs = getAttributeNames(attributes); if (attrs.size() > 1 && attrs.contains(ALL_ATTRIBUTES)) { // attrs contains * and other attributes throw new IllegalArgumentException("invalid attributes: " + attributes); } Map<String, Object> result = new HashMap<>(); if (attrs.size() == 1 && attrs.contains(ALL_ATTRIBUTES)) { // for 'view:*' format, get all keys for all providers for the view AttributeProvider provider = providersByName.get(view); readAll(file, provider, result); for (String inheritedView : provider.inherits()) { AttributeProvider inheritedProvider = providersByName.get(inheritedView); readAll(file, inheritedProvider, result); } } else { // for 'view:attr1,attr2,etc' for (String attr : attrs) { result.put(attr, getAttribute(file, view, attr)); } } return ImmutableMap.copyOf(result); }
/** * Gets the value of the given attribute for the given file. {@code attribute} must be of the * form "view:attribute" or "attribute". */ public Object getAttribute(File file, String attribute) { String view = getViewName(attribute); String attr = getSingleAttribute(attribute); return getAttribute(file, view, attr); }
/** * Creates the file store for the file system. */ private static JimfsFileStore createFileStore( Configuration config, PathService pathService, FileSystemState state) { AttributeService attributeService = new AttributeService(config); // TODO(cgdecker): Make disk values configurable HeapDisk disk = new HeapDisk(config); FileFactory fileFactory = new FileFactory(disk); Map<Name, Directory> roots = new HashMap<>(); // create roots for (String root : config.roots) { JimfsPath path = pathService.parsePath(root); if (!path.isAbsolute() && path.getNameCount() == 0) { throw new IllegalArgumentException("Invalid root path: " + root); } Name rootName = path.root(); Directory rootDir = fileFactory.createRootDirectory(rootName); attributeService.setInitialAttributes(rootDir); roots.put(rootName, rootDir); } return new JimfsFileStore( new FileTree(roots), fileFactory, disk, attributeService, config.supportedFeatures, state); }
private void createInheritedViews( FileLookup lookup, AttributeProvider provider, Map<String, FileAttributeView> inheritedViews) { for (String inherited : provider.inherits()) { if (!inheritedViews.containsKey(inherited)) { AttributeProvider inheritedProvider = providersByName.get(inherited); FileAttributeView inheritedView = getFileAttributeView(lookup, inheritedProvider.viewType(), inheritedViews); inheritedViews.put(inherited, inheritedView); } } }
@Nullable private Object getAttributeInternal(File file, String view, String attribute) { AttributeProvider provider = providersByName.get(view); if (provider == null) { return null; } Object value = provider.get(file, attribute); if (value == null) { for (String inheritedView : provider.inherits()) { value = getAttributeInternal(file, inheritedView, attribute); if (value != null) { break; } } } return value; }
private static String getSingleAttribute(String attribute) { ImmutableList<String> attributeNames = getAttributeNames(attribute); if (attributeNames.size() != 1 || ALL_ATTRIBUTES.equals(attributeNames.get(0))) { throw new IllegalArgumentException("must specify a single attribute: " + attribute); } return attributeNames.get(0); }
/** * Creates a new attribute service using the given configuration. */ public AttributeService(Configuration configuration) { this(getProviders(configuration), configuration.defaultAttributeValues); }
private static void addMissingProviders(Map<String, AttributeProvider> providers) { Set<String> missingViews = new HashSet<>(); for (AttributeProvider provider : providers.values()) { for (String inheritedView : provider.inherits()) { if (!providers.containsKey(inheritedView)) { missingViews.add(inheritedView); } } } if (missingViews.isEmpty()) { return; } // add any inherited views that were not listed directly for (String view : missingViews) { addStandardProvider(providers, view); } // in case any of the providers that were added themselves have missing views they inherit addMissingProviders(providers); }
/** * Returns an attribute view of the given type for the given file lookup callback, or * {@code null} if the view type is not supported. */ @SuppressWarnings("unchecked") @Nullable public <V extends FileAttributeView> V getFileAttributeView(FileLookup lookup, Class<V> type) { AttributeProvider provider = providersByViewType.get(type); if (provider != null) { return (V) provider.view(lookup, createInheritedViews(lookup, provider)); } return null; }
/** * Returns an attribute view of the given type for the given file lookup callback, or * {@code null} if the view type is not supported. */ @Nullable <V extends FileAttributeView> V getFileAttributeView(FileLookup lookup, Class<V> type) { state.checkOpen(); return attributes.getFileAttributeView(lookup, type); }