1bea4f105Sopenharmony_ci/* 2bea4f105Sopenharmony_ci * Copyright (c) 2023-2024 Huawei Device Co., Ltd. 3bea4f105Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 4bea4f105Sopenharmony_ci * you may not use this file except in compliance with the License. 5bea4f105Sopenharmony_ci * You may obtain a copy of the License at 6bea4f105Sopenharmony_ci * 7bea4f105Sopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 8bea4f105Sopenharmony_ci * 9bea4f105Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software 10bea4f105Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 11bea4f105Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12bea4f105Sopenharmony_ci * See the License for the specific language governing permissions and 13bea4f105Sopenharmony_ci * limitations under the License. 14bea4f105Sopenharmony_ci */ 15bea4f105Sopenharmony_ci 16bea4f105Sopenharmony_ciimport type { BusinessError } from '@ohos.base'; 17bea4f105Sopenharmony_ciimport fs from '@ohos.file.fs'; 18bea4f105Sopenharmony_ciimport fileuri from '@ohos.file.fileuri'; 19bea4f105Sopenharmony_ciimport type uri from '@ohos.uri'; 20bea4f105Sopenharmony_ciimport Logger from '../log/Logger'; 21bea4f105Sopenharmony_ci 22bea4f105Sopenharmony_ciexport class FsUtil { 23bea4f105Sopenharmony_ci static readonly TAG: string = 'FsUtil'; 24bea4f105Sopenharmony_ci 25bea4f105Sopenharmony_ci public static async stat(file: string | number): Promise<fs.Stat | BusinessError> { 26bea4f105Sopenharmony_ci try { 27bea4f105Sopenharmony_ci return await fs.stat(file); 28bea4f105Sopenharmony_ci } catch (error) { 29bea4f105Sopenharmony_ci Logger.i(FsUtil.TAG, 'fs stat error = ' + JSON.stringify(error)); 30bea4f105Sopenharmony_ci return error; 31bea4f105Sopenharmony_ci } 32bea4f105Sopenharmony_ci } 33bea4f105Sopenharmony_ci 34bea4f105Sopenharmony_ci public static statSync(file: string | number): fs.Stat | BusinessError { 35bea4f105Sopenharmony_ci try { 36bea4f105Sopenharmony_ci return fs.statSync(file); 37bea4f105Sopenharmony_ci } catch (error) { 38bea4f105Sopenharmony_ci Logger.i(FsUtil.TAG, 'fs statSync error = ' + JSON.stringify(error)); 39bea4f105Sopenharmony_ci return error; 40bea4f105Sopenharmony_ci } 41bea4f105Sopenharmony_ci } 42bea4f105Sopenharmony_ci 43bea4f105Sopenharmony_ci public static async access(path: string): Promise<boolean | BusinessError> { 44bea4f105Sopenharmony_ci try { 45bea4f105Sopenharmony_ci return await fs.access(path); 46bea4f105Sopenharmony_ci } catch (error) { 47bea4f105Sopenharmony_ci Logger.i(FsUtil.TAG, 'fs access error = ' + JSON.stringify(error)); 48bea4f105Sopenharmony_ci return error; 49bea4f105Sopenharmony_ci } 50bea4f105Sopenharmony_ci } 51bea4f105Sopenharmony_ci 52bea4f105Sopenharmony_ci public static accessSync(path: string): boolean { 53bea4f105Sopenharmony_ci try { 54bea4f105Sopenharmony_ci return fs.accessSync(path); 55bea4f105Sopenharmony_ci } catch (error) { 56bea4f105Sopenharmony_ci Logger.i(FsUtil.TAG, 'fs accessSync error = ' + JSON.stringify(error)); 57bea4f105Sopenharmony_ci return false; 58bea4f105Sopenharmony_ci } 59bea4f105Sopenharmony_ci } 60bea4f105Sopenharmony_ci 61bea4f105Sopenharmony_ci public static openSync(path: string, mode?: number): fs.File | BusinessError { 62bea4f105Sopenharmony_ci try { 63bea4f105Sopenharmony_ci return fs.openSync(path, mode); 64bea4f105Sopenharmony_ci } catch (error) { 65bea4f105Sopenharmony_ci Logger.i(FsUtil.TAG, 'fs openSync error = ' + JSON.stringify(error)); 66bea4f105Sopenharmony_ci return error; 67bea4f105Sopenharmony_ci } 68bea4f105Sopenharmony_ci } 69bea4f105Sopenharmony_ci 70bea4f105Sopenharmony_ci public static async close(file: number | fs.File): Promise<void | BusinessError> { 71bea4f105Sopenharmony_ci try { 72bea4f105Sopenharmony_ci return await fs.close(file); 73bea4f105Sopenharmony_ci } catch (error) { 74bea4f105Sopenharmony_ci Logger.i(FsUtil.TAG, 'fs close error = ' + JSON.stringify(error)); 75bea4f105Sopenharmony_ci return error; 76bea4f105Sopenharmony_ci } 77bea4f105Sopenharmony_ci } 78bea4f105Sopenharmony_ci 79bea4f105Sopenharmony_ci public static closeSync(file: number | fs.File): void | BusinessError { 80bea4f105Sopenharmony_ci try { 81bea4f105Sopenharmony_ci return fs.closeSync(file); 82bea4f105Sopenharmony_ci } catch (error) { 83bea4f105Sopenharmony_ci Logger.i(FsUtil.TAG, 'fs closeSync error = ' + JSON.stringify(error)); 84bea4f105Sopenharmony_ci return error; 85bea4f105Sopenharmony_ci } 86bea4f105Sopenharmony_ci } 87bea4f105Sopenharmony_ci 88bea4f105Sopenharmony_ci public static async mkdir(path: string, recursion: boolean = false): Promise<void | BusinessError> { 89bea4f105Sopenharmony_ci try { 90bea4f105Sopenharmony_ci return await fs.mkdir(path, recursion); 91bea4f105Sopenharmony_ci } catch (error) { 92bea4f105Sopenharmony_ci Logger.i(FsUtil.TAG, 'fs mkdir error = ' + JSON.stringify(error)); 93bea4f105Sopenharmony_ci return error; 94bea4f105Sopenharmony_ci } 95bea4f105Sopenharmony_ci } 96bea4f105Sopenharmony_ci 97bea4f105Sopenharmony_ci public static mkdirSync(path: string, recursion: boolean = false): void | BusinessError { 98bea4f105Sopenharmony_ci try { 99bea4f105Sopenharmony_ci return fs.mkdirSync(path, recursion); 100bea4f105Sopenharmony_ci } catch (error) { 101bea4f105Sopenharmony_ci Logger.i(FsUtil.TAG, 'fs mkdirSync error = ' + JSON.stringify(error)); 102bea4f105Sopenharmony_ci return error; 103bea4f105Sopenharmony_ci } 104bea4f105Sopenharmony_ci } 105bea4f105Sopenharmony_ci 106bea4f105Sopenharmony_ci public static async rmdir(path: string): Promise<void | BusinessError> { 107bea4f105Sopenharmony_ci try { 108bea4f105Sopenharmony_ci return await fs.rmdir(path); 109bea4f105Sopenharmony_ci } catch (error) { 110bea4f105Sopenharmony_ci Logger.i(FsUtil.TAG, 'fs rmdir error = ' + JSON.stringify(error)); 111bea4f105Sopenharmony_ci return error; 112bea4f105Sopenharmony_ci } 113bea4f105Sopenharmony_ci } 114bea4f105Sopenharmony_ci 115bea4f105Sopenharmony_ci public static rmdirSync(path: string): void | BusinessError { 116bea4f105Sopenharmony_ci try { 117bea4f105Sopenharmony_ci return fs.rmdirSync(path); 118bea4f105Sopenharmony_ci } catch (error) { 119bea4f105Sopenharmony_ci Logger.i(FsUtil.TAG, 'fs rmdirSync error = ' + JSON.stringify(error)); 120bea4f105Sopenharmony_ci return error; 121bea4f105Sopenharmony_ci } 122bea4f105Sopenharmony_ci } 123bea4f105Sopenharmony_ci 124bea4f105Sopenharmony_ci public static async moveFile(src: string, dest: string, mode?: number): Promise<void | BusinessError> { 125bea4f105Sopenharmony_ci try { 126bea4f105Sopenharmony_ci return await fs.moveFile(src, dest, mode); 127bea4f105Sopenharmony_ci } catch (error) { 128bea4f105Sopenharmony_ci Logger.i(FsUtil.TAG, 'fs moveFile error = ' + JSON.stringify(error)); 129bea4f105Sopenharmony_ci return error; 130bea4f105Sopenharmony_ci } 131bea4f105Sopenharmony_ci } 132bea4f105Sopenharmony_ci 133bea4f105Sopenharmony_ci public static async moveDir(src: string, dest: string, mode?: number): Promise<void | BusinessError> { 134bea4f105Sopenharmony_ci try { 135bea4f105Sopenharmony_ci return await fs.moveDir(src, dest, mode); 136bea4f105Sopenharmony_ci } catch (error) { 137bea4f105Sopenharmony_ci Logger.i(FsUtil.TAG, 'fs moveDir error = ' + JSON.stringify(error)); 138bea4f105Sopenharmony_ci return error; 139bea4f105Sopenharmony_ci } 140bea4f105Sopenharmony_ci } 141bea4f105Sopenharmony_ci 142bea4f105Sopenharmony_ci public static moveFileSync(src: string, dest: string, mode?: number): void | BusinessError { 143bea4f105Sopenharmony_ci try { 144bea4f105Sopenharmony_ci return fs.moveFileSync(src, dest, mode); 145bea4f105Sopenharmony_ci } catch (error) { 146bea4f105Sopenharmony_ci Logger.i(FsUtil.TAG, 'fs moveFileSync error = ' + JSON.stringify(error)); 147bea4f105Sopenharmony_ci return error; 148bea4f105Sopenharmony_ci } 149bea4f105Sopenharmony_ci } 150bea4f105Sopenharmony_ci 151bea4f105Sopenharmony_ci public static moveDirSync(src: string, dest: string, mode?: number): void | BusinessError { 152bea4f105Sopenharmony_ci try { 153bea4f105Sopenharmony_ci return fs.moveDirSync(src, dest, mode); 154bea4f105Sopenharmony_ci } catch (error) { 155bea4f105Sopenharmony_ci Logger.i(FsUtil.TAG, 'fs moveDirSync error = ' + JSON.stringify(error)); 156bea4f105Sopenharmony_ci return error; 157bea4f105Sopenharmony_ci } 158bea4f105Sopenharmony_ci } 159bea4f105Sopenharmony_ci 160bea4f105Sopenharmony_ci public static async rename(oldPath: string, newPath: string): Promise<void | BusinessError> { 161bea4f105Sopenharmony_ci try { 162bea4f105Sopenharmony_ci return await fs.rename(oldPath, newPath); 163bea4f105Sopenharmony_ci } catch (error) { 164bea4f105Sopenharmony_ci Logger.i(FsUtil.TAG, 'fs rename error = ' + JSON.stringify(error)); 165bea4f105Sopenharmony_ci return error; 166bea4f105Sopenharmony_ci } 167bea4f105Sopenharmony_ci } 168bea4f105Sopenharmony_ci 169bea4f105Sopenharmony_ci public static renameSync(oldPath: string, newPath: string): void | BusinessError { 170bea4f105Sopenharmony_ci try { 171bea4f105Sopenharmony_ci return fs.renameSync(oldPath, newPath); 172bea4f105Sopenharmony_ci } catch (error) { 173bea4f105Sopenharmony_ci Logger.i(FsUtil.TAG, 'fs renameSync error = ' + JSON.stringify(error)); 174bea4f105Sopenharmony_ci return error; 175bea4f105Sopenharmony_ci } 176bea4f105Sopenharmony_ci } 177bea4f105Sopenharmony_ci 178bea4f105Sopenharmony_ci public static async unlink(path: string): Promise<void | BusinessError> { 179bea4f105Sopenharmony_ci try { 180bea4f105Sopenharmony_ci return await fs.unlink(path); 181bea4f105Sopenharmony_ci } catch (error) { 182bea4f105Sopenharmony_ci Logger.i(FsUtil.TAG, 'fs unlink error = ' + JSON.stringify(error)); 183bea4f105Sopenharmony_ci return error; 184bea4f105Sopenharmony_ci } 185bea4f105Sopenharmony_ci } 186bea4f105Sopenharmony_ci 187bea4f105Sopenharmony_ci public static unlinkSync(path: string): void | BusinessError { 188bea4f105Sopenharmony_ci try { 189bea4f105Sopenharmony_ci return fs.unlinkSync(path); 190bea4f105Sopenharmony_ci } catch (error) { 191bea4f105Sopenharmony_ci Logger.i(FsUtil.TAG, 'fs unlinkSync error = ' + JSON.stringify(error)); 192bea4f105Sopenharmony_ci return error; 193bea4f105Sopenharmony_ci } 194bea4f105Sopenharmony_ci } 195bea4f105Sopenharmony_ci 196bea4f105Sopenharmony_ci // @ts-ignore 197bea4f105Sopenharmony_ci public static async write(fd: number, buffer: ArrayBuffer | string, options?: fs.WriteOptions): Promise<number | BusinessError> { 198bea4f105Sopenharmony_ci try { 199bea4f105Sopenharmony_ci return await fs.write(fd, buffer, options); 200bea4f105Sopenharmony_ci } catch (error) { 201bea4f105Sopenharmony_ci Logger.i(FsUtil.TAG, 'fs write error = ' + JSON.stringify(error)); 202bea4f105Sopenharmony_ci return error; 203bea4f105Sopenharmony_ci } 204bea4f105Sopenharmony_ci } 205bea4f105Sopenharmony_ci 206bea4f105Sopenharmony_ci // @ts-ignore 207bea4f105Sopenharmony_ci public static writeSync(fd: number, buffer: ArrayBuffer | string, options?: fs.WriteOptions): number | BusinessError { 208bea4f105Sopenharmony_ci try { 209bea4f105Sopenharmony_ci return fs.writeSync(fd, buffer, options); 210bea4f105Sopenharmony_ci } catch (error) { 211bea4f105Sopenharmony_ci Logger.i(FsUtil.TAG, 'fs writeSync error = ' + JSON.stringify(error)); 212bea4f105Sopenharmony_ci return error; 213bea4f105Sopenharmony_ci } 214bea4f105Sopenharmony_ci } 215bea4f105Sopenharmony_ci 216bea4f105Sopenharmony_ci // @ts-ignore 217bea4f105Sopenharmony_ci public static async read(fd: number, buffer: ArrayBuffer, options?: fs.ReadOptions): Promise<number | BusinessError> { 218bea4f105Sopenharmony_ci try { 219bea4f105Sopenharmony_ci return await fs.read(fd, buffer, options); 220bea4f105Sopenharmony_ci } catch (error) { 221bea4f105Sopenharmony_ci Logger.i(FsUtil.TAG, 'fs read error = ' + JSON.stringify(error)); 222bea4f105Sopenharmony_ci return error; 223bea4f105Sopenharmony_ci } 224bea4f105Sopenharmony_ci } 225bea4f105Sopenharmony_ci 226bea4f105Sopenharmony_ci // @ts-ignore 227bea4f105Sopenharmony_ci public static readSync(fd: number, buffer: ArrayBuffer, options?: fs.ReadOptions): number | BusinessError { 228bea4f105Sopenharmony_ci try { 229bea4f105Sopenharmony_ci return fs.readSync(fd, buffer, options); 230bea4f105Sopenharmony_ci } catch (error) { 231bea4f105Sopenharmony_ci Logger.i(FsUtil.TAG, 'fs readSync error = ' + JSON.stringify(error)); 232bea4f105Sopenharmony_ci return error; 233bea4f105Sopenharmony_ci } 234bea4f105Sopenharmony_ci } 235bea4f105Sopenharmony_ci 236bea4f105Sopenharmony_ci public static readTextSync(path: string): string | BusinessError { 237bea4f105Sopenharmony_ci try { 238bea4f105Sopenharmony_ci return fs.readTextSync(path); 239bea4f105Sopenharmony_ci } catch (error) { 240bea4f105Sopenharmony_ci Logger.i(FsUtil.TAG, `fs readTextSync error = ${JSON.stringify(error)}`); 241bea4f105Sopenharmony_ci return error; 242bea4f105Sopenharmony_ci } 243bea4f105Sopenharmony_ci } 244bea4f105Sopenharmony_ci 245bea4f105Sopenharmony_ci // @ts-ignore 246bea4f105Sopenharmony_ci public static listFileSync(path: string, options?: fs.ListFileOptions): string[] | BusinessError { 247bea4f105Sopenharmony_ci try { 248bea4f105Sopenharmony_ci let res = fs.listFileSync(path, options); 249bea4f105Sopenharmony_ci return res; 250bea4f105Sopenharmony_ci } catch (error) { 251bea4f105Sopenharmony_ci Logger.i(FsUtil.TAG, 'fs listFileSync error = ' + JSON.stringify(error)); 252bea4f105Sopenharmony_ci return error; 253bea4f105Sopenharmony_ci } 254bea4f105Sopenharmony_ci } 255bea4f105Sopenharmony_ci 256bea4f105Sopenharmony_ci public static async fsync(fd: number): Promise<void | BusinessError> { 257bea4f105Sopenharmony_ci try { 258bea4f105Sopenharmony_ci return await fs.fsync(fd); 259bea4f105Sopenharmony_ci } catch (error) { 260bea4f105Sopenharmony_ci Logger.i(FsUtil.TAG, 'fs fsync error = ' + JSON.stringify(error)); 261bea4f105Sopenharmony_ci return error; 262bea4f105Sopenharmony_ci } 263bea4f105Sopenharmony_ci } 264bea4f105Sopenharmony_ci 265bea4f105Sopenharmony_ci public static fsyncSync(fd: number): void | BusinessError { 266bea4f105Sopenharmony_ci try { 267bea4f105Sopenharmony_ci return fs.fsyncSync(fd); 268bea4f105Sopenharmony_ci } catch (error) { 269bea4f105Sopenharmony_ci Logger.i(FsUtil.TAG, 'fs fsync error = ' + JSON.stringify(error)); 270bea4f105Sopenharmony_ci return error; 271bea4f105Sopenharmony_ci } 272bea4f105Sopenharmony_ci } 273bea4f105Sopenharmony_ci 274bea4f105Sopenharmony_ci /** 275bea4f105Sopenharmony_ci * 强制删除文件 276bea4f105Sopenharmony_ci * @param uri 删除文件的uri 277bea4f105Sopenharmony_ci */ 278bea4f105Sopenharmony_ci public static forceDelete(uri: string): number | BusinessError { 279bea4f105Sopenharmony_ci try { 280bea4f105Sopenharmony_ci let fileUri: fileuri.FileUri = new fileuri.FileUri(uri); 281bea4f105Sopenharmony_ci let filePath: string = fileUri.path; 282bea4f105Sopenharmony_ci if (fs.statSync(filePath).isDirectory()) { 283bea4f105Sopenharmony_ci fs.rmdirSync(filePath); 284bea4f105Sopenharmony_ci } else { 285bea4f105Sopenharmony_ci fs.unlinkSync(filePath); 286bea4f105Sopenharmony_ci } 287bea4f105Sopenharmony_ci return 0; 288bea4f105Sopenharmony_ci } catch (error) { 289bea4f105Sopenharmony_ci Logger.i(FsUtil.TAG, 'force delete file error: ' + JSON.stringify(error)); 290bea4f105Sopenharmony_ci return error; 291bea4f105Sopenharmony_ci } 292bea4f105Sopenharmony_ci } 293bea4f105Sopenharmony_ci 294bea4f105Sopenharmony_ci /** 295bea4f105Sopenharmony_ci * 文件夹判空 296bea4f105Sopenharmony_ci * @param path 文件夹的uri 297bea4f105Sopenharmony_ci */ 298bea4f105Sopenharmony_ci public static isFolderEmpty(path: string): boolean | BusinessError { 299bea4f105Sopenharmony_ci try { 300bea4f105Sopenharmony_ci let fileList = fs.listFileSync(path, { listNum: 1 }); 301bea4f105Sopenharmony_ci return fileList.length === 0; 302bea4f105Sopenharmony_ci } catch (error) { 303bea4f105Sopenharmony_ci Logger.i(FsUtil.TAG, 'isFolderEmpty error: ' + JSON.stringify(error)); 304bea4f105Sopenharmony_ci return error; 305bea4f105Sopenharmony_ci } 306bea4f105Sopenharmony_ci } 307bea4f105Sopenharmony_ci /** 308bea4f105Sopenharmony_ci * 判断文件是否为文件夹(目前暂不支持应用沙箱目录) 309bea4f105Sopenharmony_ci * @param path 文件 310bea4f105Sopenharmony_ci * @returns 311bea4f105Sopenharmony_ci */ 312bea4f105Sopenharmony_ci public static isFolder(path: string): boolean | BusinessError { 313bea4f105Sopenharmony_ci try { 314bea4f105Sopenharmony_ci let stat = fs.statSync(path); 315bea4f105Sopenharmony_ci return stat?.isDirectory(); 316bea4f105Sopenharmony_ci } catch (error) { 317bea4f105Sopenharmony_ci Logger.i(FsUtil.TAG, path + ' isFolder error: ' + JSON.stringify(error)); 318bea4f105Sopenharmony_ci return error; 319bea4f105Sopenharmony_ci } 320bea4f105Sopenharmony_ci } 321bea4f105Sopenharmony_ci 322bea4f105Sopenharmony_ci /** 323bea4f105Sopenharmony_ci * 判断文件是否存在 324bea4f105Sopenharmony_ci * @param uri 文件uri 325bea4f105Sopenharmony_ci * @returns 判断结果 326bea4f105Sopenharmony_ci */ 327bea4f105Sopenharmony_ci public static isFileExist(uri: string): boolean { 328bea4f105Sopenharmony_ci let isExist: boolean = false; 329bea4f105Sopenharmony_ci try { 330bea4f105Sopenharmony_ci Logger.i(FsUtil.TAG, 'open start'); 331bea4f105Sopenharmony_ci let fileFd: fs.File = fs.openSync(uri, fs.OpenMode.READ_ONLY); 332bea4f105Sopenharmony_ci fs.closeSync(fileFd); 333bea4f105Sopenharmony_ci isExist = true; 334bea4f105Sopenharmony_ci } catch (error) { 335bea4f105Sopenharmony_ci Logger.i(FsUtil.TAG, 'openSync fail: ' + JSON.stringify(error)); 336bea4f105Sopenharmony_ci } 337bea4f105Sopenharmony_ci return isExist; 338bea4f105Sopenharmony_ci } 339bea4f105Sopenharmony_ci 340bea4f105Sopenharmony_ci /** 341bea4f105Sopenharmony_ci * 判断文件是否被删除(包括软删除和硬删除) 342bea4f105Sopenharmony_ci * @param uri 文件uri 343bea4f105Sopenharmony_ci * @returns 判断结果 344bea4f105Sopenharmony_ci */ 345bea4f105Sopenharmony_ci public static isFileDeleted(uri: string): boolean { 346bea4f105Sopenharmony_ci try { 347bea4f105Sopenharmony_ci Logger.i(FsUtil.TAG, 'open start'); 348bea4f105Sopenharmony_ci let fileFd: fs.File = fs.openSync(uri, fs.OpenMode.READ_ONLY); // 此处报错说明被硬删除了 349bea4f105Sopenharmony_ci const path = fileFd.path; 350bea4f105Sopenharmony_ci fs.closeSync(fileFd); 351bea4f105Sopenharmony_ci let stat = fs.statSync(path); 352bea4f105Sopenharmony_ci if (stat.ctime === 0 && stat.mtime === 0) { // 说明被软删除了 353bea4f105Sopenharmony_ci return true; 354bea4f105Sopenharmony_ci } 355bea4f105Sopenharmony_ci return false; 356bea4f105Sopenharmony_ci } catch (error) { 357bea4f105Sopenharmony_ci Logger.i(FsUtil.TAG, 'openSync fail: ' + JSON.stringify(error)); 358bea4f105Sopenharmony_ci return true; 359bea4f105Sopenharmony_ci } 360bea4f105Sopenharmony_ci } 361bea4f105Sopenharmony_ci 362bea4f105Sopenharmony_ci /** 363bea4f105Sopenharmony_ci * 判断目录下是否存在同名文件 364bea4f105Sopenharmony_ci * @param destUri:目录uri 365bea4f105Sopenharmony_ci * @param fileName:待判断的文件名 366bea4f105Sopenharmony_ci * @returns 判断结果 367bea4f105Sopenharmony_ci */ 368bea4f105Sopenharmony_ci public static isExistDupName(destUri: string, fileName: string): boolean { 369bea4f105Sopenharmony_ci let isExistDupName: boolean = false; 370bea4f105Sopenharmony_ci try { 371bea4f105Sopenharmony_ci let destFileInfo: uri.URI = new fileuri.FileUri(destUri); 372bea4f105Sopenharmony_ci let newFilePath: string = destFileInfo.path + '/' + fileName; 373bea4f105Sopenharmony_ci isExistDupName = fs.accessSync(newFilePath); 374bea4f105Sopenharmony_ci } catch (err) { 375bea4f105Sopenharmony_ci Logger.i(FsUtil.TAG, 'isExistDupName err: ' + JSON.stringify(err)); 376bea4f105Sopenharmony_ci } 377bea4f105Sopenharmony_ci return isExistDupName; 378bea4f105Sopenharmony_ci } 379bea4f105Sopenharmony_ci 380bea4f105Sopenharmony_ci public static getInoByUri(uri: string): string { 381bea4f105Sopenharmony_ci try { 382bea4f105Sopenharmony_ci let fileUri: fileuri.FileUri = new fileuri.FileUri(uri); 383bea4f105Sopenharmony_ci let stat = fs.statSync(fileUri.path); 384bea4f105Sopenharmony_ci return stat.ino.toString(); 385bea4f105Sopenharmony_ci } catch (error) { 386bea4f105Sopenharmony_ci Logger.i(FsUtil.TAG, `get ino failed, error message : ${error?.message}, error code : ${error?.code}`); 387bea4f105Sopenharmony_ci return ''; 388bea4f105Sopenharmony_ci } 389bea4f105Sopenharmony_ci } 390bea4f105Sopenharmony_ci 391bea4f105Sopenharmony_ci /** 392bea4f105Sopenharmony_ci * 文件拷贝同步接口,适合十几兆的小文件拷贝 393bea4f105Sopenharmony_ci * @param srcPath 源文件 394bea4f105Sopenharmony_ci * @param destinationPath 目标文件 395bea4f105Sopenharmony_ci * @param mode 拷贝模式 396bea4f105Sopenharmony_ci * @returns true:拷贝成功,目标文件已经存在 397bea4f105Sopenharmony_ci */ 398bea4f105Sopenharmony_ci public static copyFileSyncByPath(srcPath: string, destinationPath: string, mode?: number): boolean { 399bea4f105Sopenharmony_ci try { 400bea4f105Sopenharmony_ci fs.copyFileSync(srcPath, destinationPath, mode); 401bea4f105Sopenharmony_ci return FsUtil.isExistSyncByPath(destinationPath); 402bea4f105Sopenharmony_ci } catch (error) { 403bea4f105Sopenharmony_ci Logger.i(FsUtil.TAG, 'copyFileSyncByPath err: ' + JSON.stringify(error)); 404bea4f105Sopenharmony_ci return false; 405bea4f105Sopenharmony_ci } 406bea4f105Sopenharmony_ci } 407bea4f105Sopenharmony_ci 408bea4f105Sopenharmony_ci /** 409bea4f105Sopenharmony_ci * 判断文件是否存在 410bea4f105Sopenharmony_ci * 411bea4f105Sopenharmony_ci * @param path 文件全目录 412bea4f105Sopenharmony_ci * @returns true:文件存在 413bea4f105Sopenharmony_ci */ 414bea4f105Sopenharmony_ci public static isExistSyncByPath(path: string): boolean { 415bea4f105Sopenharmony_ci try { 416bea4f105Sopenharmony_ci return fs.accessSync(path); 417bea4f105Sopenharmony_ci } catch (error) { 418bea4f105Sopenharmony_ci Logger.i(FsUtil.TAG, 'isExistSyncByPath error = ' + JSON.stringify(error)); 419bea4f105Sopenharmony_ci return false; 420bea4f105Sopenharmony_ci } 421bea4f105Sopenharmony_ci } 422bea4f105Sopenharmony_ci 423bea4f105Sopenharmony_ci /** 424bea4f105Sopenharmony_ci * 重命名异步接口 425bea4f105Sopenharmony_ci * @param oldPath 即将要重命名的文件全路径 426bea4f105Sopenharmony_ci * @param newPath 重命名之后的文件全路径 427bea4f105Sopenharmony_ci * @returns true:命名成功 428bea4f105Sopenharmony_ci */ 429bea4f105Sopenharmony_ci public static renameSyncByPath(oldPath: string, newPath: string): boolean { 430bea4f105Sopenharmony_ci try { 431bea4f105Sopenharmony_ci fs.renameSync(oldPath, newPath); 432bea4f105Sopenharmony_ci return FsUtil.isExistSyncByPath(newPath); 433bea4f105Sopenharmony_ci } catch (error) { 434bea4f105Sopenharmony_ci Logger.i(FsUtil.TAG, 'fs renameSync error = ' + JSON.stringify(error)); 435bea4f105Sopenharmony_ci return false; 436bea4f105Sopenharmony_ci } 437bea4f105Sopenharmony_ci } 438bea4f105Sopenharmony_ci 439bea4f105Sopenharmony_ci /** 440bea4f105Sopenharmony_ci * 同步获取文件大小 441bea4f105Sopenharmony_ci * @param path 文件全路径 442bea4f105Sopenharmony_ci * @returns 文件夹大小 443bea4f105Sopenharmony_ci */ 444bea4f105Sopenharmony_ci public static getFileSizeSyncByPath(path: string): number { 445bea4f105Sopenharmony_ci let size = 0; 446bea4f105Sopenharmony_ci try { 447bea4f105Sopenharmony_ci let stat = fs.statSync(path); 448bea4f105Sopenharmony_ci size = stat.size; 449bea4f105Sopenharmony_ci } catch (error) { 450bea4f105Sopenharmony_ci Logger.i(FsUtil.TAG, 'getFileInfoByFs fail, error:' + JSON.stringify(error)); 451bea4f105Sopenharmony_ci } 452bea4f105Sopenharmony_ci return size; 453bea4f105Sopenharmony_ci } 454bea4f105Sopenharmony_ci}