What is an object that stores metadata about a file or directory on a file system?
The definition of metadata is "data about other data." With a file system, the data is contained in its files and directories, and the metadata tracks information about each of these objects: Is it a regular file, a directory, or a link? What is its size, creation date, last modified date, file owner, group owner, and access permissions? Show
A file system's metadata is typically referred to as its file attributes. The
If a program needs multiple file attributes around the same time, it can be inefficient to use methods that retrieve a single attribute. Repeatedly accessing the file system to retrieve a single attribute can adversely affect performance. For this reason, the
Before showing examples of the The supported views are as follows:
A specific file system implementation might support only the basic file attribute view, or it may support several of these file attribute views. A file system implementation might support other attribute views not included in this API. In most instances, you should not have to deal directly with any of the The The remainder of this section covers the following topics:
Basic File AttributesAs mentioned previously, to read the basic attributes of a file, you can use one of the A word about time stamps: The set of basic attributes includes three time stamps: The following code snippet reads and prints the basic file attributes for a given file and uses the methods in the Path file = ...; BasicFileAttributes attr = Files.readAttributes(file, BasicFileAttributes.class); System.out.println("creationTime: " + attr.creationTime()); System.out.println("lastAccessTime: " + attr.lastAccessTime()); System.out.println("lastModifiedTime: " + attr.lastModifiedTime()); System.out.println("isDirectory: " + attr.isDirectory()); System.out.println("isOther: " + attr.isOther()); System.out.println("isRegularFile: " + attr.isRegularFile()); System.out.println("isSymbolicLink: " + attr.isSymbolicLink()); System.out.println("size: " + attr.size()); In addition to the accessor methods shown in this example, there is
a Setting Time StampsThe following code snippet sets the last modified time in milliseconds: Path file = ...; BasicFileAttributes attr = Files.readAttributes(file, BasicFileAttributes.class); long currentTime = System.currentTimeMillis(); FileTime ft = FileTime.fromMillis(currentTime); Files.setLastModifiedTime(file, ft); } DOS File AttributesDOS file attributes are also supported on file systems other than DOS, such as Samba. The following snippet uses the methods of the
Path file = ...; try { DosFileAttributes attr = Files.readAttributes(file, DosFileAttributes.class); System.out.println("isReadOnly is " + attr.isReadOnly()); System.out.println("isHidden is " + attr.isHidden()); System.out.println("isArchive is " + attr.isArchive()); System.out.println("isSystem is " + attr.isSystem()); } catch (UnsupportedOperationException x) { System.err.println("DOS file" + " attributes not supported:" + x); } However, you can set a DOS attribute using the Path file = ...; Files.setAttribute(file, "dos:hidden", true); POSIX File PermissionsPOSIX is an acronym for Portable Operating System Interface for UNIX and is a set of IEEE and ISO standards designed to ensure interoperability among different flavors of UNIX. If a program conforms to these POSIX standards, it should be easily ported to other POSIX-compliant operating systems. Besides file owner and group owner, POSIX supports nine file permissions: read, write, and execute permissions for the file owner, members of the same group, and "everyone else." The following code snippet reads the POSIX file attributes for a given file and prints them to standard output. The code uses the methods in the Path file = ...; PosixFileAttributes attr = Files.readAttributes(file, PosixFileAttributes.class); System.out.format("%s %s %s%n", attr.owner().getName(), attr.group().getName(), PosixFilePermissions.toString(attr.permissions())); The
The following code snippet reads the attributes from one file and creates a new file, assigning the attributes from the original file to the new file: Path sourceFile = ...; Path newFile = ...; PosixFileAttributes attrs = Files.readAttributes(sourceFile, PosixFileAttributes.class); FileAttribute The To set a file's permissions to values represented as a hard-coded string, you can use the following code: Path file = ...; Set The
Setting a File or Group OwnerTo translate a name into an object you can store as a file owner or a group owner, you can use the
The following code snippet shows how to set the file owner by using the Path file = ...; UserPrincipal owner = file.GetFileSystem().getUserPrincipalLookupService() .lookupPrincipalByName("sally"); Files.setOwner(file, owner); There is no special-purpose method in the Path file = ...; GroupPrincipal group = file.getFileSystem().getUserPrincipalLookupService() .lookupPrincipalByGroupName("green"); Files.getFileAttributeView(file, PosixFileAttributeView.class) .setGroup(group); User-Defined File AttributesIf the file attributes supported by your file system implementation aren't sufficient for your needs, you can use the
Some implementations map this concept to features like NTFS Alternative Data Streams and extended attributes on file systems such as ext3 and ZFS. Most implementations impose restrictions on the size of the value, for example, ext3 limits the size to 4 kilobytes. A file's MIME type can be stored as a user-defined attribute by using this code snippet: Path file = ...; UserDefinedFileAttributeView view = Files .getFileAttributeView(file, UserDefinedFileAttributeView.class); view.write("user.mimetype", Charset.defaultCharset().encode("text/html"); To read the MIME type attribute, you would use this code snippet: Path file = ...; UserDefinedFileAttributeView view = Files .getFileAttributeView(file,UserDefinedFileAttributeView.class); String name = "user.mimetype"; ByteBuffer buf = ByteBuffer.allocate(view.size(name)); view.read(name, buf); buf.flip(); String value = Charset.defaultCharset().decode(buf).toString(); The Note: In Linux, you might have to enable extended attributes for user-defined attributes to work. If you receive an $ sudo mount -o remount,user_xattr / If you want to make the change permanent, add an entry to File Store AttributesYou can use the
The following code snippet prints the space usage for the file store where a particular file resides: Path file = ...; FileStore store = Files.getFileStore(file); long total = store.getTotalSpace() / 1024; long used = (store.getTotalSpace() - store.getUnallocatedSpace()) / 1024; long avail = store.getUsableSpace() / 1024; The What is an object that stores metadata about a file or directory on a file system Linux?The inode (index node) is a data structure in a Unix-style file system that describes a file-system object such as a file or a directory. Each inode stores the attributes and disk block locations of the object's data.
What is a file storage?File storage—also called file-level or file-based storage—is a hierarchical storage methodology used to organize and store data on a computer hard drive or on network-attached storage (NAS) device.
Which command display information about all block storage devices that are currently available on the system?The “df” command displays the information of device name, total blocks, total disk space, used disk space, available disk space, and mount points on a file system.
What is a file system in operating system?An integral part of an OS is what is called a File System. A File System is a data structure that stores data and information on storage devices (hard drives, floppy disc, etc.), making them easily retrievable. Different OS's use different file systems, but all have similar features.
|