Android File System

Learn via video courses
Topics Covered

Android's file system structure exhibits similarities with Linux due to its foundation on the Linux kernel, yet it possesses distinct characteristics. Unlike Windows, Android doesn't use drives or directories but employs partitions within a singular directory, akin to a tree with varied branches. Users navigate these partitions using file managers to organize their files amidst what might seem like a chaotic storage system. Moreover, in Java, the FileSystems class facilitates access to the default file system through methods like FileSystems.getDefault(), offering a gateway to interact with files and other system objects. This approach underscores Android's unique approach while ensuring compatibility and functionality within its ecosystem.

A File System is a Factory for Several Types of Objects

Access to all these partitions is provided via APIs including Java File API to provide a standard interface to access and manipulate the data and the files on the device.

getPath

This method returns a Path object that is obtained by converting a sequence of strings that represent the path of a file or a directory. 

Syntax:

The getPath() method comprises two parameters. The first parameter is a string that represents the path of the file or the directory. Another parameter is yet another optional string that represents the additional path elements.

Exceptions:

InvalidPathException () is thrown by this method when the path string is invalid or if any of these path components comprise an invalid character.

NullPointerException is thrown when the String first is null.

getPathMatcher

The String representation of the Path object is passed to the getMatcher() method that would return a PathMatcher object to perform the match operations on the path string.

Syntax:

Parameters:

  • syntaxAndPattern: It is a string that specifies both the syntax and pattern which can be distinguished while matching paths. After the identification of both, it takes the form of syntax: pattern where the syntax and pattern are separated by a colon (:).

Exceptions:

  • The getPathMatcher() throws IllegalArgumentException if either the syntax or the pattern string is not in the correct format.

getFileStores

The getFileStores() method returns an Iterable object that can be further utilized to iterate over the file stores available in the file system. The Iterable object is actually in the form of Iterable<FileStore> which provides an iterator to iterate over the elements in the collection of FileStore objects. Syntax:

However, it's not possible to determine the exact order of the FileStore element as the file stores might change during the lifetime of the Java Virtual Machine (JVM).

FileStore objects may have different capabilities and limitations. For example, some file stores may support only read-only access, while others may support both read and write access

Parameters: This method does not accept any parameters.

Exceptions: There are no exceptions for this method.

getUserPrincipalLookupService

The getUserPrincipalLookupService() method in the Java New Input/Output (NIO) API returns a UserPrincipalLookupService object that can be used to look up the UserPrincipal object associated with a given user name or group name.

Syntax:

Here, the UserPrincipal object is meant to represent a user's identity while the UserPrincipalLookupService provides a mechanism to retrieve the UserPrincipal object associated with a specific user name or a group name or the ID.

Example:

Parameters: This method does not accept any parameters.

Exceptions: There are no exceptions for this method.

The newWatchService

To begin with, Android File System comprises WatchService that monitors all the file system events like file creation, modification, and deletion. This allows for the developers to receive a notification in such scenarios when any of these events occur so that appropriate action can be taken.

The newWatchService() method eventually returns a new WatchService object that can be used to monitor any change in the file system.

Syntax:

Parameters: This method does not accept any parameters.

Exceptions: This method throws UnsupportedOperationException if the file system has no support for watching the called operation.

Example: We can access the path of the directory that has to be monitored, get the new WatchService object for the accessible path of the directory, and use the same to monitor events such as file creation and deletion in that directory.

Android File System Hierarchy Explained

The Android File System hierarchy is further disintegrated into several directories. Each one of them has a specific purpose that helps in troubleshooting any specific issues related to one directory and managing the system effectively.

Android File System Hierarchy Explained

/boot

The /boot partition comprises the boot image and the kernel that are required to start the Android OS. The device cannot boot without the /boot partition.

This partition is read-only and cannot be modified by the user. To modify it, the device must be rooted, and a custom recovery must be installed. Once the device is rooted, the /boot partition can be accessed and modified using a file manager or a command-line interface.

It is mandatory to keep the files in this partition unchanged so the system can boot correctly with no issues. Wiping the /boot partition (if required, but is strongly discouraged though) should always be followed by installing a new ROM which would eventually bring up a new /boot partition.

/system

The /system partition is a critical component of the Android File System as it contains the entire Android OS, including even the GUI components as well as all the applications that come pre-installed with the device.

The /system partition contains the directories and files like app, bin, fonts, framework, etc.

The contents of the /system partition are read-only and can only be modified with a rooted device to remove any pre-installed apps or customize the Android OS.

Removing this partition will erase all the pre-installed system applications and should only be done when the user wants to install a custom ROM or troubleshoot an issue with the OS.

/recovery

The /recovery partition is a small partition that includes the recovery image to recover the device if any software failure occurs. This is yet another read-only directory that cannot be modified.

The /recovery partition contains the recovery image and the scripts to automatically boot the device into recovery mode (used for system maintenance like installing software updates, data backup or restore, or performing a factory reset) and to update the Android OS.

/data

