+ All Categories
Home > Documents > Hacking the File System w ith JDK™ Release...

Hacking the File System w ith JDK™ Release...

Date post: 10-Jun-2020
Category:
Upload: others
View: 0 times
Download: 0 times
Share this document with a friend
43
Hacking the File System with JDK™ Release 7 Alan Bateman Sun Microsystems Inc. Carl Quinn Netflix Inc.
Transcript
Page 1: Hacking the File System w ith JDK™ Release 7openjdk.java.net/projects/nio/presentations/TS-5052.pdf · Hacking the File System w ith JDK™ Release 7 Alan Bateman Sun Microsystems

Hac k ing t he Fi le Syst em w i t h J DK ™ Release 7

Alan BatemanSun Microsystems Inc.

Carl QuinnNetflix Inc.

Page 2: Hacking the File System w ith JDK™ Release 7openjdk.java.net/projects/nio/presentations/TS-5052.pdf · Hacking the File System w ith JDK™ Release 7 Alan Bateman Sun Microsystems

Agenda• Introduction• Path operations• File operations• Directories

Page 3: Hacking the File System w ith JDK™ Release 7openjdk.java.net/projects/nio/presentations/TS-5052.pdf · Hacking the File System w ith JDK™ Release 7 Alan Bateman Sun Microsystems

Agenda• Introduction• Path operations• File operations• Directories• Recursive operations• File change notification• File attributes• Provider interface• Conclusion

Page 4: Hacking the File System w ith JDK™ Release 7openjdk.java.net/projects/nio/presentations/TS-5052.pdf · Hacking the File System w ith JDK™ Release 7 Alan Bateman Sun Microsystems

Introduction: Basic Concepts

• FileRef– Reference to a file

Page 5: Hacking the File System w ith JDK™ Release 7openjdk.java.net/projects/nio/presentations/TS-5052.pdf · Hacking the File System w ith JDK™ Release 7 Alan Bateman Sun Microsystems

Introduction: Basic Concepts

• FileRef– Reference to a file

• Path– Locates a file using a system dependent path

Page 6: Hacking the File System w ith JDK™ Release 7openjdk.java.net/projects/nio/presentations/TS-5052.pdf · Hacking the File System w ith JDK™ Release 7 Alan Bateman Sun Microsystems

Introduction: Basic Concepts

• FileRef– Reference to a file

• Path– Locates a file using a system dependent path

• FileSystem– Provides interface to file system– Factory for objects to access files and other

objects in the file system.– Default file system for local/platform file system

Page 7: Hacking the File System w ith JDK™ Release 7openjdk.java.net/projects/nio/presentations/TS-5052.pdf · Hacking the File System w ith JDK™ Release 7 Alan Bateman Sun Microsystems

FileSystem as a factory

FileSystem

Page 8: Hacking the File System w ith JDK™ Release 7openjdk.java.net/projects/nio/presentations/TS-5052.pdf · Hacking the File System w ith JDK™ Release 7 Alan Bateman Sun Microsystems

FileSystem as a factory

FileSystem

Path

WatchService

UserPrincipal-LookupService

FileStore

PathMatcher

Page 9: Hacking the File System w ith JDK™ Release 7openjdk.java.net/projects/nio/presentations/TS-5052.pdf · Hacking the File System w ith JDK™ Release 7 Alan Bateman Sun Microsystems

FileSystem as a factory

FileSystemProvider

FileSystem

Path

WatchService

UserPrincipal-LookupService

FileStore

PathMatcher

Page 10: Hacking the File System w ith JDK™ Release 7openjdk.java.net/projects/nio/presentations/TS-5052.pdf · Hacking the File System w ith JDK™ Release 7 Alan Bateman Sun Microsystems

Path

• Immutable• Create from path String or URI• Defines methods to access and manipulate paths• Defines methods to access files• File.toPath() for interoperability

Page 11: Hacking the File System w ith JDK™ Release 7openjdk.java.net/projects/nio/presentations/TS-5052.pdf · Hacking the File System w ith JDK™ Release 7 Alan Bateman Sun Microsystems

Path operations

• Accessing components – getName, getParent, getRoot, subpath

• Testing and comparing– startsWith, endsWith, equals, compareTo

• Combining– resolve (and inverse: relativize)

Page 12: Hacking the File System w ith JDK™ Release 7openjdk.java.net/projects/nio/presentations/TS-5052.pdf · Hacking the File System w ith JDK™ Release 7 Alan Bateman Sun Microsystems

File operations

• I/O:– newInputStream/newOutputStream for stream I/O

