1# @ohos.file.statvfs (File System Space Statistics)
2
3The **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
12import { statfs } from '@kit.CoreFileKit';
13```
14
15## statfs.getFreeSize
16
17getFreeSize(path:string):Promise<number>
18
19Obtains 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
37For details about the error codes, see [Basic File IO Error Codes](errorcode-filemanagement.md#basic-file-io-error-codes).
38
39**Example**
40
41  ```ts
42  import { BusinessError } from '@kit.BasicServicesKit';
43  import { common } from '@kit.AbilityKit';
44
45  let context = getContext(this) as common.UIAbilityContext;
46  let path = context.filesDir;
47  statfs.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## statfs.getFreeSize
55
56getFreeSize(path:string, callback:AsyncCallback<number>): void
57
58Obtains 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 used to return the free size obtained, in bytes.|
68
69**Error codes**
70
71For details about the error codes, see [Basic File IO Error Codes](errorcode-filemanagement.md#basic-file-io-error-codes).
72
73**Example**
74
75  ```ts
76  import { BusinessError } from '@kit.BasicServicesKit';
77  import { common } from '@kit.AbilityKit';
78
79  let context = getContext(this) as common.UIAbilityContext;
80  let path = context.filesDir;
81  statfs.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## statfs.getFreeSizeSync<sup>10+</sup>
91
92getFreeSizeSync(path:string): number
93
94Obtains 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
112For details about the error codes, see [Basic File IO Error Codes](errorcode-filemanagement.md#basic-file-io-error-codes).
113
114**Example**
115
116  ```ts
117  import { common } from '@kit.AbilityKit';
118
119  let context = getContext(this) as common.UIAbilityContext;
120  let path = context.filesDir;
121  let number = statfs.getFreeSizeSync(path);
122  console.info("getFreeSizeSync succeed, Size: " + number);
123  ```
124
125## statfs.getTotalSize
126
127getTotalSize(path: string): Promise&lt;number&gt;
128
129Obtains 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
147For details about the error codes, see [Basic File IO Error Codes](errorcode-filemanagement.md#basic-file-io-error-codes).
148
149**Example**
150
151  ```ts
152  import { BusinessError } from '@kit.BasicServicesKit';
153  import { common } from '@kit.AbilityKit';
154
155  let context = getContext(this) as common.UIAbilityContext;
156  let path = context.filesDir;
157  statfs.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## statfs.getTotalSize
165
166getTotalSize(path: string, callback: AsyncCallback&lt;number&gt;): void
167
168Obtains 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 used to return the total size obtained, in bytes.  |
178
179**Error codes**
180
181For details about the error codes, see [Basic File IO Error Codes](errorcode-filemanagement.md#basic-file-io-error-codes).
182
183**Example**
184
185  ```ts
186  import { BusinessError } from '@kit.BasicServicesKit';
187  import { common } from '@kit.AbilityKit';
188
189  let context = getContext(this) as common.UIAbilityContext;
190  let path = context.filesDir;
191  statfs.getTotalSize(path, (err: BusinessError, number: number) => {
192    if (err) {
193      console.error("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## statfs.getTotalSizeSync<sup>10+</sup>
201
202getTotalSizeSync(path: string): number
203
204Obtains 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
222For details about the error codes, see [Basic File IO Error Codes](errorcode-filemanagement.md#basic-file-io-error-codes).
223
224**Example**
225
226  ```ts
227  import { common } from '@kit.AbilityKit';
228
229  let context = getContext(this) as common.UIAbilityContext;
230  let path = context.filesDir;
231  let number = statfs.getTotalSizeSync(path);
232  console.info("getTotalSizeSync succeed, Size: " + number);
233  ```
234