• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 # @ohos.file.statvfs (File System Space Statistics)
2 
3 The **statfs** module provides APIs for obtaining file system information, including the total size and free size a file system, in bytes.
4 
5 > **NOTE**
6 >
7 > The initial APIs of this module are supported since API version 9. Newly added APIs will be marked with a superscript to indicate their earliest API version.
8 
9 ## Modules to Import
10 
11 ```ts
12 import statvfs from '@ohos.file.statvfs';
13 ```
14 
15 ## statvfs.getFreeSize
16 
17 getFreeSize(path:string):Promise<number>
18 
19 Obtains the free size of the specified file system in bytes. This API uses a promise to return the result.
20 
21 **System capability**: SystemCapability.FileManagement.File.FileIO
22 
23 **Parameters**
24 
25   | Name| Type  | Mandatory| Description                        |
26   | ------ | ------ | ---- | ---------------------------- |
27   | path   | string | Yes  | File path of the file system.|
28 
29 **Return value**
30 
31   | Type                 | Description          |
32   | --------------------- | -------------- |
33   | Promise<number> | Promise used to return the free size obtained, in bytes.|
34 
35 **Error codes**
36 
37 For details about the error codes, see [Basic File IO Error Codes](../errorcodes/errorcode-filemanagement.md#basic-file-io-error-codes).
38 
39 **Example**
40 
41   ```ts
42   import { BusinessError } from '@ohos.base';
43   import common from '@ohos.app.ability.common';
44 
45   let context = getContext(this) as common.UIAbilityContext;
46   let path = context.filesDir;
47   statvfs.getFreeSize(path).then((number: number) => {
48     console.info("getFreeSize succeed, Size: " + number);
49   }).catch((err: BusinessError) => {
50     console.error("getFreeSize failed with error message: " + err.message + ", error code: " + err.code);
51   });
52   ```
53 
54 ## statvfs.getFreeSize
55 
56 getFreeSize(path:string, callback:AsyncCallback<number>): void
57 
58 Obtains the free size of the specified file system in bytes. This API uses an asynchronous callback to return the result.
59 
60 **System capability**: SystemCapability.FileManagement.File.FileIO
61 
62 **Parameters**
63 
64   | Name  | Type                       | Mandatory| Description                        |
65   | -------- | --------------------------- | ---- | ---------------------------- |
66   | path     | string                      | Yes  | File path of the file system.|
67   | callback | AsyncCallback<number> | Yes  | Callback invoked to return the free size obtained, in bytes.|
68 
69 **Error codes**
70 
71 For details about the error codes, see [Basic File IO Error Codes](../errorcodes/errorcode-filemanagement.md#basic-file-io-error-codes).
72 
73 **Example**
74 
75   ```ts
76   import { BusinessError } from '@ohos.base';
77   import common from '@ohos.app.ability.common';
78 
79   let context = getContext(this) as common.UIAbilityContext;
80   let path = context.filesDir;
81   statvfs.getFreeSize(path, (err: BusinessError, number: number) => {
82     if (err) {
83       console.error("getFreeSize failed with error message: " + err.message + ", error code: " + err.code);
84     } else {
85       console.info("getFreeSize succeed, Size: " + number);
86     }
87   });
88   ```
89 
90 ## statvfs.getFreeSizeSync<sup>10+</sup>
91 
92 getFreeSizeSync(path:string): number
93 
94 Obtains the free size of the specified file system in bytes. This API returns the result synchronously.
95 
96 **System capability**: SystemCapability.FileManagement.File.FileIO
97 
98 **Parameters**
99 
100   | Name| Type  | Mandatory| Description                        |
101   | ------ | ------ | ---- | ---------------------------- |
102   | path   | string | Yes  | File path of the file system.|
103 
104 **Return value**
105 
106   | Type                 | Description          |
107   | --------------------- | -------------- |
108   | number | Free size obtained, in bytes.|
109 
110 **Error codes**
111 
112 For details about the error codes, see [Basic File IO Error Codes](../errorcodes/errorcode-filemanagement.md#basic-file-io-error-codes).
113 
114 **Example**
115 
116   ```ts
117   import common from '@ohos.app.ability.common';
118 
119   let context = getContext(this) as common.UIAbilityContext;
120   let path = context.filesDir;
121   let number = statvfs.getFreeSizeSync(path);
122   console.info("getFreeSizeSync succeed, Size: " + number);
123   ```
124 
125 ## statvfs.getTotalSize
126 
127 getTotalSize(path: string): Promise&lt;number&gt;
128 
129 Obtains the total size of the specified file system in bytes. This API uses a promise to return the result.
130 
131 **System capability**: SystemCapability.FileManagement.File.FileIO
132 
133 **Parameters**
134 
135   | Name| Type  | Mandatory| Description                        |
136   | ---- | ------ | ---- | ---------------------------- |
137   | path | string | Yes  | File path of the file system.|
138 
139 **Return value**
140 
141   | Type                 | Description        |
142   | --------------------- | ------------ |
143   | Promise&lt;number&gt; | Promise used to return the total size obtained, in bytes.|
144 
145 **Error codes**
146 
147 For details about the error codes, see [Basic File IO Error Codes](../errorcodes/errorcode-filemanagement.md#basic-file-io-error-codes).
148 
149 **Example**
150 
151   ```ts
152   import { BusinessError } from '@ohos.base';
153   import common from '@ohos.app.ability.common';
154 
155   let context = getContext(this) as common.UIAbilityContext;
156   let path = context.filesDir;
157   statvfs.getTotalSize(path).then((number: number) => {
158     console.info("getTotalSize succeed, Size: " + number);
159   }).catch((err: BusinessError) => {
160     console.error("getTotalSize failed with error message: " + err.message + ", error code: " + err.code);
161   });
162   ```
163 
164 ## statvfs.getTotalSize
165 
166 getTotalSize(path: string, callback: AsyncCallback&lt;number&gt;): void
167 
168 Obtains the total size of the specified file system in bytes. This API uses an asynchronous callback to return the result.
169 
170 **System capability**: SystemCapability.FileManagement.File.FileIO
171 
172 **Parameters**
173 
174   | Name  | Type                       | Mandatory| Description                        |
175   | -------- | --------------------------- | ---- | ---------------------------- |
176   | path     | string                      | Yes  | File path of the file system.|
177   | callback | AsyncCallback&lt;number&gt; | Yes  | Callback invoked to return the total size obtained, in bytes.  |
178 
179 **Error codes**
180 
181 For details about the error codes, see [Basic File IO Error Codes](../errorcodes/errorcode-filemanagement.md#basic-file-io-error-codes).
182 
183 **Example**
184 
185   ```ts
186   import { BusinessError } from '@ohos.base';
187   import common from '@ohos.app.ability.common';
188 
189   let context = getContext(this) as common.UIAbilityContext;
190   let path = context.filesDir;
191   statvfs.getTotalSize(path, (err: BusinessError, number: number) => {
192     if (err) {
193       console.info("getTotalSize failed with error message: " + err.message + ", error code: " + err.code);
194     } else {
195       console.info("getTotalSize succeed, Size: " + number);
196     }
197   });
198   ```
199 
200 ## statvfs.getTotalSizeSync<sup>10+</sup>
201 
202 getTotalSizeSync(path: string): number
203 
204 Obtains the total size of the specified file system in bytes. This API returns the result synchronously.
205 
206 **System capability**: SystemCapability.FileManagement.File.FileIO
207 
208 **Parameters**
209 
210   | Name| Type  | Mandatory| Description                        |
211   | ---- | ------ | ---- | ---------------------------- |
212   | path | string | Yes  | File path of the file system.|
213 
214 **Return value**
215 
216   | Type                 | Description        |
217   | --------------------- | ------------ |
218   | number | Total size obtained, in bytes.|
219 
220 **Error codes**
221 
222 For details about the error codes, see [Basic File IO Error Codes](../errorcodes/errorcode-filemanagement.md#basic-file-io-error-codes).
223 
224 **Example**
225 
226   ```ts
227   import common from '@ohos.app.ability.common';
228 
229   let context = getContext(this) as common.UIAbilityContext;
230   let path = context.filesDir;
231   let number = statvfs.getTotalSizeSync(path);
232   console.info("getTotalSizeSync succeed, Size: " + number);
233   ```
234