Page 13: Hacking the File System w ith JDK™ Release 7openjdk.java.net/projects/nio/presentations/TS-5052.pdf · Hacking the File System w ith JDK™ Release 7 Alan Bateman Sun Microsystems

File operations

• I/O:– newInputStream/newOutputStream for stream I/O– newSeekableByteChannel for channel I/O

• SeekableByteChannel = ByteChannel + file position• cast to FileChannel for advanced operations such as file

locking, memory mapped I/O, ...

Page 14: Hacking the File System w ith JDK™ Release 7openjdk.java.net/projects/nio/presentations/TS-5052.pdf · Hacking the File System w ith JDK™ Release 7 Alan Bateman Sun Microsystems

File operations

• I/O:– newInputStream/newOutputStream for stream I/O– newSeekableByteChannel for channel I/O

• SeekableByteChannel = ByteChannel + file position• cast to FileChannel for advanced operations such as file

locking, memory mapped I/O, ...• Other operations:

– createFile, createDirectory, delete, copyTo, moveTo, isSameFile, toRealPath, checkAccess (exists, notExists), createSymbolicLink, readSymbolicLink, ...

Page 15: Hacking the File System w ith JDK™ Release 7openjdk.java.net/projects/nio/presentations/TS-5052.pdf · Hacking the File System w ith JDK™ Release 7 Alan Bateman Sun Microsystems

Exceptions

• All methods that access file system may throw IOException– Specific exceptions for specific failures– Specific exceptions extend FileSystemException

• All other exceptions in API are unchecked

Page 16: Hacking the File System w ith JDK™ Release 7openjdk.java.net/projects/nio/presentations/TS-5052.pdf · Hacking the File System w ith JDK™ Release 7 Alan Bateman Sun Microsystems

Directories

• DirectoryStream to iterate over the entries– Scales to large directories– Uses less resources– Smooth out response time for remote file systems– Provides handle to open directory

• Filter using glob, regex, or custom filter

Page 17: Hacking the File System w ith JDK™ Release 7openjdk.java.net/projects/nio/presentations/TS-5052.pdf · Hacking the File System w ith JDK™ Release 7 Alan Bateman Sun Microsystems

Recursive Operations

• Files.walkFileTree– Walks a file tree rooted at a given starting file

Page 18: Hacking the File System w ith JDK™ Release 7openjdk.java.net/projects/nio/presentations/TS-5052.pdf · Hacking the File System w ith JDK™ Release 7 Alan Bateman Sun Microsystems

Recursive Operations

interface FileVisitor<T> { FileVisitResult preVisitDirectory(T dir); FileVisitResult preVisitDirectoryFailed(T dir, IOException exc); FileVisitResult visitFile(T file, BasicFileAttributes attrs); FileVisitResult visitFileFailed(T file, IOException exc); FileVisitResult postVisitDirectory(T dir, IOException exc);}

• Files.walkFileTree– Walks a file tree rooted at a given starting file– Invoke FileVisitor method for each file/directory

Page 19: Hacking the File System w ith JDK™ Release 7openjdk.java.net/projects/nio/presentations/TS-5052.pdf · Hacking the File System w ith JDK™ Release 7 Alan Bateman Sun Microsystems

Recursive Operations

• Files.walkFileTree– Walks a file tree rooted at a given starting file– Invoke FileVisitor method for each file/directory– SimpleFileVisitor with default behavior

Page 20: Hacking the File System w ith JDK™ Release 7openjdk.java.net/projects/nio/presentations/TS-5052.pdf · Hacking the File System w ith JDK™ Release 7 Alan Bateman Sun Microsystems

Recursive Operations

enum FileVisitResult { CONTINUE, TERMINATE, SKIP_SUBTREE, SKIP_SIBLINGS}

• Files.walkFileTree– Walks a file tree rooted at a given starting file– Invoke FileVisitor method for each file/directory– SimpleFileVisitor with default behavior– Return value controls iteration

Page 21: Hacking the File System w ith JDK™ Release 7openjdk.java.net/projects/nio/presentations/TS-5052.pdf · Hacking the File System w ith JDK™ Release 7 Alan Bateman Sun Microsystems

Recursive Operations

• Files.walkFileTree– Walks a file tree rooted at a given starting file– Invoke FileVisitor method for each file/directory– SimpleFileVisitor with default behavior– Return value controls iteration– Options control if sym links are followed

• Not followed by default• When following then cycles are detected and reported

Page 22: Hacking the File System w ith JDK™ Release 7openjdk.java.net/projects/nio/presentations/TS-5052.pdf · Hacking the File System w ith JDK™ Release 7 Alan Bateman Sun Microsystems

