/** * Creates a new root directory with the given ID and name. */ public static Directory createRoot(int id, Name name) { return new Directory(id, name); }
private boolean expandIfNeeded() { if (entryCount <= resizeThreshold) { return false; } DirectoryEntry[] newTable = new DirectoryEntry[table.length << 1]; // redistribute all current entries in the new table for (DirectoryEntry entry : table) { while (entry != null) { int index = bucketIndex(entry.name(), newTable.length); addToBucket(index, newTable, entry); DirectoryEntry next = entry.next; // set entry.next to null; it's always the last entry in its bucket after being added entry.next = null; entry = next; } } this.table = newTable; resizeThreshold <<= 1; return true; }
/** * Unlinks the given name from the file it is linked to. * * @throws IllegalArgumentException if {@code name} is a reserved name such as "." or no entry * exists for the name */ public void unlink(Name name) { DirectoryEntry entry = remove(checkNotReserved(name, "unlink")); entry.file().unlinked(); }
@Override void unlinked() { // we don't actually remove the parent link when this directory is unlinked, but the parent's // link count should go down all the same parent().decrementLinkCount(); }
int index = bucketIndex(entry.name(), table.length); if (expandIfNeeded()) { index = bucketIndex(entry.name(), table.length); addToBucket(index, table, entry); } else {
/** * Links the given name to the given file in this directory. * * @throws IllegalArgumentException if {@code name} is a reserved name such as "." or if an * entry already exists for the name */ public void link(Name name, File file) { DirectoryEntry entry = new DirectoryEntry(this, checkNotReserved(name, "link"), file); put(entry); file.linked(entry); }
/** * Gets the directory entry for the root with the given name or {@code null} if no such root * exists. */ @Nullable public DirectoryEntry getRoot(Name name) { Directory dir = roots.get(name); return dir == null ? null : dir.entryInParent(); }
/** * Creates a copy of this directory. The copy does <i>not</i> contain a copy of the entries in * this directory. */ @Override Directory copyWithoutContent(int id) { return Directory.create(id); }
/** * Returns the entry for the given name in this table or null if no such entry exists. */ @Nullable public DirectoryEntry get(Name name) { int index = bucketIndex(name, table.length); DirectoryEntry entry = table[index]; while (entry != null) { if (name.equals(entry.name())) { return entry; } entry = entry.next; } return null; }
/** * Returns true if this directory has no entries other than those to itself and its parent. */ public boolean isEmpty() { return entryCount() == 2; }
/** * Creates a new root directory with the given name. */ public Directory createRootDirectory(Name name) { return Directory.createRoot(nextFileId(), name); }
int index = bucketIndex(entry.name(), table.length); if (expandIfNeeded()) { index = bucketIndex(entry.name(), table.length); addToBucket(index, table, entry); } else {
@Override void unlinked() { // we don't actually remove the parent link when this directory is unlinked, but the parent's // link count should go down all the same parent().decrementLinkCount(); }
/** * Links the given name to the given file in this directory. * * @throws IllegalArgumentException if {@code name} is a reserved name such as "." or if an * entry already exists for the name */ public void link(Name name, File file) { DirectoryEntry entry = new DirectoryEntry(this, checkNotReserved(name, "link"), file); put(entry); file.linked(entry); }
/** * Gets the {@linkplain Path#toRealPath(LinkOption...) real path} to the file located by the * given path. */ public JimfsPath toRealPath( JimfsPath path, PathService pathService, Set<? super LinkOption> options) throws IOException { checkNotNull(path); checkNotNull(options); store.readLock().lock(); try { DirectoryEntry entry = lookUp(path, options).requireExists(path); List<Name> names = new ArrayList<>(); names.add(entry.name()); while (!entry.file().isRootDirectory()) { entry = entry.directory().entryInParent(); names.add(entry.name()); } // names are ordered last to first in the list, so get the reverse view List<Name> reversed = Lists.reverse(names); Name root = reversed.remove(0); return pathService.createPath(root, reversed); } finally { store.readLock().unlock(); } }
/** * Creates a new directory. */ public Directory createDirectory() { return Directory.create(nextFileId()); }
/** * Removes and returns the entry for the given name from the directory. * * @throws IllegalArgumentException if there is no entry with the given name in the directory */ @VisibleForTesting DirectoryEntry remove(Name name) { int index = bucketIndex(name, table.length); DirectoryEntry prev = null; DirectoryEntry entry = table[index]; while (entry != null) { if (name.equals(entry.name())) { if (prev != null) { prev.next = entry.next; } else { table[index] = entry.next; } entry.next = null; entryCount--; entry.file().decrementLinkCount(); return entry; } prev = entry; entry = entry.next; } throw new IllegalArgumentException("no entry matching '" + name + "' in this directory"); }
/** * Returns true if this directory has no entries other than those to itself and its parent. */ public boolean isEmpty() { return entryCount() == 2; }
/** * Creates a new root directory with the given name. */ public Directory createRootDirectory(Name name) { return Directory.createRoot(nextFileId(), name); }
/** * Unlinks the given name from the file it is linked to. * * @throws IllegalArgumentException if {@code name} is a reserved name such as "." or no entry * exists for the name */ public void unlink(Name name) { DirectoryEntry entry = remove(checkNotReserved(name, "unlink")); entry.file().unlinked(); }