From 2dc32bb9344964baa0316a2d19ba084d11e10bc7 Mon Sep 17 00:00:00 2001 From: futurezhou Date: Fri, 21 Apr 2023 17:17:21 +0800 Subject: [PATCH] fileio Interface switching Signed-off-by: futurezhou --- .../FileExtensionAbility.ts | 1070 +++++++++-------- 1 file changed, 539 insertions(+), 531 deletions(-) diff --git a/services/file_extension_hap/entry/src/main/ets/FileExtensionAbility/FileExtensionAbility.ts b/services/file_extension_hap/entry/src/main/ets/FileExtensionAbility/FileExtensionAbility.ts index ecfe2109..b6516f60 100644 --- a/services/file_extension_hap/entry/src/main/ets/FileExtensionAbility/FileExtensionAbility.ts +++ b/services/file_extension_hap/entry/src/main/ets/FileExtensionAbility/FileExtensionAbility.ts @@ -13,21 +13,20 @@ * limitations under the License. */ // @ts-nocheck -import Extension from '@ohos.application.FileAccessExtensionAbility' -import fileio from '@ohos.fileio' -import { init, findVolumeInfo, delVolumeInfo, getVolumeInfoList, path2uri } from './VolumeManager' -import { onReceiveEvent } from './Subcriber' -import fileExtensionInfo from "@ohos.file.fileExtensionInfo" -import hilog from '@ohos.hilog' +import Extension from '@ohos.application.FileAccessExtensionAbility'; +import fileio from '@ohos.fileio'; +import fileIO from '@ohos.file.fs'; +import { init, findVolumeInfo, delVolumeInfo, getVolumeInfoList, path2uri } from './VolumeManager'; +import { onReceiveEvent } from './Subcriber'; +import fileExtensionInfo from '@ohos.file.fileExtensionInfo'; +import hilog from '@ohos.hilog'; import process from '@ohos.process'; -const DeviceFlag = fileExtensionInfo.DeviceFlag; -const DocumentFlag = fileExtensionInfo.DocumentFlag; -const NotifyType = fileExtensionInfo.NotifyType; -const DeviceType = fileExtensionInfo.DeviceType; +const deviceFlag = fileExtensionInfo.DeviceFlag; +const documentFlag = fileExtensionInfo.DocumentFlag; +const notifyType = fileExtensionInfo.NotifyType; +const deviceType = fileExtensionInfo.DeviceType; const BUNDLE_NAME = 'com.ohos.UserFile.ExternalFileManager'; -const DEFAULT_MODE = 0o666; -const CREATE_FILE_FLAGS = 0o100; const URI_SCHEME = 'datashare://'; const DOMAIN_CODE = 0x0001; const TAG = 'ExternalFileManager'; @@ -40,576 +39,585 @@ const E_GETRESULT = 14300004; let callbackFun = null; export default class FileExtAbility extends Extension { - onCreate(want) { - init(); - onReceiveEvent(function (data) { - if (data.event == 'usual.event.data.VOLUME_MOUNTED') { - if (callbackFun != null) { - let uri = path2uri('', data.parameters.path); - callbackFun(data.parameters.type, NotifyType.DEVICE_ONLINE, uri); - } - process.exit(0); - } else { - if (callbackFun != null) { - let uri = ''; - let deviceType = 0; - let volumeInfo = findVolumeInfo(data.parameters.id); - if (volumeInfo) { - uri = volumeInfo.uri; - deviceType = volumeInfo.type; - } - callbackFun(deviceType, NotifyType.DEVICE_OFFLINE, uri); - } - delVolumeInfo(data.parameters.id); - } - }); - } + onCreate(want): void { + init(); + onReceiveEvent(function (data) { + if (data.event === 'usual.event.data.VOLUME_MOUNTED') { + if (callbackFun !== null) { + let uri = path2uri('', data.parameters.path); + callbackFun(data.parameters.type, notifyType.DEVICE_ONLINE, uri); + } + process.exit(0); + } else { + if (callbackFun !== null) { + let uri = ''; + let deviceType = 0; + let volumeInfo = findVolumeInfo(data.parameters.id); + if (volumeInfo) { + uri = volumeInfo.uri; + deviceType = volumeInfo.type; + } + callbackFun(deviceType, notifyType.DEVICE_OFFLINE, uri); + } + delVolumeInfo(data.parameters.id); + } + }); + } - registerCallback(callback) { - callbackFun = callback; - } + registerCallback(callback): void { + callbackFun = callback; + } - checkUri(uri) { - if (uri.indexOf(URI_SCHEME) == 0) { - uri = uri.replace(URI_SCHEME, ''); - return /^\/([^\/]+\/?)+$/.test(uri); - } else { - hilog.error(DOMAIN_CODE, TAG, 'checkUri error, uri is ' + uri); - return false; - } + checkUri(uri): boolean { + if (uri.indexOf(URI_SCHEME) === 0) { + uri = uri.replace(URI_SCHEME, ''); + return /^\/([^\/]+\/?)+$/.test(uri); + } else { + hilog.error(DOMAIN_CODE, TAG, 'checkUri error, uri is ' + uri); + return false; } + } - getPath(uri) { - let sep = '://'; - let arr = uri.split(sep); - if (arr.length < 2) { - return uri; - } - let path = uri.replace(arr[0] + sep, ''); - if (arr[1].indexOf('/') > 0) { - path = path.replace(arr[1].split('/')[0], ''); - } - path = path.replace('/' + BUNDLE_NAME, ''); - if (path.charAt(path.length - 1) == '/') { - path = path.substr(0, path.length - 1); - } - return path; + getPath(uri): string { + let sep = '://'; + let arr = uri.split(sep); + let minLength = 2; + if (arr.length < minLength) { + return uri; + } + let path = uri.replace(arr[0] + sep, ''); + if (arr[1].indexOf('/') > 0) { + path = path.replace(arr[1].split('/')[0], ''); } + path = path.replace('/' + BUNDLE_NAME, ''); + if (path.charAt(path.length - 1) === '/') { + path = path.substr(0, path.length - 1); + } + return path; + } - genNewFileUri(uri, displayName) { - let newFileUri = uri; - if (uri.charAt(uri.length - 1) == '/') { - newFileUri += displayName; - } else { - newFileUri += '/' + displayName; - } - return newFileUri; + genNewFileUri(uri, displayName) { + let newFileUri = uri; + if (uri.charAt(uri.length - 1) === '/') { + newFileUri += displayName; + } else { + newFileUri += '/' + displayName; } + return newFileUri; + } - getFileName(uri) { - let arr = uri.split('/'); - let name = arr.pop(); - if (name == '') { - name = arr.pop(); - } - return name; + getFileName(uri): string { + let arr = uri.split('/'); + let name = arr.pop(); + if (name === '') { + name = arr.pop(); } + return name; + } - renameUri(uri, displayName) { - let arr = uri.split('/'); - let newFileUri = ''; - if (arr.pop() == '') { - arr.pop(); - arr.push(displayName) - arr.push(''); - } else { - arr.push(displayName) - } - for (var index = 0; index < arr.length; index++) { - if (arr[index] == '') { - newFileUri += '/'; - } else if (index == arr.length - 1) { - newFileUri += arr[index]; - } else { - newFileUri += arr[index] + '/'; - } - } - return newFileUri; + renameUri(uri, displayName): string { + let arr = uri.split('/'); + let newFileUri = ''; + if (arr.pop() === '') { + arr.pop(); + arr.push(displayName); + arr.push(''); + } else { + arr.push(displayName); + } + for (let index = 0; index < arr.length; index++) { + if (arr[index] === '') { + newFileUri += '/'; + } else if (index === arr.length - 1) { + newFileUri += arr[index]; + } else { + newFileUri += arr[index] + '/'; + } } + return newFileUri; + } - recurseDir(path, cb) { - try { - let stat = fileio.statSync(path); - if (stat.isDirectory()) { - let dir = fileio.opendirSync(path); - let hasNextFile = true; - cb(path, true, hasNextFile); - while (hasNextFile) { - try { - let dirent = dir.readSync(); - this.recurseDir(path + '/' + dirent.name, cb); - } catch (e) { - hasNextFile = false; - cb(path, true, hasNextFile); - } - } - dir.closeSync(); - } else { - cb(path, false); - } - } catch (e) { - hilog.error(DOMAIN_CODE, TAG, 'recurseDir error ' + e.message); - cb(path, true); - } + recurseDir(path, cb): void { + try { + let stat = fileIO.statSync(path); + if (stat.isDirectory()) { + let dir = fileio.opendirSync(path); + let hasNextFile = true; + cb(path, true, hasNextFile); + while (hasNextFile) { + try { + let dirent = dir.readSync(); + this.recurseDir(path + '/' + dirent.name, cb); + } catch (e) { + hasNextFile = false; + cb(path, true, hasNextFile); + } + } + dir.closeSync(); + } else { + cb(path, false); + } + } catch (e) { + hilog.error(DOMAIN_CODE, TAG, 'recurseDir error ' + e.message); + cb(path, true); } + } - isCrossDeviceLink(sourceFileUri, targetParentUri) { - let roots = this.getRoots().roots; - for (let index = 0; index < roots.length; index++) { - let uri = roots[index].uri; - if (sourceFileUri.indexOf(uri) == 0 && targetParentUri.indexOf(uri) == 0) { - return false; - } - } - return true; + isCrossDeviceLink(sourceFileUri, targetParentUri): boolean { + let roots = this.getRoots().roots; + for (let index = 0; index < roots.length; index++) { + let uri = roots[index].uri; + if (sourceFileUri.indexOf(uri) === 0 && targetParentUri.indexOf(uri) === 0) { + return false; + } } + return true; + } - openFile(sourceFileUri, flags) { - if (!this.checkUri(sourceFileUri)) { - return { - fd: ERR_ERROR, - code: E_URIS, - }; - } - let fd = 0; - try { - let path = this.getPath(sourceFileUri); - fd = fileio.openSync(path, flags, DEFAULT_MODE); - return { - fd: fd, - code: ERR_OK, - }; - } catch (e) { - hilog.error(DOMAIN_CODE, TAG, 'openFile error ' + e.message); - return { - fd: ERR_ERROR, - code: e.code, - }; - } + openFile(sourceFileUri, flags): {number, number} { + if (!this.checkUri(sourceFileUri)) { + return { + fd: ERR_ERROR, + code: E_URIS, + }; } + try { + let path = this.getPath(sourceFileUri); + let file = fileIO.openSync(path, flags); + return { + fd: file.fd, + code: ERR_OK, + }; + } catch (e) { + hilog.error(DOMAIN_CODE, TAG, 'openFile error ' + e.message); + return { + fd: ERR_ERROR, + code: e.code, + }; + } + } - createFile(parentUri, displayName) { - if (!this.checkUri(parentUri)) { - return { - uri: '', - code: E_URIS, - }; - } - try { - let newFileUri = this.genNewFileUri(parentUri, displayName); - if (this.access(newFileUri).isExist) { - return { - uri: '', - code: E_EXIST, - }; - } - let path = this.getPath(newFileUri); - let fd = fileio.openSync(path, CREATE_FILE_FLAGS, DEFAULT_MODE); - fileio.closeSync(fd); - return { - uri: newFileUri, - code: ERR_OK, - }; - } catch (e) { - hilog.error(DOMAIN_CODE, TAG, 'createFile error ' + e.message); - return { - uri: '', - code: e.code, - }; - } + createFile(parentUri, displayName): {string, number} { + if (!this.checkUri(parentUri)) { + return { + uri: '', + code: E_URIS, + }; + } + try { + let newFileUri = this.genNewFileUri(parentUri, displayName); + if (this.access(newFileUri).isExist) { + return { + uri: '', + code: E_EXIST, + }; + } + let path = this.getPath(newFileUri); + let file = fileIO.openSync(path, fileIO.OpenMode.CREATE); + fileIO.closeSync(file); + return { + uri: newFileUri, + code: ERR_OK, + }; + } catch (e) { + hilog.error(DOMAIN_CODE, TAG, 'createFile error ' + e.message); + return { + uri: '', + code: e.code, + }; } + } - mkdir(parentUri, displayName) { - if (!this.checkUri(parentUri)) { - return { - uri: '', - code: E_URIS, - }; - } - try { - let newFileUri = this.genNewFileUri(parentUri, displayName); - let path = this.getPath(newFileUri); - fileio.mkdirSync(path); - return { - uri: newFileUri, - code: ERR_OK, - }; - } catch (e) { - hilog.error(DOMAIN_CODE, TAG, 'mkdir error ' + e.message); - return { - uri: '', - code: e.code, - }; - } + mkdir(parentUri, displayName): {string, number} { + if (!this.checkUri(parentUri)) { + return { + uri: '', + code: E_URIS, + }; + } + try { + let newFileUri = this.genNewFileUri(parentUri, displayName); + let path = this.getPath(newFileUri); + fileIO.mkdirSync(path); + return { + uri: newFileUri, + code: ERR_OK, + }; + } catch (e) { + hilog.error(DOMAIN_CODE, TAG, 'mkdir error ' + e.message); + return { + uri: '', + code: e.code, + }; } + } - delete(selectFileUri) { - if (!this.checkUri(selectFileUri)) { - return E_URIS; - } - let path = this.getPath(selectFileUri); - let code = ERR_OK; - this.recurseDir(path, function (filePath, isDirectory, hasNextFile) { - try { - if (isDirectory) { - if (!hasNextFile) { - fileio.rmdirSync(filePath); - } - } else { - fileio.unlinkSync(filePath); - } - } catch (e) { - hilog.error(DOMAIN_CODE, TAG, 'delete error ' + e.message); - // At present, the master libn has modified the interface exception throwing mechanism - // and the exception has no code attribute, which will lead to the failure of some faf use cases. - // In the later stage, the new file_api interfaces will be merged and modified in a unified way. - code = E_GETRESULT; - } - }); - return code; + delete(selectFileUri): number { + if (!this.checkUri(selectFileUri)) { + return E_URIS; } + let path = this.getPath(selectFileUri); + let code = ERR_OK; + this.recurseDir(path, function (filePath, isDirectory, hasNextFile) { + try { + if (isDirectory) { + if (!hasNextFile) { + fileIO.rmdirSync(filePath); + } + } else { + fileIO.unlinkSync(filePath); + } + } catch (e) { + hilog.error(DOMAIN_CODE, TAG, 'delete error ' + e.message); + code = e.code;; + } + }); + return code; + } - move(sourceFileUri, targetParentUri) { - if (!this.checkUri(sourceFileUri) || !this.checkUri(targetParentUri)) { - return { - uri: '', - code: E_URIS, - }; - } - let displayName = this.getFileName(sourceFileUri); - let newFileUri = this.genNewFileUri(targetParentUri, displayName); - let oldPath = this.getPath(sourceFileUri); - let newPath = this.getPath(newFileUri); - if (oldPath == newPath) { - // move to the same directory - return { - uri: newFileUri, - code: ERR_OK, - }; - } else if (newPath.indexOf(oldPath) == 0) { - // move to a subdirectory of the source directory - return { - uri: '', - code: E_GETRESULT, - }; - } - try { - // The source file does not exist or the destination is not a directory - fileio.accessSync(oldPath); - let stat = fileio.statSync(this.getPath(targetParentUri)); - if (!stat || !stat.isDirectory()) { - return { - uri: '', - code: E_GETRESULT, - }; - } - // If not across devices, use fileio.renameSync to move - if (!this.isCrossDeviceLink(sourceFileUri, targetParentUri)) { - fileio.renameSync(oldPath, newPath); - return { - uri: newFileUri, - code: ERR_OK, - }; - } - } catch (e) { - hilog.error(DOMAIN_CODE, TAG, 'move error ' + e.message); - return { - uri: '', - code: e.code, - }; - } - /** - * Recursive source directory - * If it is a directory, create a new directory first and then delete the source directory. - * If it is a file, copy the file first and then delete the source file. - * The source directory will be deleted after the sub files are deleted - */ - this.recurseDir(oldPath, function (filePath, isDirectory, hasNextFile) { + move(sourceFileUri, targetParentUri): {string, number} { + if (!this.checkUri(sourceFileUri) || !this.checkUri(targetParentUri)) { + return { + uri: '', + code: E_URIS, + }; + } + let displayName = this.getFileName(sourceFileUri); + let newFileUri = this.genNewFileUri(targetParentUri, displayName); + let oldPath = this.getPath(sourceFileUri); + let newPath = this.getPath(newFileUri); + if (oldPath === newPath) { + // move to the same directory + return { + uri: newFileUri, + code: ERR_OK, + }; + } else if (newPath.indexOf(oldPath) === 0) { + // move to a subdirectory of the source directory + return { + uri: '', + code: E_GETRESULT, + }; + } + try { + // The source file does not exist or the destination is not a directory + let isAccess = fileIO.accessSync(oldPath); + if(!isAccess) + { + return { + uri: '', + code: E_GETRESULT, + }; + } + let stat = fileIO.statSync(this.getPath(targetParentUri)); + if (!stat || !stat.isDirectory()) { + return { + uri: '', + code: E_GETRESULT, + }; + } + // If not across devices, use fileio.renameSync to move + if (!this.isCrossDeviceLink(sourceFileUri, targetParentUri)) { + fileIO.renameSync(oldPath, newPath); + return { + uri: newFileUri, + code: ERR_OK, + }; + } + } catch (e) { + hilog.error(DOMAIN_CODE, TAG, 'move error ' + e.message); + return { + uri: '', + code: e.code, + }; + } + /** + * Recursive source directory + * If it is a directory, create a new directory first and then delete the source directory. + * If it is a file, copy the file first and then delete the source file. + * The source directory will be deleted after the sub files are deleted + */ + this.recurseDir(oldPath, function (filePath, isDirectory, hasNextFile) { + try { + let newFilePath = filePath.replace(oldPath, newPath); + if (isDirectory) { + if (hasNextFile) { try { - let newFilePath = filePath.replace(oldPath, newPath); - if (isDirectory) { - if (hasNextFile) { - try { - // If the target directory already has a directory with the same name, it will not be created - fileio.accessSync(newFilePath); - } catch (e) { - fileio.mkdirSync(newFilePath); - } - } else { - fileio.rmdirSync(filePath); - } - } else { - fileio.copyFileSync(filePath, newFilePath); - fileio.unlinkSync(filePath); - } + // If the target directory already has a directory with the same name, it will not be created + let isAccess = fileIO.accessSync(newFilePath); + if(!isAccess) + { + fileIO.mkdirSync(newFilePath); + } } catch (e) { - hilog.error(DOMAIN_CODE, TAG, 'move error ' + e.message); - return { - uri: '', - code: e.code, - }; + hilog.error(DOMAIN_CODE, TAG, 'mkdirSync error ' + e.message); } - }); + } else { + fileIO.rmdirSync(filePath); + } + } else { + fileIO.copyFileSync(filePath, newFilePath); + fileIO.unlinkSync(filePath); + } + } catch (e) { + hilog.error(DOMAIN_CODE, TAG, 'move error ' + e.message); return { - uri: newFileUri, - code: ERR_OK, + uri: '', + code: e.code, }; - } + } + }); + return { + uri: newFileUri, + code: ERR_OK, + }; + } - rename(sourceFileUri, displayName) { - if (!this.checkUri(sourceFileUri)) { - return { - uri: '', - code: E_URIS, - }; - } - try { - let newFileUri = this.renameUri(sourceFileUri, displayName); - let oldPath = this.getPath(sourceFileUri); - let newPath = this.getPath(newFileUri); - fileio.renameSync(oldPath, newPath); - return { - uri: newFileUri, - code: ERR_OK, - }; - } catch (e) { - hilog.error(DOMAIN_CODE, TAG, 'rename error ' + e.message); - return { - uri: '', - code: e.code, - }; - } + rename(sourceFileUri, displayName): {string, number} { + if (!this.checkUri(sourceFileUri)) { + return { + uri: '', + code: E_URIS, + }; } + try { + let newFileUri = this.renameUri(sourceFileUri, displayName); + let oldPath = this.getPath(sourceFileUri); + let newPath = this.getPath(newFileUri); + fileIO.renameSync(oldPath, newPath); + return { + uri: newFileUri, + code: ERR_OK, + }; + } catch (e) { + hilog.error(DOMAIN_CODE, TAG, 'rename error ' + e.message); + return { + uri: '', + code: e.code, + }; + } + } - access(sourceFileUri) { - if (!this.checkUri(sourceFileUri)) { - hilog.error(DOMAIN_CODE, TAG, 'access checkUri fail'); - return { - isExist: false, - code: E_URIS, - }; - } - try { - let path = this.getPath(sourceFileUri); - fileio.accessSync(path); - } catch (e) { - hilog.error(DOMAIN_CODE, TAG, 'access error ' + e.message); - if (e.message == 'No such file or directory') { - return { - isExist: false, - code: ERR_OK, - }; - } - return { - isExist: false, - code: e.code, - } - } + access(sourceFileUri): {boolean, number} { + if (!this.checkUri(sourceFileUri)) { + hilog.error(DOMAIN_CODE, TAG, 'access checkUri fail'); + return { + isExist: false, + code: E_URIS, + }; + } + let isAccess = false; + try { + let path = this.getPath(sourceFileUri); + isAccess = fileIO.accessSync(path); + if(!isAccess) + { return { - isExist: true, - code: ERR_OK, + isExist: isAccess, + code: ERR_OK, }; + } + } catch (e) { + hilog.error(DOMAIN_CODE, TAG, 'access error ' + e.message); + return { + isExist: isAccess, + code: e.code, + }; } + return { + isExist: isAccess, + code: ERR_OK, + }; + } - listFile(sourceFileUri, offset, count, filter) { - if (!this.checkUri(sourceFileUri)) { - return { - infos: [], - code: E_URIS, - }; - } - let infos = []; + listFile(sourceFileUri, offset, count, filter) { + if (!this.checkUri(sourceFileUri)) { + return { + infos: [], + code: E_URIS, + }; + } + let infos = []; + try { + let path = this.getPath(sourceFileUri); + let dir = fileio.opendirSync(path); + let hasNextFile = true; + let i = 0; + while (hasNextFile) { try { - let path = this.getPath(sourceFileUri); - let dir = fileio.opendirSync(path); - let hasNextFile = true; - let i = 0; - while (hasNextFile) { - try { - let dirent = dir.readSync(); - let stat = fileio.statSync(path + '/' + dirent.name); - let mode = DocumentFlag.SUPPORTS_READ | DocumentFlag.SUPPORTS_WRITE; - if (stat.isDirectory()) { - mode |= DocumentFlag.REPRESENTS_DIR; - } else { - mode |= DocumentFlag.REPRESENTS_FILE; - } - - if (offset > i) { - i++; - continue; - } + let dirent = dir.readSync(); + let stat = fileIO.statSync(path + '/' + dirent.name); + let mode = documentFlag.SUPPORTS_READ | documentFlag.SUPPORTS_WRITE; + if (stat.isDirectory()) { + mode |= documentFlag.REPRESENTS_DIR; + } else { + mode |= documentFlag.REPRESENTS_FILE; + } - infos.push({ - uri: this.genNewFileUri(sourceFileUri, dirent.name), - fileName: dirent.name, - mode: mode, - size: stat.size, - mtime: stat.mtime, - mimeType: '', - }); + if (offset > i) { + i++; + continue; + } - i++; - if (i == (offset + count)) { - hasNextFile = false; - break; - } - } catch (e) { - hasNextFile = false; - } - } - dir.closeSync(); - } catch (e) { - hilog.error(DOMAIN_CODE, TAG, 'listFile error ' + e.message); - return { - infos: [], - // At present, the master libn has modified the interface exception throwing mechanism - // and the exception has no code attribute, which will lead to the failure of some faf use cases. - // In the later stage, the new file_api interfaces will be merged and modified in a unified way. - code: E_GETRESULT, - }; - } - return { - infos: infos, - code: ERR_OK, - }; - } + infos.push({ + uri: this.genNewFileUri(sourceFileUri, dirent.name), + fileName: dirent.name, + mode: mode, + size: stat.size, + mtime: stat.mtime, + mimeType: '', + }); - getFileInfoFromUri(selectFileUri) { - if (!this.checkUri(selectFileUri)) { - return { - fileInfo: {}, - code: E_URIS, - }; - } - let fileInfo = {}; - try { - let path = this.getPath(selectFileUri); - let fileName = this.getFileName(path); - let stat = fileio.statSync(path) - let mode = DocumentFlag.SUPPORTS_READ | DocumentFlag.SUPPORTS_WRITE; - if (stat.isDirectory()) { - mode |= DocumentFlag.REPRESENTS_DIR; - } else { - mode |= DocumentFlag.REPRESENTS_FILE; - } - fileInfo = { - uri: selectFileUri, - fileName: fileName, - mode: mode, - size: stat.size, - mtime: stat.mtime, - mimeType: '', - }; + i++; + if (i === (offset + count)) { + hasNextFile = false; + break; + } } catch (e) { - hilog.error(DOMAIN_CODE, TAG, 'getFileInfoFromUri error ' + e.message); - return { - fileInfo: {}, - code: e.code, - }; - } - return { - fileInfo: fileInfo, - code: ERR_OK, - }; + hasNextFile = false; + } + } + dir.closeSync(); + } catch (e) { + hilog.error(DOMAIN_CODE, TAG, 'listFile error ' + e.message); + return { + infos: [], + // At present, the master libn has modified the interface exception throwing mechanism + // and the exception has no code attribute, which will lead to the failure of some faf use cases. + // In the later stage, the new file_api interfaces will be merged and modified in a unified way. + code: E_GETRESULT, + }; } + return { + infos: infos, + code: ERR_OK, + }; + } - getRoots() { - let roots = [ - { - uri: 'datashare:///com.ohos.UserFile.ExternalFileManager/data/storage/el1/bundle/storage_daemon', - displayName: 'shared_disk', - deviceType: DeviceType.DEVICE_SHARED_DISK, - deviceFlags: DeviceFlag.SUPPORTS_READ | DeviceFlag.SUPPORTS_WRITE, - }, - ]; - roots = roots.concat(getVolumeInfoList()); - return { - roots: roots, - code: ERR_OK, - }; + getFileInfoFromUri(selectFileUri) { + if (!this.checkUri(selectFileUri)) { + return { + fileInfo: {}, + code: E_URIS, + }; } - - getNormalResult(dirPath, column, queryResults) { - if (column === 'display_name') { - let index = dirPath.lastIndexOf('/'); - let target = dirPath.substring(index + 1, ); - queryResults.push(String(target)); - } else if (column === 'relative_path') { - let index = dirPath.lastIndexOf('/'); - let target = dirPath.substring(0, index + 1); - queryResults.push(target); - } else { - queryResults.push(''); - } + let fileInfo = {}; + try { + let path = this.getPath(selectFileUri); + let fileName = this.getFileName(path); + let stat = fileIO.statSync(path); + let mode = documentFlag.SUPPORTS_READ | documentFlag.SUPPORTS_WRITE; + if (stat.isDirectory()) { + mode |= documentFlag.REPRESENTS_DIR; + } else { + mode |= documentFlag.REPRESENTS_FILE; + } + fileInfo = { + uri: selectFileUri, + fileName: fileName, + mode: mode, + size: stat.size, + mtime: stat.mtime, + mimeType: '', + }; + } catch (e) { + hilog.error(DOMAIN_CODE, TAG, 'getFileInfoFromUri error ' + e.message); + return { + fileInfo: {}, + code: e.code, + }; } + return { + fileInfo: fileInfo, + code: ERR_OK, + }; + } - getResultFromStat(dirPath, column, stat, queryResults) { - if (column === 'size' && stat.isDirectory()) { - let size = 0; - this.recurseDir(dirPath, function (filePath, isDirectory, hasNextFile) { - if (isDirectory && !hasNextFile) { - return; - } + getRoots() { + let roots = [ + { + uri: 'datashare:///com.ohos.UserFile.ExternalFileManager/data/storage/el1/bundle/storage_daemon', + displayName: 'shared_disk', + deviceType: deviceType.DEVICE_SHARED_DISK, + deviceFlags: deviceFlag.SUPPORTS_READ | deviceFlag.SUPPORTS_WRITE, + }, + ]; + roots = roots.concat(getVolumeInfoList()); + return { + roots: roots, + code: ERR_OK, + }; + } - if (!isDirectory) { - let fileStat = fileio.statSync(filePath); - size += fileStat.size; - } - }); - queryResults.push(String(size)); - } else { - queryResults.push(String(stat[column])); - } + getNormalResult(dirPath, column, queryResults): void { + if (column === 'display_name') { + let index = dirPath.lastIndexOf('/'); + let target = dirPath.substring(index + 1, ); + queryResults.push(String(target)); + } else if (column === 'relative_path') { + let index = dirPath.lastIndexOf('/'); + let target = dirPath.substring(0, index + 1); + queryResults.push(target); + } else { + queryResults.push(''); } + } - query(uri, columns) { - if (!this.checkUri(uri)) { - return { - results: [], - code: E_URIS, - }; + getResultFromStat(dirPath, column, stat, queryResults): void { + if (column === 'size' && stat.isDirectory()) { + let size = 0; + this.recurseDir(dirPath, function (filePath, isDirectory, hasNextFile) { + if (isDirectory && !hasNextFile) { + return; } - if (!this.access(uri).isExist) { - return { - results: [], - code: E_NOEXIST, - }; - } + if (!isDirectory) { + let fileStat = fileIO.statSync(filePath); + size += fileStat.size; + } + }); + queryResults.push(String(size)); + } else { + queryResults.push(String(stat[column])); + } + } - let queryResults = []; - try { - let dirPath = this.getPath(uri); - let stat = fileio.statSync(dirPath); - for (let index in columns) { - let column = columns[index]; - if (column in stat) { - this.getResultFromStat(dirPath, column, stat, queryResults); - } else { - this.getNormalResult(dirPath, column, queryResults); - } - } - } catch (e) { - hilog.error(DOMAIN_CODE, TAG, 'query error ' + e.message); - return { - results: [], - code: E_GETRESULT, - }; - } - return { - results: queryResults, - code: ERR_OK, - }; + query(uri, columns): {[], number} { + if (!this.checkUri(uri)) { + return { + results: [], + code: E_URIS, + }; + } + + if (!this.access(uri).isExist) { + return { + results: [], + code: E_GETRESULT, + }; + } + let queryResults = []; + try { + let dirPath = this.getPath(uri); + let stat = fileIO.statSync(dirPath); + for (let index in columns) { + let column = columns[index]; + if (column in stat) { + this.getResultFromStat(dirPath, column, stat, queryResults); + } else { + this.getNormalResult(dirPath, column, queryResults); + } + } + } catch (e) { + hilog.error(DOMAIN_CODE, TAG, 'query error ' + e.message); + return { + results: [], + code: E_GETRESULT, + }; } + return { + results: queryResults, + code: ERR_OK, + }; + } }; \ No newline at end of file -- Gitee