startpreVisitDirectory

dir linkfile

Page 23: Hacking the File System w ith JDK™ Release 7openjdk.java.net/projects/nio/presentations/TS-5052.pdf · Hacking the File System w ith JDK™ Release 7 Alan Bateman Sun Microsystems

startpreVisitDirectory

dir linkfile

Page 24: Hacking the File System w ith JDK™ Release 7openjdk.java.net/projects/nio/presentations/TS-5052.pdf · Hacking the File System w ith JDK™ Release 7 Alan Bateman Sun Microsystems

startpreVisitDirectory

dir linkfile

visitFile

visitFile visitFile

Page 25: Hacking the File System w ith JDK™ Release 7openjdk.java.net/projects/nio/presentations/TS-5052.pdf · Hacking the File System w ith JDK™ Release 7 Alan Bateman Sun Microsystems

startpreVisitDirectory

dir linkfile

postVisitDirectory

visitFile

visitFile visitFile

visitFile

Page 26: Hacking the File System w ith JDK™ Release 7openjdk.java.net/projects/nio/presentations/TS-5052.pdf · Hacking the File System w ith JDK™ Release 7 Alan Bateman Sun Microsystems

File Change Notification

• Improve performance of applications that are forced to poll the file system today

Page 27: Hacking the File System w ith JDK™ Release 7openjdk.java.net/projects/nio/presentations/TS-5052.pdf · Hacking the File System w ith JDK™ Release 7 Alan Bateman Sun Microsystems

File Change Notification

• Improve performance of applications that are forced to poll the file system today

• WatchService– Watch registered objects for events and changes– Makes use of native event facility where available– Supports concurrent processing of events

Page 28: Hacking the File System w ith JDK™ Release 7openjdk.java.net/projects/nio/presentations/TS-5052.pdf · Hacking the File System w ith JDK™ Release 7 Alan Bateman Sun Microsystems

File Change Notification

• Improve performance of applications that are forced to poll the file system today

• WatchService– Watch registered objects for events and changes– Makes use of native event facility where available– Supports concurrent processing of events

• Path implements Watchable– Register directory to get events when entries are

created, deleted, or modified

Page 29: Hacking the File System w ith JDK™ Release 7openjdk.java.net/projects/nio/presentations/TS-5052.pdf · Hacking the File System w ith JDK™ Release 7 Alan Bateman Sun Microsystems

WatchService API

• WatchKey represents registration of a watchable object with a WatchService.

// registerWatchKey key = file.register(watcher, ENTRY_CREATE, ENTRY_MODIFY);// cancel registrationkey.cancel();

Page 30: Hacking the File System w ith JDK™ Release 7openjdk.java.net/projects/nio/presentations/TS-5052.pdf · Hacking the File System w ith JDK™ Release 7 Alan Bateman Sun Microsystems

WatchService API

• WatchKey represents registration of a watchable object with a WatchService.

• WatchKey signalled and queued when event detected

• WatchService poll or take methods used to retrieve signalled key.

class WatchService { WatchKey take(); WatchKey poll(); WatchKey poll(long timeout, TimeUnit unit);}

Page 31: Hacking the File System w ith JDK™ Release 7openjdk.java.net/projects/nio/presentations/TS-5052.pdf · Hacking the File System w ith JDK™ Release 7 Alan Bateman Sun Microsystems

WatchService API

• WatchKey represents registration of a watchable object with a WatchService.

• WatchKey signalled and queued when event detected

• WatchService poll or take methods used to retrieve signalled keys

• Process events• WatchKey reset method returns key to ready

state

Page 32: Hacking the File System w ith JDK™ Release 7openjdk.java.net/projects/nio/presentations/TS-5052.pdf · Hacking the File System w ith JDK™ Release 7 Alan Bateman Sun Microsystems

File Attributes

• Meta-data associated with file• Long standing requests from applications

Page 33: Hacking the File System w ith JDK™ Release 7openjdk.java.net/projects/nio/presentations/TS-5052.pdf · Hacking the File System w ith JDK™ Release 7 Alan Bateman Sun Microsystems

File Attributes

• Meta-data associated with file• Long standing requests from applications

– File owner– Permissions– Timestamps– DOS attributes– Extended attributes– ...

Page 34: Hacking the File System w ith JDK™ Release 7openjdk.java.net/projects/nio/presentations/TS-5052.pdf · Hacking the File System w ith JDK™ Release 7 Alan Bateman Sun Microsystems

File Attributes

• Meta-data associated with file• Long standing requests from applications• Highly file system specific

