• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# @ohos.file.statvfs (File System Space Statistics)
2<!--Kit: Core File Kit-->
3<!--Subsystem: FileManagement-->
4<!--Owner: @wangke25; @gsl_1234; @wuchengjun5-->
5<!--Designer: @gsl_1234; @wangke25-->
6<!--Tester: @liuhonggang123; @yue-ye2; @juxiaopang-->
7<!--Adviser: @foryourself-->
8
9This module provides APIs for obtaining file system information, including the total size and free size of a file system, in bytes.
10
11> **NOTE**
12>
13> 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.
14
15## Modules to Import
16
17```ts
18import { statfs } from '@kit.CoreFileKit';
19```
20
21## statfs.getFreeSize
22
23getFreeSize(path:string): Promise&lt;number&gt;
24
25Obtains the free size of the specified file system, in bytes. This API uses a promise to return the result.
26
27**System capability**: SystemCapability.FileManagement.File.FileIO
28
29**Parameters**
30
31  | Name| Type  | Mandatory| Description                        |
32  | ------ | ------ | ---- | ---------------------------- |
33  | path   | string | Yes  | File path of the file system.|
34
35**Return value**
36
37  | Type                 | Description          |
38  | --------------------- | -------------- |
39  | Promise&lt;number&gt; | Promise used to return the free size obtained, in bytes.|
40
41**Error codes**
42
43For details about the error codes, see [Basic File IO Error Codes](errorcode-filemanagement.md#basic-file-io-error-codes).
44
45| ID| Error Message|
46| -------- | -------- |
47| 13900002 | No such file or directory. |
48| 13900004 | Interrupted system call. |
49| 13900005 | I/O error. |
50| 13900008 | Bad file descriptor. |
51| 13900011 | Out of memory. |
52| 13900012 | Permission denied. |
53| 13900013 | Bad address. |
54| 13900018 | Not a directory. |
55| 13900030 | File name too long. |
56| 13900031 | Function not implemented. |
57| 13900033 | Too many symbolic links encountered. |
58| 13900038 | Value too large for defined data type. |
59| 13900042 | Unknown error. |
60
61**Example**
62
63<!--code_no_check-->
64  ```ts
65  import { BusinessError } from '@kit.BasicServicesKit';
66  import { common } from '@kit.AbilityKit';
67
68  // Obtain the context from the component and ensure that the return value of this.getUIContext().getHostContext() is UIAbilityContext.
69  let context = this.getUIContext().getHostContext() as common.UIAbilityContext;
70  let path = context.filesDir;
71  statfs.getFreeSize(path).then((number: number) => {
72    console.info("getFreeSize succeed, Size: " + number);
73  }).catch((err: BusinessError) => {
74    console.error("getFreeSize failed with error message: " + err.message + ", error code: " + err.code);
75  });
76  ```
77
78## statfs.getFreeSize
79
80getFreeSize(path:string, callback:AsyncCallback&lt;number&gt;): void
81
82Obtains the free size of the specified file system, in bytes. This API uses an asynchronous callback to return the result.
83
84**System capability**: SystemCapability.FileManagement.File.FileIO
85
86**Parameters**
87
88  | Name  | Type                       | Mandatory| Description                        |
89  | -------- | --------------------------- | ---- | ---------------------------- |
90  | path     | string                      | Yes  | File path of the file system.|
91  | callback | AsyncCallback&lt;number&gt; | Yes  | Callback used to return the free size obtained, in bytes.|
92
93**Error codes**
94
95For details about the error codes, see [Basic File IO Error Codes](errorcode-filemanagement.md#basic-file-io-error-codes).
96
97| ID| Error Message|
98| -------- | -------- |
99| 13900002 | No such file or directory. |
100| 13900004 | Interrupted system call. |
101| 13900005 | I/O error. |
102| 13900008 | Bad file descriptor. |
103| 13900011 | Out of memory. |
104| 13900012 | Permission denied. |
105| 13900013 | Bad address. |
106| 13900018 | Not a directory. |
107| 13900030 | File name too long. |
108| 13900031 | Function not implemented. |
109| 13900033 | Too many symbolic links encountered. |
110| 13900038 | Value too large for defined data type. |
111| 13900042 | Unknown error. |
112
113**Example**
114
115<!--code_no_check-->
116  ```ts
117  import { BusinessError } from '@kit.BasicServicesKit';
118  import { common } from '@kit.AbilityKit';
119
120  // Obtain the context from the component and ensure that the return value of this.getUIContext().getHostContext() is UIAbilityContext.
121  let context = this.getUIContext().getHostContext() as common.UIAbilityContext;
122  let path = context.filesDir;
123  statfs.getFreeSize(path, (err: BusinessError, number: number) => {
124    if (err) {
125      console.error("getFreeSize failed with error message: " + err.message + ", error code: " + err.code);
126    } else {
127      console.info("getFreeSize succeed, Size: " + number);
128    }
129  });
130  ```
131
132## statfs.getFreeSizeSync<sup>10+</sup>
133
134getFreeSizeSync(path:string): number
135
136Obtains the free size of the specified file system, in bytes. This API returns the result synchronously.
137
138**System capability**: SystemCapability.FileManagement.File.FileIO
139
140**Parameters**
141
142  | Name| Type  | Mandatory| Description                        |
143  | ------ | ------ | ---- | ---------------------------- |
144  | path   | string | Yes  | File path of the file system.|
145
146**Return value**
147
148  | Type                 | Description          |
149  | --------------------- | -------------- |
150  | number | Free size obtained, in bytes.|
151
152**Error codes**
153
154For details about the error codes, see [Basic File IO Error Codes](errorcode-filemanagement.md#basic-file-io-error-codes).
155
156| ID| Error Message|
157| -------- | -------- |
158| 13900002 | No such file or directory. |
159| 13900004 | Interrupted system call. |
160| 13900005 | I/O error. |
161| 13900008 | Bad file descriptor. |
162| 13900011 | Out of memory. |
163| 13900012 | Permission denied. |
164| 13900013 | Bad address. |
165| 13900018 | Not a directory. |
166| 13900030 | File name too long. |
167| 13900031 | Function not implemented. |
168| 13900033 | Too many symbolic links encountered. |
169| 13900038 | Value too large for defined data type. |
170| 13900042 | Unknown error. |
171
172**Example**
173
174<!--code_no_check-->
175  ```ts
176  import { common } from '@kit.AbilityKit';
177
178  // Obtain the context from the component and ensure that the return value of this.getUIContext().getHostContext() is UIAbilityContext.
179  let context = this.getUIContext().getHostContext() as common.UIAbilityContext;
180  let path = context.filesDir;
181  let number = statfs.getFreeSizeSync(path);
182  console.info("getFreeSizeSync succeed, Size: " + number);
183  ```
184
185## statfs.getTotalSize
186
187getTotalSize(path: string): Promise&lt;number&gt;
188
189Obtains the total size of the specified file system, in bytes. This API uses a promise to return the result.
190
191**System capability**: SystemCapability.FileManagement.File.FileIO
192
193**Parameters**
194
195  | Name| Type  | Mandatory| Description                        |
196  | ---- | ------ | ---- | ---------------------------- |
197  | path | string | Yes  | File path of the file system.|
198
199**Return value**
200
201  | Type                 | Description        |
202  | --------------------- | ------------ |
203  | Promise&lt;number&gt; | Promise used to return the total size obtained, in bytes.|
204
205**Error codes**
206
207For details about the error codes, see [Basic File IO Error Codes](errorcode-filemanagement.md#basic-file-io-error-codes).
208
209| ID| Error Message|
210| -------- | -------- |
211| 13900002 | No such file or directory. |
212| 13900004 | Interrupted system call. |
213| 13900005 | I/O error. |
214| 13900008 | Bad file descriptor. |
215| 13900011 | Out of memory. |
216| 13900012 | Permission denied. |
217| 13900013 | Bad address. |
218| 13900018 | Not a directory. |
219| 13900030 | File name too long. |
220| 13900031 | Function not implemented. |
221| 13900033 | Too many symbolic links encountered. |
222| 13900038 | Value too large for defined data type. |
223| 13900042 | Unknown error. |
224
225**Example**
226
227<!--code_no_check-->
228  ```ts
229  import { BusinessError } from '@kit.BasicServicesKit';
230  import { common } from '@kit.AbilityKit';
231
232  // Obtain the context from the component and ensure that the return value of this.getUIContext().getHostContext() is UIAbilityContext.
233  let context = this.getUIContext().getHostContext() as common.UIAbilityContext;
234  let path = context.filesDir;
235  statfs.getTotalSize(path).then((number: number) => {
236    console.info("getTotalSize succeed, Size: " + number);
237  }).catch((err: BusinessError) => {
238    console.error("getTotalSize failed with error message: " + err.message + ", error code: " + err.code);
239  });
240  ```
241
242## statfs.getTotalSize
243
244getTotalSize(path: string, callback: AsyncCallback&lt;number&gt;): void
245
246Obtains the total size of the specified file system, in bytes. This API uses an asynchronous callback to return the result.
247
248**System capability**: SystemCapability.FileManagement.File.FileIO
249
250**Parameters**
251
252  | Name  | Type                       | Mandatory| Description                        |
253  | -------- | --------------------------- | ---- | ---------------------------- |
254  | path     | string                      | Yes  | File path of the file system.|
255  | callback | AsyncCallback&lt;number&gt; | Yes  | Callback used to return the total size obtained, in bytes.  |
256
257**Error codes**
258
259For details about the error codes, see [Basic File IO Error Codes](errorcode-filemanagement.md#basic-file-io-error-codes).
260
261| ID| Error Message|
262| -------- | -------- |
263| 13900002 | No such file or directory. |
264| 13900004 | Interrupted system call. |
265| 13900005 | I/O error. |
266| 13900008 | Bad file descriptor. |
267| 13900011 | Out of memory. |
268| 13900012 | Permission denied. |
269| 13900013 | Bad address. |
270| 13900018 | Not a directory. |
271| 13900030 | File name too long. |
272| 13900031 | Function not implemented. |
273| 13900033 | Too many symbolic links encountered. |
274| 13900038 | Value too large for defined data type. |
275| 13900042 | Unknown error. |
276
277**Example**
278
279<!--code_no_check-->
280  ```ts
281  import { BusinessError } from '@kit.BasicServicesKit';
282  import { common } from '@kit.AbilityKit';
283
284  // Obtain the context from the component and ensure that the return value of this.getUIContext().getHostContext() is UIAbilityContext.
285  let context = this.getUIContext().getHostContext() as common.UIAbilityContext;
286  let path = context.filesDir;
287  statfs.getTotalSize(path, (err: BusinessError, number: number) => {
288    if (err) {
289      console.error("getTotalSize failed with error message: " + err.message + ", error code: " + err.code);
290    } else {
291      console.info("getTotalSize succeed, Size: " + number);
292    }
293  });
294  ```
295
296## statfs.getTotalSizeSync<sup>10+</sup>
297
298getTotalSizeSync(path: string): number
299
300Obtains the total size of the specified file system, in bytes. This API returns the result synchronously.
301
302**System capability**: SystemCapability.FileManagement.File.FileIO
303
304**Parameters**
305
306  | Name| Type  | Mandatory| Description                        |
307  | ---- | ------ | ---- | ---------------------------- |
308  | path | string | Yes  | File path of the file system.|
309
310**Return value**
311
312  | Type                 | Description        |
313  | --------------------- | ------------ |
314  | number | Total size obtained, in bytes.|
315
316**Error codes**
317
318For details about the error codes, see [Basic File IO Error Codes](errorcode-filemanagement.md#basic-file-io-error-codes).
319
320| ID| Error Message|
321| -------- | -------- |
322| 13900002 | No such file or directory. |
323| 13900004 | Interrupted system call. |
324| 13900005 | I/O error. |
325| 13900008 | Bad file descriptor. |
326| 13900011 | Out of memory. |
327| 13900012 | Permission denied. |
328| 13900013 | Bad address. |
329| 13900018 | Not a directory. |
330| 13900030 | File name too long. |
331| 13900031 | Function not implemented. |
332| 13900033 | Too many symbolic links encountered. |
333| 13900038 | Value too large for defined data type. |
334| 13900042 | Unknown error. |
335
336**Example**
337
338<!--code_no_check-->
339  ```ts
340  import { common } from '@kit.AbilityKit';
341
342  // Obtain the context from the component and ensure that the return value of this.getUIContext().getHostContext() is UIAbilityContext.
343  let context = this.getUIContext().getHostContext() as common.UIAbilityContext;
344  let path = context.filesDir;
345  let number = statfs.getTotalSizeSync(path);
346  console.info("getTotalSizeSync succeed, Size: " + number);
347  ```
348