app-fs-space-statistics.md 2.7 KB
Newer Older
A
Annie_wang 已提交
1 2 3 4 5 6 7 8 9 10 11 12
# 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](../reference/apis/js-apis-file-statvfs.md) and [ohos.file.storageStatistics](../reference/apis/js-apis-file-storage-statistics.md).

**Table 1** APIs for file system and application space statistics

| Module| API| Description|
| -------- | -------- | -------- |
A
Annie_wang 已提交
13 14 15
| \@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.|
A
Annie_wang 已提交
16 17 18

**Table 2** Attributes for application space statistics

A
Annie_wang 已提交
19
| BundleStats Attribute| Description| Directory for Statistics|
A
Annie_wang 已提交
20
| -------- | -------- | -------- |
A
Annie_wang 已提交
21 22 23
| 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<br>/data/storage/el1/base/haps/entry/cache<br>/data/storage/el2/base/cache<br>/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.<br>- Local application file directories (parent directories of the **cache** directories):<br>**/data/storage/el1/base**<br>**/data/storage/el2/base**<br>- Distributed application directory: **/data/storage/el2/distributedfiles**<br>- Database directories:<br>**/data/storage/el1/database**<br>**/data/storage/el2/database** |
A
Annie_wang 已提交
24 25 26 27

## Development Example

- Obtain the free space of **/data** of the file system.
A
Annie_wang 已提交
28
  
A
Annie_wang 已提交
29 30 31 32 33 34 35 36 37 38 39 40 41 42
  ```ts
  import statvfs from '@ohos.file.statvfs';
  
  let path = "/data";
  statvfs.getFreeSize(path, (err, 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.
A
Annie_wang 已提交
43
  
A
Annie_wang 已提交
44 45 46 47 48 49 50 51 52 53 54
  ```ts
  import storageStatistics from "@ohos.file.storageStatistics";
  
  storageStatistics.getCurrentBundleStats((err, 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}`);
    }
  });
  ```