The /data partition contains all the user and the app data. The user data can be in the form of photos, videos, music, contacts, preferences, settings, android apps, etc that have been installed into the device.

This is a read-write partition that can be modified by the data coming from various installed applications. It is important to back up the files in this partition regularly.

This entire partition will be deleted if you perform a factory reset, returning the device to its original settings much like a brand-new one.

/cache

The /cache partition contains all the temporary files used by the OS and the apps. This is a read-write partition and can also be cleared by the system or a user.

The files that the /cache partition includes can be quickly accessed without having to retrieve them either from the internal storage or the external SD card. Doing so improves the performance and speed of the device by minimizing the time it requires to access the frequently used data.

We can remove the cache data as it has no impact on the personal and important details required by any application but it will wipe out the data as well as the components that are frequently accessed.

For instance, it creates cached data, thumbnails, etc as the temporary data from any media file that we download or edit.

Clearing the cache partition can help you free up some space in the device and the cached data starts to populate again while we use the device.

/misc

The /misc partition in the Android file system is meant for the storage of the miscellaneous system settings.

This partition contains a file referred to as misc.img that includes the necessary device-specific information, like the bootloader state as to know if the device is in the locked or unlocked state. The misc.img also contains the errors that occur during the boot process.

The /misc also stores the data for over-the-air (0TA) updates, which informs if the device is up-to-date or needs an upgrade.

The absence of the /misc partition can lead to the improper functioning of several features of the device.

/sdcard

The /sdcard partition in the Android File System is more often an emulated external storage device that can be accessed by both the user as well as the Android system. Emulated external storage device allows apps to access a part of the device's internal storage as if it were external storage and provides compatibility with the legacy apps that couldn't access the internal storage directly.

The partition is typically mounted as /mnt/sdcard or /storage/sdcard0, and it is the default storage location for most user-generated content on the device.

The /sdcard partition allows the user to easily transfer files between the device and a computer or Android device. You can connect this device to a PC via Media Transfer Protocol (MTP) to transfer your files. Alternatively, a card reader can also be used for the same purpose.

Many Android apps are designed to save user-generated content to the /sdcard partition by default, making it easier for users to access and manage their files. Removing any data from this partition will not cause any issues to the device. However, it might be a hassle for the apps that had been using any files from the removed data.

/sd-ext

The /sd-ext partition is an optional partition that can be utilized to provide additional storage space for an Android device if the internal storage is limited. It is more often used along with the /data partition as well.

Commonly found in custom ROMs that provide additional storage space for data as well as apps, users can allocate some memory by using a part of the device's external SD card as an extension to the device's internal storage. This ultimately helps in installing more apps and storing enough data without running out of space.

The /sd-ext partition allows a user to install more applications on the device in the case of limited internal storage space as it can be quickly filled with the app data.

In addition, the /sd-ext partition is not usually supported by all Android devices and even requires additional steps for the setup in some cases. It is mounted as /mnt/sd-ext or /storage/sdcard1.

How to Access the Root Files in Android?

To access the root files in the Android File System, we need to have root access enabled on our Android device. This provides us with administrative privileges to access and modify the system files and system settings, unlike a normal user.

Here's how you can access the root files in the Android File System:

  • Root the Android device: To be the administrator, the device first needs to be rooted. A rooting tool can be used for the same. However, always ensure that the rooting process has to be done correctly and it also voids the device warranty. 
  • Root file manager installation: Once the device is rooted, a root file manager app can be used to access and manage all the system files. There are several root file manager apps available on the Google Play Store.
  • Grant root access to the file manager: As the root file manager gets installed, it has to be granted the root access that enables it to get all the system files and modify them, if needed by the user. Usually, the root file manager app prompts to grant root access upon its initial launching.
  • Navigate to the root directory: As access to the root file manager is now granted, we can navigate to the root directory of the Android system. The root directory is the one that comprises the entire Android file system hierarchy and contains all the system files and folders. 

Now that we have navigated to the root directory, we can now access and modify the system files and folders as per our needs.

However, rooting an Android device can be a hazard due to a few of the reasons listed here:

  • Rooting exposes the device to security risks by allowing malware to access files and data beyond the user's knowledge.

  • It voids the warranty because manufacturers don't encourage the modification of the device's software.

  • It can lead to bricking of the Android device, which means your device can be rendered partially or completely inoperable.

Moreover, banking apps can detect rooted devices and prevent their use on such devices because it compromises security and allows easier stealing of sensitive information like bank credentials.

Conclusion

  • The Android File System is a hierarchical file system structure that manages the data and files on an Android device.
  • The Android File System is divided into several partitions, with each one having its specific purpose. These partitions include /boot, /system, /recovery, /data, /cache, /misc, /sdcard, and /sd-ext.
  • The /boot partition comprises the boot image and the kernel that are required to start the Android OS. The device cannot boot without the /boot partition.
  • The /recovery partition is a small partition that includes the recovery image to recover the device if any software failure occurs.
  • The /misc partition in the Android file system is meant for the storage of the miscellaneous system settings.