Page 35: Hacking the File System w ith JDK™ Release 7openjdk.java.net/projects/nio/presentations/TS-5052.pdf · Hacking the File System w ith JDK™ Release 7 Alan Bateman Sun Microsystems

File Attributes

• Group related attributes• Define view of those attributes

– Typesafe methods to access attributes in group– Bulk access where applicable– Converts to/from file system representation

• Implementation requires to support basic view– Attributes common to most file systems– BasicFileAttributes in examples

• Implementation may support others

Page 36: Hacking the File System w ith JDK™ Release 7openjdk.java.net/projects/nio/presentations/TS-5052.pdf · Hacking the File System w ith JDK™ Release 7 Alan Bateman Sun Microsystems

File Attributes

• java.nio.file.attribute package• basic view: BasicFileAttributeView • posix view: PosixFileAttributeView• acl view: AclFileAttributeView

– Based on NFSv4 ACL model• owner view: FileOwnerAttributeView• user-defined view: UserDefinedFileAttributeView

Page 37: Hacking the File System w ith JDK™ Release 7openjdk.java.net/projects/nio/presentations/TS-5052.pdf · Hacking the File System w ith JDK™ Release 7 Alan Bateman Sun Microsystems

Attributes class makes it easy

BasicFileAttributes attrs = Attributes.readBasicFileAttributes(file);BasicFileAttributes attrs = Attributes.readBasicFileAttributes(file, NOFOLLOW_LINKS);FileTime lastModifedTime = ...Attributes.setLastModifiedTime(file, lastModifedTime);PosixFileAttributes attrs = Attributes.readPosixFileAttributes(file);Set<PosixFilePermission> perms = ...Attributes.setPosixFilePermissions(file, perms);UserPrincipal owner = Attributes.getOwner(file);

Page 38: Hacking the File System w ith JDK™ Release 7openjdk.java.net/projects/nio/presentations/TS-5052.pdf · Hacking the File System w ith JDK™ Release 7 Alan Bateman Sun Microsystems

File Attributes

• Dynamic access– treat attributes as name/value pairs

boolean isSymbolicLink (Boolean)file.getAttribute(“isSymbolicLink”, NOFOLLOW_LINKS);Set<PosixFilePermission> perms = ...file.setAttribute(“posix:permissions”, perms);Map<String,?> attrs = file.readAttributes(“basic:*”);

Page 39: Hacking the File System w ith JDK™ Release 7openjdk.java.net/projects/nio/presentations/TS-5052.pdf · Hacking the File System w ith JDK™ Release 7 Alan Bateman Sun Microsystems

File Attributes

• Dynamic access– treat attributes as name/value pairs– loose type safety

• Ask FileSystem for the set of supported views• Ask underlying FileStore if it supports view• Set initial attributes when creating files

– important for security related attributesFileAttribute<Set<PosixFilePermission>> perms = ...f.createFile(perms);

Page 40: Hacking the File System w ith JDK™ Release 7openjdk.java.net/projects/nio/presentations/TS-5052.pdf · Hacking the File System w ith JDK™ Release 7 Alan Bateman Sun Microsystems

Provider interface

• Used to develop and deploy custom file systems– Desktop file system– Memory file system

• Provider identified by URI scheme• Factory for FileSystem instances• Default provider required (file:///)• Can replace or interpose on default provider

– Create virtual file system– java.io uses default provider

Page 41: Hacking the File System w ith JDK™ Release 7openjdk.java.net/projects/nio/presentations/TS-5052.pdf · Hacking the File System w ith JDK™ Release 7 Alan Bateman Sun Microsystems

Conclusion

• Finally address shortcomings of java.io.File• Easy to use for basic and common operations• Provides access to more advanced facilities for

applications that require it• Extensible via provider mechanism

Page 42: Hacking the File System w ith JDK™ Release 7openjdk.java.net/projects/nio/presentations/TS-5052.pdf · Hacking the File System w ith JDK™ Release 7 Alan Bateman Sun Microsystems

More information

• BOF-5087: All Things I/O with JDK™ Release 7, Thursday @ 6:30pm, Gateway 102-103.

• OpenJDK New I/O Project– http://openjdk.java.net/projects/nio

• Blogs– http://blogs.sun.com/alanb

Page 43: Hacking the File System w ith JDK™ Release 7openjdk.java.net/projects/nio/presentations/TS-5052.pdf · Hacking the File System w ith JDK™ Release 7 Alan Bateman Sun Microsystems

Alan BatemanSun Microsystems Inc.

Carl QuinnNetflix Inc.


Recommended