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 2e82947b721f0596b2c73dd3142a00cc8d242287..214916d9d5a6627a614fb3856c188c69a5c46e5b 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 @@ -14,7 +14,7 @@ */ // @ts-nocheck import Extension from '@ohos.application.FileAccessExtensionAbility'; -import fileio from '@ohos.fileio'; +import fs from '@ohos.file.fs'; import { init, delVolumeInfo, getVolumeInfoList, path2uri } from './VolumeManager'; import { onReceiveEvent } from './Subcriber'; import fileExtensionInfo from '@ohos.file.fileExtensionInfo'; @@ -25,10 +25,9 @@ const deviceFlag = fileExtensionInfo.DeviceFlag; const documentFlag = fileExtensionInfo.DocumentFlag; 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 DEFAULT_MODE = 0; const TAG = 'ExternalFileManager'; const ERR_OK = 0; const ERR_ERROR = -1; @@ -118,32 +117,6 @@ export default class FileExtAbility extends Extension { return newFileUri; } - 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): boolean { let roots = this.getRoots().roots; for (let index = 0; index < roots.length; index++) { @@ -162,12 +135,11 @@ export default class FileExtAbility extends Extension { code: E_URIS, }; } - let fd = 0; try { let path = this.getPath(sourceFileUri); - fd = fileio.openSync(path, flags, DEFAULT_MODE); + let file = fs.openSync(path, flags); return { - fd: fd, + fd: file.fd, code: ERR_OK, }; } catch (e) { @@ -195,8 +167,8 @@ export default class FileExtAbility extends Extension { }; } let path = this.getPath(newFileUri); - let fd = fileio.openSync(path, CREATE_FILE_FLAGS, DEFAULT_MODE); - fileio.closeSync(fd); + let file = fs.openSync(path, fs.OpenMode.CREATE); + fs.closeSync(file); return { uri: newFileUri, code: ERR_OK, @@ -220,7 +192,7 @@ export default class FileExtAbility extends Extension { try { let newFileUri = this.genNewFileUri(parentUri, displayName); let path = this.getPath(newFileUri); - fileio.mkdirSync(path); + fs.mkdirSync(path); return { uri: newFileUri, code: ERR_OK, @@ -240,23 +212,12 @@ export default class FileExtAbility extends Extension { } 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; - } - }); + try { + fs.rmdirSync(path); + } catch (e) { + hilog.error(DOMAIN_CODE, TAG, 'deleteFile error ' + e.message); + return e.code; + } return code; } @@ -271,6 +232,7 @@ export default class FileExtAbility extends Extension { 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 { @@ -286,17 +248,23 @@ export default class FileExtAbility extends Extension { } try { // The source file does not exist or the destination is not a directory - fileio.accessSync(oldPath); - let stat = fileio.statSync(this.getPath(targetParentUri)); + let isAccess = fs.accessSync(oldPath); + if (!isAccess) { + return { + uri: '', + code: E_GETRESULT, + }; + } + let stat = fs.statSync(this.getPath(targetParentUri)); if (!stat || !stat.isDirectory()) { return { uri: '', code: E_GETRESULT, }; } - // If not across devices, use fileio.renameSync to move + // If not across devices, use fs.renameSync to move if (!this.isCrossDeviceLink(sourceFileUri, targetParentUri)) { - fileio.renameSync(oldPath, newPath); + fs.renameSync(oldPath, newPath); return { uri: newFileUri, code: ERR_OK, @@ -309,39 +277,15 @@ export default class FileExtAbility extends Extension { 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 { - // 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); - } - } catch (e) { - hilog.error(DOMAIN_CODE, TAG, 'move error ' + e.message); - return { - uri: '', - code: e.code, - }; - } - }); + try { + fs.moveDirSync(oldPath, newPath, DEFAULT_MODE); + } catch (e) { + hilog.error(DOMAIN_CODE, TAG, 'move error ' + e.message); + return { + uri: '', + code: e.code, + }; + } return { uri: newFileUri, code: ERR_OK, @@ -359,7 +303,7 @@ export default class FileExtAbility extends Extension { let newFileUri = this.renameUri(sourceFileUri, displayName); let oldPath = this.getPath(sourceFileUri); let newPath = this.getPath(newFileUri); - fileio.renameSync(oldPath, newPath); + fs.renameSync(oldPath, newPath); return { uri: newFileUri, code: ERR_OK, @@ -381,24 +325,25 @@ export default class FileExtAbility extends Extension { code: E_URIS, }; } + let isAccess = false; 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') { + isAccess = fs.accessSync(path); + if (!isAccess) { return { - isExist: false, + isExist: isAccess, code: ERR_OK, }; } + } catch (e) { + hilog.error(DOMAIN_CODE, TAG, 'access error ' + e.message); return { - isExist: false, + isExist: isAccess, code: e.code, }; } return { - isExist: true, + isExist: isAccess, code: ERR_OK, }; } @@ -411,53 +356,44 @@ export default class FileExtAbility extends Extension { }; } let infos = []; - 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; - } - - infos.push({ - uri: this.genNewFileUri(sourceFileUri, dirent.name), - fileName: dirent.name, - mode: mode, - size: stat.size, - mtime: stat.mtime, - mimeType: '', - }); + let path = this.getPath(sourceFileUri); + let statPath = fs.statSync(path); + if (!statPath.isDirectory()) { + return { + infos: [], + code: E_GETRESULT, + }; + } - i++; - if (i === (offset + count)) { - hasNextFile = false; - break; - } - } catch (e) { - hasNextFile = false; + try { + let fileName = fs.listFileSync(path); + for (let i = 0; i < fileName.length; i++) { + if (offset > i) { + continue; } + if (i === (offset + count)) { + break; + } + let mode = documentFlag.SUPPORTS_READ | documentFlag.SUPPORTS_WRITE; + let stat = fs.statSync(path + '/' + fileName[i]); + if (stat.isDirectory()) { + mode |= documentFlag.REPRESENTS_DIR; + } else { + mode |= documentFlag.REPRESENTS_FILE; + } + infos.push({ + uri: this.genNewFileUri(sourceFileUri, fileName[i]), + fileName: fileName[i], + mode: mode, + size: stat.size, + mtime: stat.mtime, + mimeType: '', + }); } - dir.closeSync(); } catch (e) { - hilog.error(DOMAIN_CODE, TAG, 'listFile error ' + e.message); + hilog.error(DOMAIN_CODE, TAG, 'getFileinfo 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, }; } @@ -478,7 +414,7 @@ export default class FileExtAbility extends Extension { try { let path = this.getPath(selectFileUri); let fileName = this.getFileName(path); - let stat = fileio.statSync(path); + let stat = fs.statSync(path); let mode = documentFlag.SUPPORTS_READ | documentFlag.SUPPORTS_WRITE; if (stat.isDirectory()) { mode |= documentFlag.REPRESENTS_DIR; @@ -536,16 +472,37 @@ export default class FileExtAbility extends Extension { } } + recurseDir(path, cb): void { + try { + let stat = fs.statSync(path); + if (stat.isDirectory()) { + let fileName = fs.listFileSync(path); + for (let fileLen = 0; fileLen < fileName.length; fileLen++) { + let statFileName = fs.statSync(path + '/' + fileName[fileLen]); + if (statFileName.isDirectory()) { + this.recurseDir(path + '/' + fileName[fileLen], cb); + } else { + cb(path + '/' + fileName[fileLen], false); + } + } + } else { + cb(path, false); + } + } catch (e) { + hilog.error(DOMAIN_CODE, TAG, 'recurseDir error ' + e.message); + cb(path, true); + } + } + getResultFromStat(dirPath, column, stat, queryResults): void { if (column === 'size' && stat.isDirectory()) { let size = 0; - this.recurseDir(dirPath, function (filePath, isDirectory, hasNextFile) { - if (isDirectory && !hasNextFile) { + this.recurseDir(dirPath, function (filePath, isDirectory) { + if (isDirectory) { return; } - if (!isDirectory) { - let fileStat = fileio.statSync(filePath); + let fileStat = fs.statSync(filePath); size += fileStat.size; } }); @@ -562,18 +519,16 @@ export default class FileExtAbility extends Extension { code: E_URIS, }; } - if (!this.access(uri).isExist) { return { results: [], code: E_NOEXIST, }; } - let queryResults = []; try { let dirPath = this.getPath(uri); - let stat = fileio.statSync(dirPath); + let stat = fs.statSync(dirPath); for (let index in columns) { let column = columns[index]; if (column in stat) {