harmony 鸿蒙Obtaining Application and File System Space Statistics

  • 2023-06-24
  • 浏览 (545)

Obtaining Application and File System Space Statistics

This topic describes how to obtain information about the free system space and space occupied by applications so as to prevent insufficient storage space of the system or ensure proper use of quota-controlled cacheDir directories.

Available APIs

For details about the APIs, see ohos.file.statvfs and ohos.file.storageStatistics.

Table 1 APIs for file system and application space statistics

Module API Description
\@ohos.file.storageStatistics getCurrentBundleStats Obtains the storage space of the current application, in bytes.
\@ohos.file.statvfs getFreeSize Obtains the free space of a file system, in bytes.
\@ohos.file.statvfs getTotalSize Obtains the total space of a file system, in bytes.

Table 2 Attributes for application space statistics

BundleStats Attribute Description Directory for Statistics
appSize Size of the application installation files, in bytes. /data/storage/el1/bundle
cacheSize Size of the application cache files, in bytes. /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 the application files (excluding the application installation files and cache files), in bytes. The application files include local files, distributed files, and database files.
- Local application file directories (parent directories of the cache directories):
/data/storage/el1/base
/data/storage/el2/base
- Distributed application directory: /data/storage/el2/distributedfiles
- Database directories:
/data/storage/el1/database
/data/storage/el2/database

Development Example

  • Obtain the free space of /data of the file system.
  import statvfs from '@ohos.file.statvfs';
  import { BusinessError } from '@ohos.base';
  
  let path = "/data";
  statvfs.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}`);
    }
  });
  • Obtain the space occupied by the current application.
  import storageStatistics from "@ohos.file.storageStatistics";
  import { BusinessError } from '@ohos.base';
  
  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}`);
    }
  });

你可能感兴趣的鸿蒙文章

harmony 鸿蒙File Management

harmony 鸿蒙Accessing Application Files

harmony 鸿蒙Backup and Restoration Accessed by Applications

harmony 鸿蒙Application Data Backup and Restoration Overview

harmony 鸿蒙Backup and Restoration Triggered by System Applications

harmony 鸿蒙Application File Overview

harmony 鸿蒙Uploading and Downloading an Application File

harmony 鸿蒙Application Sandbox Directory

harmony 鸿蒙Developing a File Manager Application (for System Applications Only)

harmony 鸿蒙Distributed File System Overview

0  赞