initialize

This commit is contained in:
罗明
2025-08-08 14:47:49 +08:00
parent 9f254489da
commit 88c09d7ac8
18 changed files with 2967 additions and 2 deletions
+4
View File
@@ -0,0 +1,4 @@
**/.vscode
**/.cache
**/cjpm.lock
**/.idea
+49 -2
View File
@@ -1,3 +1,50 @@
# filemanagement_filemanagement_cangjie_wrapper
# File Management
文件管理模块仓颉封装层
## Introduction
The file management subsystem provides a complete file management solution for OpenHarmony. It provides secure and easy-to-use file access and comprehensive file management capabilities, including:
- A sandbox to ensure the least privilege as well as application data security
- Unified management of user files and streamlined user data access and storage to ensure user data security and purity
- Access framework to allow applications to access distributed file system files and cloud files as they access local files
- Sharing of user data and system files across applications and devices
- System storage management capabilities and basic file system capabilities
**Figure 1** Architecture of the file management subsystem
![](figures/file_management_architecture.png "file management subsystem architecture")
The file management subsystem provides the file access framework, file sharing framework, and storage management framework for applications.
| Module | Description |
| ------------ | ------------------------------------------------------------ |
| File access interface| 1. Provides complete JavaScript APIs to implement basic file access capabilities.<br>2. Provides extension APIs for local and distributed files.|
| Storage management | 1. Provides data backup and restore to support system and application data backup and cloning.<br>2. Provides space management capabilities such as application space clearing and statistics, and quota control.<br>3. Provides storage management capabilities such as mount operations, external card management, device management, and multi-user management.|
| User files | 1. Provides a sandbox to ensure user data security and purity.<br>2. Allows access to user data only through **mediaLibrary**.<br>3. Provides a unified file management framework.|
| Application files | 1. Provides a sandbox to ensure the least privilege as well as application data security.<br>2. Supports file sharing between applications, across devices, and in groups.|
| Distributed capabilities | 1. Provides basic cross-device access capabilities and supports distributed access using the same account and temporary access using different accounts.<br>2. Supports cross-device hopping, such as application hopping and distributed pasteboard.|
| Basic file system| 1. Supports local file systems such as ext4, Flash-Friendly File System (F2FS), Extensible File Allocation Table (exFAT), and New Technology File System (NTFS).<br>2. Supports network file systems such as the distributed file system and Network File System (NFS).<br>3. Provides tools related to file systems.|
## Directory Structure
```
foundation/filemanagement/filemanagement_cangjie_api
├── ohos # Cangjie File Management code
├── kit # Cangjie kit code
├── figures # architecture pictures
```
## Repositories Involved
**User file management**
[filemanagement_user_file_service](https://gitee.com/openharmony/filemanagement_user_file_service)
**Application file management**
[filemanagement_app_file_service](https://gitee.com/openharmony/filemanagement_app_file_service)
**File management APIs**
filemanagement_cangjie_api
[filemanagement_file_api](https://gitee.com/openharmony/filemanagement_file_api)
+50
View File
@@ -0,0 +1,50 @@
# 文件管理子系统
## 简介
文件管理子系统为OpenHarmony提供一套完整的文件数据管理解决方案,提供安全、易用的文件访问能力和完善的文件存储管理能力,包括:
1. 为应用提供安全的沙箱隔离技术,保证应用数据安全基础上权限最小化;
2. 统一的公共文件管理能力,统一公共数据访问入库,保证用户数据安全、纯净;
3. 分布式文件系统和云接入文件系统访问框架,应用可以像使用本地文件一样使用分布式和云端文件;
4. 支持公共数据、跨应用、跨设备的系统级文件分享能力;
5. 提供系统的存储管理能力和基础文件系统能力。
**图 1** 文件管理子系统架构图
![](figures/文件管理子系统架构图.jpg "文件管理子系统架构图")
文件管理子系统对应用提供文件访问框架、文件分享框架、存储管理框架能力。
| 模块 | 详细描述 |
| ------------ | ------------------------------------------------------------ |
| 文件访问接口 | 1. 提供完整文件JS 接口,支持基础文件访问能力; <br/>2. 提供本地文件、分布式文件扩展接口。 |
| 存储管理 | 1. 提供数据备份恢复框架能力,支持系统和应用数据备份克隆等场景; <br/>2. 提供应用空间清理和统计、配额管控等空间管理能力;<br/>3. 提供挂载管理、外卡管理、设备管理及多用户管理等存储存储管理能力。 |
| 公共文件 | 1. 公共数据沙箱隔离,保证用户数据安全、纯净; <br/>2. 统一公共数据访问入口,仅medialibrary <br/>3. 提供统一的FMF的文件管理框架。 |
| 应用文件 | 1. 为应用提供安全的沙箱隔离技术,保证应用数据安全基础上权限最小化; <br/>2. 支持应用间文件分享和文件跨设备分享,支持群组分享。 |
| 分布式能力 | 1. 提供基础分布式跨端访问能力,支持同账号分布式访问和异账号临时访问; <br/>2. 支持文件跨端迁移能力,支撑应用迁移、分布式剪切板等分布式场景。 |
| 基础文件系统 | 1. 支持ext4、f2fs、exfat、ntfs等本地文件系统; <br/>2. 支持分布式文件系统、nfs等网络文件系统;<br/>3. 文件系统相关工具。 |
## 目录
```
foundation/filemanagement/filemanagement_cangjie_api
├── ohos # 仓颉文件管理接口实现
├── kit # 仓颉kit化代码
├── figures # 存放readme中的架构图
```
## 相关仓
**公共文件管理**
[filemanagement_user_file_service](https://gitee.com/openharmony/filemanagement_user_file_service)
**应用文件管理**
[filemanagement_app_file_service](https://gitee.com/openharmony/filemanagement_app_file_service)
**文件接口**
filemanagement_cangjie_api
[filemanagement_file_api](https://gitee.com/openharmony/filemanagement_file_api)
+42
View File
@@ -0,0 +1,42 @@
{
"name": "@ohos/filemanagement_cangjie_api",
"description": "Provides the application with cangjie interfaces for filemanagement",
"version": "6.0",
"license": "Apache-2.0",
"publishAs": "code-segment",
"segment": {
"destPath": "foundation/filemanagement/filemanagement_cangjie_api"
},
"dirs": {},
"scripts": {},
"component": {
"name": "filemanagement_cangjie_api",
"subsystem": "filemanagement",
"syscap": [
"SystemCapability.FileManagement.File.FileIO",
"SystemCapability.FileManagement.AppFileService"
],
"features": [],
"adapted_system_type": [
"standard"
],
"rom": "30000KB",
"ram": "100000KB",
"deps": {
"components": [
"cangjie_api"
]
},
"build": {
"sub_component": [
"//foundation/filemanagement/filemanagement_cangjie_api/kit/CoreFileKit:kit.CoreFileKit",
"//foundation/filemanagement/filemanagement_cangjie_api/ohos/file_fileuri:ohos.file_fileuri",
"//foundation/filemanagement/filemanagement_cangjie_api/ohos/file_fs:ohos.file_fs"
],
"inner_kits": [
],
"test": [
]
}
}
}
Binary file not shown.

After

Width:  |  Height:  |  Size: 41 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 80 KiB

+29
View File
@@ -0,0 +1,29 @@
# Copyright (c) 2025 Huawei Device Co., Ltd.
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import("//build/ohos.gni")
import("//build/cangjie/cjc.gni")
ohos_cj_library("kit.CoreFileKit") {
sources = [ "index.cj" ]
cj_deps = [
"../../ohos/file_fs:ohos.file_fs",
"../../ohos/file_fileuri:ohos.file_fileuri",
]
kit = true
subsystem_name = "filemanagement"
part_name = "filemanagement_cangjie_api"
lib_name = "CoreFileKit"
}
+19
View File
@@ -0,0 +1,19 @@
/*
* Copyright (c) 2025 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package kit.CoreFileKit
public import ohos.file_fs.*
public import ohos.file_fileuri.*
+32
View File
@@ -0,0 +1,32 @@
# Copyright (c) 2025 Huawei Device Co., Ltd.
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import("//build/ohos.gni")
import("//build/cangjie/cjc.gni")
ohos_cj_library("ohos.file_fileuri") {
sources = [ "file_uri.cj" ]
cj_external_deps = [
"arkui_cangjie_api:ohos.base",
"cangjie_api:ohos.ffi",
"cangjie_api:ohos.labels",
"hiviewdfx_cangjie_api:ohos.hilog",
]
external_deps = [ "app_file_service:cj_file_fileuri_ffi" ]
subsystem_name = "filemanagement"
part_name = "filemanagement_cangjie_api"
lib_name = "file_fileuri"
}
+172
View File
@@ -0,0 +1,172 @@
/*
* Copyright (c) 2025 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package ohos.file_fileuri
import ohos.ffi.*
import ohos.hilog.*
import ohos.base.*
import ohos.labels.*
foreign func FfiOHOSFILEUriCreateUri(uriOrPath: CString): Int64
foreign func FfiOHOSFILEUriGetPath(id: Int64): CString
foreign func FfiOHOSFILEUriGetName(id: Int64): CString
foreign func FfiOHOSFILEUriToString(id: Int64): CString
foreign func FfiOHOSFILEUriGetUriFromPath(cPath: CString): CString
@!APILevel[
21,
atomicservice: true,
stagemodelonly: true,
syscap: "SystemCapability.FileManagement.AppFileService"
]
public class FileUri <: RemoteDataLite & ToString {
/**
* Constructor for obtaining the instance of the FileUri class.
*
* @param { String } uriOrPath - Uri or Path.
* @since 10
* @brief constructor(uriOrPath: string)
*/
@!APILevel[
21,
atomicservice: true,
stagemodelonly: true,
syscap: "SystemCapability.FileManagement.AppFileService"
]
public init(uriOrPath: String) {
super(
unsafe {
if (uriOrPath == "") {
Hilog.error(0xD003900, "File-FileUir", "Invalid argument")
throw BusinessException(13900020, "Invalid argument")
}
let uriCString = LibC.mallocCString(uriOrPath)
let id = FfiOHOSFILEUriCreateUri(uriCString)
LibC.free(uriCString)
if (id < 0) {
Hilog.error(0xD003900, "File-FileUir", "Out of memory")
throw BusinessException(13900011, "Out of memory")
}
id
})
}
~init() {
releaseFFIData(myDataId)
}
/**
* Obtains the file path of uri.
*
* @type { String }
* @readonly
* @brief readonly path: string
*/
@!APILevel[
21,
atomicservice: true,
stagemodelonly: true,
syscap: "SystemCapability.FileManagement.AppFileService"
]
public prop path: String {
get() {
unsafe {
let cPath = FfiOHOSFILEUriGetPath(getID())
let resultPath = cPath.toString()
LibC.free(cPath)
return resultPath
}
}
}
/**
* Obtains the file name of uri.
*
* @type { String }
* @readonly
* @brief readonly name: string
*/
@!APILevel[
21,
atomicservice: true,
stagemodelonly: true,
syscap: "SystemCapability.FileManagement.AppFileService"
]
public prop name: String {
get() {
unsafe {
let cName = FfiOHOSFILEUriGetName(getID())
let resultName = cName.toString()
LibC.free(cName)
return resultName
}
}
}
/**
* Get the uri string
*
* @returns { string } Return the file uri string
* @brief toString(): string
*/
@!APILevel[
21,
atomicservice: true,
stagemodelonly: true,
syscap: "SystemCapability.FileManagement.AppFileService"
]
public func toString(): String {
unsafe {
let cStr = FfiOHOSFILEUriToString(getID())
let str = cStr.toString()
LibC.free(cStr)
return str
}
}
/**
* Get the uri from the path of file in app sandbox
*
* @param { string } path the path of file in app sandbox
* @returns { string } Return the file uri
* @brief getUriFromPath(path: string): string
*/
@!APILevel[
21,
atomicservice: true,
stagemodelonly: true,
syscap: "SystemCapability.FileManagement.AppFileService"
]
public static func getUriFromPath(path: String): String {
if (path == "") {
Hilog.error(0xD003900, "File-FileUir", "The input parameter is invalid")
throw BusinessException(ERR_PARAMETER_ERROR, "The input parameter is invalid")
}
unsafe {
let cPath = LibC.mallocCString(path)
let cUri = FfiOHOSFILEUriGetUriFromPath(cPath)
LibC.free(cPath)
let str = cUri.toString()
LibC.free(cUri)
return str
}
}
}
+40
View File
@@ -0,0 +1,40 @@
# Copyright (c) 2025 Huawei Device Co., Ltd.
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import("//build/ohos.gni")
import("//build/cangjie/cjc.gni")
ohos_cj_library("ohos.file_fs") {
sources = [
"cj_file_fs.cj",
"conflict_file_exception.cj",
"file.cj",
"native.cj",
"random_access_file.cj",
"stat.cj",
"stream.cj",
]
cj_external_deps = [
"arkui_cangjie_api:ohos.base",
"cangjie_api:ohos.ffi",
"cangjie_api:ohos.labels",
"hiviewdfx_cangjie_api:ohos.hilog",
]
external_deps = [ "file_api:cj_file_fs_ffi" ]
subsystem_name = "filemanagement"
part_name = "filemanagement_cangjie_api"
lib_name = "file_fs"
}
File diff suppressed because it is too large Load Diff
+95
View File
@@ -0,0 +1,95 @@
/*
* Copyright (c) 2025 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package ohos.file_fs
import ohos.ffi.*
import ohos.base.*
import ohos.labels.*
import std.collection.*
const FILEIO_DOMAIN_ID = 0xD004388u32
const FILEIO_INVALID_ARGUMENT = 13900020i32
const INVALID_ARGS_CODE: Int32 = 13900020
let ERROR_CODE_MAP: HashMap<Int32, String> = HashMap<Int32, String>(
(13900001, "Operation not permitted"),
(13900002, "No such file or directory"),
(13900003, "No such process"),
(13900004, "Interrupted system call"),
(13900005, "I/O error"),
(13900006, "No such device or address"),
(13900007, "Arg list too long"),
(13900008, "Bad file descriptor"),
(13900009, "No child processes"),
(13900010, "Try again"),
(13900011, "Out of memory"),
(13900012, "Permission denied"),
(13900013, "Bad address"),
(13900014, "Device or resource busy"),
(13900015, "File exists"),
(13900016, "Cross-device link"),
(13900017, "No such device"),
(13900018, "Not a directory"),
(13900019, "Is a directory"),
(13900020, "Invalid argument"),
(13900021, "File table overflow"),
(13900022, "Too many open files"),
(13900023, "Text file busy"),
(13900024, "File too large"),
(13900025, "No space left on device"),
(13900026, "Illegal seek"),
(13900027, "Read-only file system"),
(13900028, "Too many links"),
(13900029, "Resource deadlock would occur"),
(13900030, "Filename too Long"),
(13900031, "Function not implemented"),
(13900032, "Directory not empty"),
(13900033, "Too many symbolic links encountered"),
(13900034, "Operation would block"),
(13900035, "Invalid request descriptor"),
(13900036, "Device not a stream"),
(13900037, "No data available"),
(13900038, "Value too large for defined data type"),
(13900039, "File descriptor in bad state"),
(13900040, "Interrupted system call should be restarted"),
(13900041, "Quota exceeded"),
(13900042, "Unknown error")
)
func getErrorInfo(code: Int32): String {
if (ERROR_CODE_MAP.contains(code)) {
return ERROR_CODE_MAP[code]
} else {
return "Unknown error"
}
}
@!APILevel[
21,
stagemodelonly: true,
syscap: "SystemCapability.FileManagement.File.FileIO"
]
public class ConflictFileException <: BusinessException {
@!APILevel[
21,
stagemodelonly: true,
syscap: "SystemCapability.FileManagement.File.FileIO"
]
public let data: Array<ConflictFiles>
init(code: Int32, msg: String, data: Array<ConflictFiles>) {
super(code, msg)
this.data = data
}
}
+128
View File
@@ -0,0 +1,128 @@
/*
* Copyright (c) 2025 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package ohos.file_fs
import ohos.ffi.*
import ohos.base.*
import ohos.labels.*
@!APILevel[
21,
atomicservice: true,
crossplatform: true,
stagemodelonly: true,
syscap: "SystemCapability.FileManagement.File.FileIO"
]
public class File <: RemoteDataLite {
init(instanceId: Int64) {
super(instanceId)
}
~init() {
releaseFFIData(myDataId)
}
@!APILevel[
21,
atomicservice: true,
crossplatform: true,
stagemodelonly: true,
syscap: "SystemCapability.FileManagement.File.FileIO"
]
public prop fd: Int32 {
get() {
unsafe { FfiOHOSFILEFsGetFD(getID()) }
}
}
@!APILevel[
21,
stagemodelonly: true,
syscap: "SystemCapability.FileManagement.File.FileIO"
]
public prop path: String {
get() {
let cStr = unsafe { FfiOHOSFILEFsGetPath(getID()) }
let ret = cStr.toString()
unsafe { LibC.free(cStr) }
return ret
}
}
@!APILevel[
21,
stagemodelonly: true,
syscap: "SystemCapability.FileManagement.File.FileIO"
]
public prop name: String {
get() {
let cStr = unsafe { FfiOHOSFILEFsGetName(getID()) }
let ret = cStr.toString()
unsafe { LibC.free(cStr) }
return ret
}
}
@!APILevel[
21,
stagemodelonly: true,
syscap: "SystemCapability.FileManagement.File.FileIO"
]
public func tryLock(exclusive!: Bool = false): Unit {
let code = unsafe { FfiOHOSFILEFsTryLock(getID(), exclusive) }
if (code != SUCCESS_CODE) {
FS_LOG.error(getErrorInfo(code))
throw BusinessException(code, getErrorInfo(code))
}
}
@!APILevel[
21,
stagemodelonly: true,
syscap: "SystemCapability.FileManagement.File.FileIO"
]
public func unLock(): Unit {
let code = unsafe { FfiOHOSFILEFsUnLock(getID()) }
if (code != SUCCESS_CODE) {
FS_LOG.error(getErrorInfo(code))
throw BusinessException(code, getErrorInfo(code))
}
}
/**
* @brief getParent(): string
*/
@!APILevel[
21,
stagemodelonly: true,
syscap: "SystemCapability.FileManagement.File.FileIO"
]
public func getParent(): String {
let ret: RetDataCString
unsafe {
ret = FfiOHOSFILEFsGetParent(getID())
}
if (ret.code != SUCCESS_CODE) {
FS_LOG.error(getErrorInfo(ret.code))
throw BusinessException(ret.code, getErrorInfo(ret.code))
}
let parent = ret.data.toString()
unsafe {
LibC.free(ret.data)
}
return parent
}
}
+176
View File
@@ -0,0 +1,176 @@
/*
* Copyright (c) 2025 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package ohos.file_fs
import ohos.ffi.*
import ohos.base.*
foreign {
// Copy
func FfiOHOSFileFsCreateCopyOptions(progressId: Int64, signalId: Int64): Int64
func FfiOHOSFileFsCreateTaskSignal(): Int64
func FfiOHOSFileFsTaskSignalCancel(id: Int64): Int64
// Stat
func FfiOHOSFileFsStatByID(file: Int32): RetDataI64
func FfiOHOSFileFsStatByString(file: CString): RetDataI64
func FfiOHOSStatGetIno(id: Int64): Int64
func FfiOHOSStatGetMode(id: Int64): Int64
func FfiOHOSStatGetUid(id: Int64): Int64
func FfiOHOSStatGetGid(id: Int64): Int64
func FfiOHOSStatGetSize(id: Int64): Int64
func FfiOHOSStatGetAtime(id: Int64): Int64
func FfiOHOSStatGetMtime(id: Int64): Int64
func FfiOHOSStatGetCtime(id: Int64): Int64
func FfiOHOSStatIsBlockDevice(id: Int64): Bool
func FfiOHOSStatIsCharacterDevice(id: Int64): Bool
func FfiOHOSStatIsDirectory(id: Int64): Bool
func FfiOHOSStatIsFIFO(id: Int64): Bool
func FfiOHOSStatIsFile(id: Int64): Bool
func FfiOHOSStatIsSocket(id: Int64): Bool
func FfiOHOSStatIsSymbolicLink(id: Int64): Bool
// Stream
func FfiOHOSFileFsCreateStream(path: CString, mode: CString): RetDataI64
func FfiOHOSFileFsFdopenStream(fd: Int32, mode: CString): RetDataI64
func FfiOHOSStreamClose(id: Int64): RetCode
func FfiOHOSStreamFlush(id: Int64): RetCode
func FfiOHOSStreamWriteCur(id: Int64, string: CPointer<UInt8>, length: Int64, encoding: CString): RetDataI64
func FfiOHOSStreamWrite(id: Int64, string: CPointer<UInt8>, length: Int64, offset: Int64, encoding: CString): RetDataI64
func FfiOHOSStreamReadCur(id: Int64, buffer: CPointer<UInt8>, size: Int64, length: Int64): RetDataI64
func FfiOHOSStreamRead(id: Int64, buffer: CPointer<UInt8>, size: Int64, length: Int64, offset: Int64): RetDataI64
// RandomAccessFile
func FfiOHOSFileFsCreateRandomAccessFileByString(file: CString, mode: Int64): RetDataI64
func FfiOHOSFileFsCreateRandomAccessFileByID(file: Int64, mode: Int64): RetDataI64
func FfiOHOSRandomAccessFileGetFd(id: Int64): Int32
func FfiOHOSRandomAccessFileGetFPointer(id: Int64): Int64
func FfiOHOSRandomAccessFileSetFilePointerSync(id: Int64, fd: Int64): Unit
func FfiOHOSRandomAccessFileClose(id: Int64): Unit
func FfiOHOSRandomAccessFileWrite(id: Int64, buffer: CPointer<Byte>, length: UIntNative, offset: Int64): RetDataI64
func FfiOHOSRandomAccessFileRead(id: Int64, buffer: CPointer<Byte>, length: UIntNative, offset: Int64): RetDataI64
// File
func FfiOHOSFileFsOpen(path: CString, openMode: Int64): RetDataI64
func FfiOHOSFileFsDup(fd: Int32): RetDataI64
func FfiOHOSFILEFsGetFD(id: Int64): Int32
func FfiOHOSFILEFsGetPath(id: Int64): CString
func FfiOHOSFILEFsGetName(id: Int64): CString
func FfiOHOSFILEFsTryLock(id: Int64, exclusive: Bool): RetCode
func FfiOHOSFILEFsUnLock(id: Int64): RetCode
func FfiOHOSFILEFsGetParent(id: Int64): RetDataCString
func FfiOHOSFileFsLstat(path: CString): RetDataI64
func FfiOHOSFileFsMkdir(path: CString, recursion: Bool, isTwoArgs: Bool): Int32
func FfiOHOSFileFsRmdir(path: CString): Int32
func FfiOHOSFileFsMoveDir(src: CString, dest: CString, mode: Int32): RetDataCArrConflictFiles
func FfiOHOSFileFsRename(oldFile: CString, newFile: CString): Int32
func FfiOHOSFileFsUnlink(path: CString): Int32
func FfiOHOSFileFsAccess(path: CString): RetDataBool
func FfiOHOSFileFsRead(fd: Int32, buffer: CPointer<Byte>, size: Int64, length: UIntNative, offset: Int64): RetDataI64
func FfiOHOSFileFsReadCur(fd: Int32, buffer: CPointer<Byte>, size: Int64, length: UIntNative): RetDataI64
func FfiOHOSFileFsWrite(fd: Int32, buffer: CPointer<UInt8>, length: UIntNative, offset: Int64, encoding: CString): RetDataI64
func FfiOHOSFileFsWriteCur(fd: Int32, buffer: CPointer<UInt8>, length: UIntNative, encoding: CString): RetDataI64
func FfiOHOSFileFsCopyDir(src: CString, dest: CString, mode: Int32): RetDataCArrConflictFiles
func FfiOHOSFileFsCopyFile(src: CString, dest: CString, mode: Int32): Int32
func FfiOHOSFileFsCopyFileSI(src: CString, dest: Int32, mode: Int32): Int32
func FfiOHOSFileFsCopyFileIS(src: Int32, dest: CString, mode: Int32): Int32
func FfiOHOSFileFsCopyFileII(src: Int32, dest: Int32, mode: Int32): Int32
func FfiOHOSFileFsMoveFile(src: CString, dest: CString, mode: Int32): Int32
func FfiOHOSFileFsMkdtemp(prefix: CString): RetDataCString
func FfiOHOSFileListFile(path: CString, options: CListFileOptions): RetDataCArrStringN
func FfiOHOSFileFsLseek(fd: Int32, offset: Int64, whence: Int32): RetDataI64
func FfiOHOSFileFsFdatasync(fd: Int32): Int32
func FfiOHOSFileFsFsync(fd: Int32): Int32
func FfiOHOSFileFsSymlink(target: CString, srcPath: CString): Int32
func FfiOHOSFileFsTruncateByString(file: CString, len: Int64): Int32
func FfiOHOSFileFsTruncateByFd(file: Int32, len: Int64): Int32
func FfiOHOSFileFsClose(file: Int64): Int32
func FfiOHOSFileFsCloseByFd(file: Int32): Int32
func FfiOHOSFileFsReadLines(path: CString, encoding: CString): RetDataI64
func FfiOHOSFileFsReaderIteratorNext(id: Int64): RetReaderIteratorResult
func FfiOHOSFileFsReadText(path: CString, offset: Int64, hasLen: Bool, len: Int64, encoding: CString): RetDataCString
func FfiOHOSFileFsUtimes(path: CString, mtime: Float64): Int32
}
+161
View File
@@ -0,0 +1,161 @@
/*
* Copyright (c) 2025 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package ohos.file_fs
import ohos.ffi.*
import ohos.base.*
import ohos.labels.*
import std.collection.*
@!APILevel[
21,
stagemodelonly: true,
syscap: "SystemCapability.FileManagement.File.FileIO"
]
public class RandomAccessFile <: RemoteDataLite {
init(instanceId: Int64) {
super(instanceId)
}
~init() {
releaseFFIData(myDataId)
}
@!APILevel[
21,
stagemodelonly: true,
syscap: "SystemCapability.FileManagement.File.FileIO"
]
public prop fd: Int32 {
get() {
unsafe { FfiOHOSRandomAccessFileGetFd(getID()) }
}
}
@!APILevel[
21,
stagemodelonly: true,
syscap: "SystemCapability.FileManagement.File.FileIO"
]
public prop filePointer: Int64 {
get() {
unsafe { FfiOHOSRandomAccessFileGetFPointer(getID()) }
}
}
/**
* @brief setFilePointer(): void
*/
@!APILevel[
21,
stagemodelonly: true,
syscap: "SystemCapability.FileManagement.File.FileIO"
]
public func setFilePointer(fp: Int64): Unit {
unsafe { FfiOHOSRandomAccessFileSetFilePointerSync(getID(), fp) }
return
}
/**
* @brief close(): void
*/
@!APILevel[
21,
stagemodelonly: true,
syscap: "SystemCapability.FileManagement.File.FileIO"
]
public func close(): Unit {
unsafe { FfiOHOSRandomAccessFileClose(getID()) }
return
}
/**
* @brief writeSync(buffer: ArrayBuffer | string,
* options?: { offset?: number; length?: number; encoding?: string; }): number
*/
@!APILevel[
21,
stagemodelonly: true,
syscap: "SystemCapability.FileManagement.File.FileIO"
]
public func write(buffer: String, writeOptions!: WriteOptions = WriteOptions()): Int64 {
let arr = unsafe { buffer.rawData() }
write(arr, writeOptions: writeOptions)
}
/**
* @brief writeSync(buffer: ArrayBuffer | string,
* options?: { offset?: number; length?: number; encoding?: string; }): number
*/
@!APILevel[
21,
stagemodelonly: true,
syscap: "SystemCapability.FileManagement.File.FileIO"
]
public func write(buffer: Array<Byte>, writeOptions!: WriteOptions = WriteOptions()): Int64 {
let writeLen: UIntNative = writeOptions.length ?? UIntNative(buffer.size)
if (writeLen > UIntNative(buffer.size)) {
throw BusinessException(INVALID_ARGS_CODE, "Invalid argument")
}
var writeBytes: Int64 = 0
unsafe {
let cBuffer = acquireArrayRawData(buffer)
let cValue = FfiOHOSRandomAccessFileWrite(getID(), cBuffer.pointer, writeLen, writeOptions.offset ?? 0)
releaseArrayRawData(cBuffer)
if (cValue.code != SUCCESS_CODE) {
FS_LOG.error(getErrorInfo(cValue.code))
throw BusinessException(cValue.code, getErrorInfo(cValue.code))
}
writeBytes = cValue.data
}
return writeBytes
}
/**
* @brief readSync(buffer: ArrayBuffer, options?: { offset?: number; length?: number; }): number
*/
@!APILevel[
21,
stagemodelonly: true,
syscap: "SystemCapability.FileManagement.File.FileIO"
]
public func read(buffer: Array<Byte>, readOptions!: ReadOptions = ReadOptions()): Int64 {
let cValue: RetDataI64
if (buffer.size == 0) {
return 0
}
let cBuffer = safeMalloc<Byte>(count: buffer.size)
if (readOptions.length.isNone()) {
cValue = unsafe {
FfiOHOSRandomAccessFileRead(getID(), cBuffer, UIntNative(buffer.size), readOptions.offset ?? 0)
}
} else {
cValue = unsafe {
FfiOHOSRandomAccessFileRead(getID(), cBuffer, readOptions.length.getOrThrow(), readOptions.offset ?? 0)
}
}
for (index in 0..cValue.data) {
let ch = unsafe { cBuffer.read(index) }
buffer[index] = ch
}
unsafe { LibC.free(cBuffer) }
if (cValue.code != SUCCESS_CODE) {
FS_LOG.error(getErrorInfo(cValue.code))
throw BusinessException(cValue.code, getErrorInfo(cValue.code))
}
return cValue.data
}
}
+230
View File
@@ -0,0 +1,230 @@
/*
* Copyright (c) 2025 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package ohos.file_fs
import ohos.ffi.*
import ohos.base.*
import ohos.labels.*
@!APILevel[
21,
atomicservice: true,
crossplatform: true,
stagemodelonly: true,
syscap: "SystemCapability.FileManagement.File.FileIO"
]
public class Stat <: RemoteDataLite {
init(instanceId: Int64) {
super(instanceId)
}
~init() {
releaseFFIData(myDataId)
}
@!APILevel[
21,
crossplatform: true,
stagemodelonly: true,
syscap: "SystemCapability.FileManagement.File.FileIO"
]
public prop ino: Int64 {
get() {
unsafe { Int64(FfiOHOSStatGetIno(getID())) }
}
}
@!APILevel[
21,
atomicservice: true,
crossplatform: true,
stagemodelonly: true,
syscap: "SystemCapability.FileManagement.File.FileIO"
]
public prop mode: Int64 {
get() {
unsafe { Int64(FfiOHOSStatGetMode(getID())) }
}
}
@!APILevel[
21,
crossplatform: true,
stagemodelonly: true,
syscap: "SystemCapability.FileManagement.File.FileIO"
]
public prop uid: Int64 {
get() {
unsafe { Int64(FfiOHOSStatGetUid(getID())) }
}
}
@!APILevel[
21,
crossplatform: true,
stagemodelonly: true,
syscap: "SystemCapability.FileManagement.File.FileIO"
]
public prop gid: Int64 {
get() {
unsafe { Int64(FfiOHOSStatGetGid(getID())) }
}
}
@!APILevel[
21,
atomicservice: true,
crossplatform: true,
stagemodelonly: true,
syscap: "SystemCapability.FileManagement.File.FileIO"
]
public prop size: Int64 {
get() {
unsafe { Int64(FfiOHOSStatGetSize(getID())) }
}
}
@!APILevel[
21,
atomicservice: true,
crossplatform: true,
stagemodelonly: true,
syscap: "SystemCapability.FileManagement.File.FileIO"
]
public prop atime: Int64 {
get() {
unsafe { Int64(FfiOHOSStatGetAtime(getID())) }
}
}
@!APILevel[
21,
atomicservice: true,
crossplatform: true,
stagemodelonly: true,
syscap: "SystemCapability.FileManagement.File.FileIO"
]
public prop mtime: Int64 {
get() {
unsafe { Int64(FfiOHOSStatGetMtime(getID())) }
}
}
@!APILevel[
21,
crossplatform: true,
stagemodelonly: true,
syscap: "SystemCapability.FileManagement.File.FileIO"
]
public prop ctime: Int64 {
get() {
unsafe { Int64(FfiOHOSStatGetCtime(getID())) }
}
}
/**
* @brief isBlockDevice(): boolean
*/
@!APILevel[
21,
crossplatform: true,
stagemodelonly: true,
syscap: "SystemCapability.FileManagement.File.FileIO"
]
public func isBlockDevice(): Bool {
unsafe { FfiOHOSStatIsBlockDevice(getID()) }
}
/**
* @brief isCharacterDevice(): boolean
*/
@!APILevel[
21,
crossplatform: true,
stagemodelonly: true,
syscap: "SystemCapability.FileManagement.File.FileIO"
]
public func isCharacterDevice(): Bool {
unsafe { FfiOHOSStatIsCharacterDevice(getID()) }
}
/**
* @brief isDirectory(): boolean
*/
@!APILevel[
21,
atomicservice: true,
crossplatform: true,
stagemodelonly: true,
syscap: "SystemCapability.FileManagement.File.FileIO"
]
public func isDirectory(): Bool {
unsafe { FfiOHOSStatIsDirectory(getID()) }
}
/**
* @brief isFIFO(): boolean
*/
@!APILevel[
21,
crossplatform: true,
stagemodelonly: true,
syscap: "SystemCapability.FileManagement.File.FileIO"
]
public func isFIFO(): Bool {
unsafe { FfiOHOSStatIsFIFO(getID()) }
}
/**
* @brief isFile(): boolean
*/
@!APILevel[
21,
atomicservice: true,
crossplatform: true,
stagemodelonly: true,
syscap: "SystemCapability.FileManagement.File.FileIO"
]
public func isFile(): Bool {
unsafe { FfiOHOSStatIsFile(getID()) }
}
/**
* @brief isSocket(): boolean
*/
@!APILevel[
21,
crossplatform: true,
stagemodelonly: true,
syscap: "SystemCapability.FileManagement.File.FileIO"
]
public func isSocket(): Bool {
unsafe { FfiOHOSStatIsSocket(getID()) }
}
/**
* @brief isSymbolicLink(): boolean
*/
@!APILevel[
21,
crossplatform: true,
stagemodelonly: true,
syscap: "SystemCapability.FileManagement.File.FileIO"
]
public func isSymbolicLink(): Bool {
unsafe { FfiOHOSStatIsSymbolicLink(getID()) }
}
}
+176
View File
@@ -0,0 +1,176 @@
/*
* Copyright (c) 2025 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package ohos.file_fs
import ohos.hilog.*
import ohos.ffi.*
import ohos.base.*
import ohos.labels.*
import std.collection.*
@!APILevel[
21,
stagemodelonly: true,
syscap: "SystemCapability.FileManagement.File.FileIO"
]
public class Stream <: RemoteDataLite {
init(instanceId: Int64) {
super(instanceId)
}
~init() {
releaseFFIData(myDataId)
}
/**
* @brief closeSync(file: number | File): void
*/
@!APILevel[
21,
stagemodelonly: true,
syscap: "SystemCapability.FileManagement.File.FileIO"
]
public func close(): Unit {
let code = unsafe { FfiOHOSStreamClose(getID()) }
match {
case code != SUCCESS_CODE =>
FS_LOG.error(getErrorInfo(code))
throw BusinessException(code, getErrorInfo(code))
case _ => return
}
}
/**
* @brief flushSync(): void
*/
@!APILevel[
21,
stagemodelonly: true,
syscap: "SystemCapability.FileManagement.File.FileIO"
]
public func flush(): Unit {
let code = unsafe { FfiOHOSStreamFlush(getID()) }
if (code != SUCCESS_CODE) {
FS_LOG.error(getErrorInfo(code))
throw BusinessException(code, getErrorInfo(code))
}
return
}
/**
* @brief writeSync(fd: number, buffer: ArrayBuffer | string,
* options?: { offset?: number; length?: number; encoding?: string; }): number
*/
@!APILevel[
21,
atomicservice: true,
crossplatform: true,
stagemodelonly: true,
syscap: "SystemCapability.FileManagement.File.FileIO"
]
public func write(buffer: String, options!: WriteOptions = WriteOptions()): Int64 {
let length = options.length ?? UIntNative(buffer.size)
if (length > UIntNative(buffer.size)) {
throw BusinessException(INVALID_ARGS_CODE, "Invalid argument")
}
let encoding = options.encoding
var writeBytes = 0
unsafe {
try (
cString = LibC.mallocCString(buffer).asResource(),
encodingCString = LibC.mallocCString(encoding).asResource()
) {
let cValue = if (options.offset.isNone()) {
FfiOHOSStreamWriteCur(getID(), cString.value.getChars(), Int64(length), encodingCString.value)
} else {
FfiOHOSStreamWrite(getID(), cString.value.getChars(), Int64(length), options.offset.getOrThrow(),
encodingCString.value)
}
if (cValue.code != SUCCESS_CODE) {
Hilog.error(0xD003900, "File-Fs", getErrorInfo(cValue.code))
throw BusinessException(cValue.code, getErrorInfo(cValue.code))
}
writeBytes = cValue.data
}
}
return writeBytes
}
@!APILevel[
21,
stagemodelonly: true,
syscap: "SystemCapability.FileManagement.File.FileIO"
]
public func write(buffer: Array<Byte>, options!: WriteOptions = WriteOptions()): Int64 {
let length = options.length ?? UIntNative(buffer.size)
if (length > UIntNative(buffer.size)) {
throw BusinessException(INVALID_ARGS_CODE, "Invalid argument")
}
let encoding = options.encoding
var writeBytes = 0
unsafe {
try (encodingCString = LibC.mallocCString(encoding).asResource()) {
let data = acquireArrayRawData(buffer)
let cValue = if (options.offset.isNone()) {
FfiOHOSStreamWriteCur(getID(), data.pointer, Int64(length), encodingCString.value)
} else {
FfiOHOSStreamWrite(getID(), data.pointer, Int64(length), options.offset.getOrThrow(),
encodingCString.value)
}
releaseArrayRawData(data)
if (cValue.code != SUCCESS_CODE) {
Hilog.error(0xD003900, "File-Fs", getErrorInfo(cValue.code))
throw BusinessException(cValue.code, getErrorInfo(cValue.code))
}
writeBytes = cValue.data
}
}
return writeBytes
}
/**
* @brief readSync(buffer: ArrayBuffer, options?: { offset?: number; length?: number; }): number
*/
@!APILevel[
21,
stagemodelonly: true,
syscap: "SystemCapability.FileManagement.File.FileIO"
]
public func read(arrayBuffer: Array<Byte>, options!: ReadOptions = ReadOptions()): Int64 {
if (arrayBuffer.size == 0) {
return 0
}
unsafe {
let buffer = safeMalloc<UInt8>(count: arrayBuffer.size)
let length = options.length ?? UIntNative(arrayBuffer.size)
let cValue = if (options.offset.isNone()) {
FfiOHOSStreamReadCur(getID(), buffer, arrayBuffer.size, Int64(length))
} else {
FfiOHOSStreamRead(getID(), buffer, arrayBuffer.size, Int64(length), options.offset.getOrThrow())
}
for (i in 0..cValue.data) {
arrayBuffer[i] = buffer.read(i)
}
LibC.free(buffer)
if (cValue.code != SUCCESS_CODE) {
Hilog.error(0xD003900, "File-Fs", getErrorInfo(cValue.code))
throw BusinessException(cValue.code, getErrorInfo(cValue.code))
}
return cValue.data
}
}
}