DEV Community

Cover image for Application and file system space statistics
liu yang
liu yang

Posted on • Edited on

Application and file system space statistics

API Introduction for File System and Application Space Statistics in HarmonyOS

Detailed API Information

For comprehensive API information, refer to the official HarmonyOS documentation for ohos.file.statvfs and ohos.file.storageStatistics.

File System and Application Space Statistics

Available Modules and Interfaces

Module Interface Name Function
@ohos.file.storageStatistics getCurrentBundleStats Gets the storage size of the current app (in bytes).
@ohos.file.statvfs getFreeSize Gets the remaining space of the specified file system (in bytes).
@ohos.file.statvfs getTotalSize Gets the total space of the specified file system (in bytes).

Application Space Statistics

BundleStats Property Meaning Statistical Path
appSize Size of the app installation file (in bytes). App installation files are stored in: /data/storage/el1/bundle.
cacheSize Size of the app cache files (in bytes). App cache files are stored in: /data/storage/el1/base/cache, /data/storage/el1/base/haps/entry/cache, /data/storage/el2/base/cache, /data/storage/el2/base/haps/entry/cache.
dataSize Size of app file storage (excluding installation and cache files, in bytes). App files include local, distributed, and database files. Local files are stored in: /data/storage/el1/base, /data/storage/el2/base. Distributed files are stored in: /data/storage/el2/distributedfiles. Database files are stored in: /data/storage/el1/database, /data/storage/el2/database.

Development Examples

Getting the Remaining Space of the File System's Data Partition

To get the remaining space of the file system's data partition, you can use the getFreeSize method from the statfs module.

import { statfs } from '@kit.CoreFileKit';
import { BusinessError } from '@kit.BasicServicesKit';
import { common } from '@kit.AbilityKit';

let context = getContext(this) as common.UIAbilityContext;
let path = context.filesDir; // Get the path to the app's private directory
statfs.getFreeSize(path, (err: BusinessError, number: number) => {
  if (err) {
    console.error(`Invoke getFreeSize failed, code is ${err.code}, message is ${err.message}`);
  } else {
    console.info(`Invoke getFreeSize succeeded, size is ${number} bytes`);
  }
});
Enter fullscreen mode Exit fullscreen mode

Explanation

  • statfs Module: Provides methods for querying file system statistics.
  • getFreeSize Method: Retrieves the remaining space of the specified file system path.
  • Error Handling: The callback function handles errors by logging the error code and message.
  • Success Handling: Logs the remaining space in bytes.

Getting the Storage Space Size of the Current Application

To get the storage space size of the current application, you can use the getCurrentBundleStats method from the storageStatistics module.

import { storageStatistics } from '@kit.CoreFileKit';
import { BusinessError } from '@kit.BasicServicesKit';

storageStatistics.getCurrentBundleStats((err: BusinessError, bundleStats: storageStatistics.BundleStats) => {
  if (err) {
    console.error(`Invoke getCurrentBundleStats failed, code is ${err.code}, message is ${err.message}`);
  } else {
    console.info(`Invoke getCurrentBundleStats succeeded, appSize is ${bundleStats.appSize} bytes`);
    console.info(`Invoke getCurrentBundleStats succeeded, cacheSize is ${bundleStats.cacheSize} bytes`);
    console.info(`Invoke getCurrentBundleStats succeeded, dataSize is ${bundleStats.dataSize} bytes`);
  }
});
Enter fullscreen mode Exit fullscreen mode

Explanation

  • storageStatistics Module: Provides methods for querying storage statistics related to the current application.
  • getCurrentBundleStats Method: Retrieves the storage statistics of the current application, including appSize, cacheSize, and dataSize.
  • Error Handling: The callback function handles errors by logging the error code and message.
  • Success Handling: Logs the storage sizes in bytes for different categories (appSize, cacheSize, dataSize).

Additional Examples

Getting the Total Space of the File System's Data Partition

To get the total space of the file system's data partition, you can use the getTotalSize method from the statvfs module.

import { statfs } from '@kit.CoreFileKit';
import { BusinessError } from '@kit.BasicServicesKit';
import { common } from '@kit.AbilityKit';

let context = getContext(this) as common.UIAbilityContext;
let path = context.filesDir; // Get the path to the app's private directory
statfs.getTotalSize(path, (err: BusinessError, number: number) => {
  if (err) {
    console.error(`Invoke getTotalSize failed, code is ${err.code}, message is ${err.message}`);
  } else {
    console.info(`Invoke getTotalSize succeeded, size is ${number} bytes`);
  }
});
Enter fullscreen mode Exit fullscreen mode

Explanation

  • getTotalSize Method: Retrieves the total space of the specified file system path.
  • Error Handling: The callback function handles errors by logging the error code and message.
  • Success Handling: Logs the total space in bytes.

Practical Use Cases

Monitoring Application Storage Usage

  • Usage Monitoring: Regularly monitor the storage usage of your application to ensure it does not exceed allocated limits.
  • User Notifications: Notify users if the application is approaching its storage limit, prompting them to clear cache or uninstall unnecessary files.

Optimizing Storage Management

  • Cache Management: Use the cacheSize property to manage the cache size effectively, clearing unnecessary cache files to free up space.
  • Data Management: Use the dataSize property to manage application data, ensuring efficient storage and retrieval of user data.

Error Handling and Debugging

  • Comprehensive Logging: Implement comprehensive logging to capture and diagnose issues related to storage statistics.
  • User Feedback: Provide clear feedback to users when operations are successful or fail, enhancing the user experience.

Top comments (0)