自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

OpenHarmony 文件監(jiān)聽(tīng)開(kāi)發(fā)樣例介紹

系統(tǒng) OpenHarmony
監(jiān)聽(tīng)機(jī)制是一種常見(jiàn)的計(jì)算機(jī)安全技術(shù),它可以監(jiān)控計(jì)算機(jī)系統(tǒng)中的各種活動(dòng),以便及時(shí)發(fā)現(xiàn)和處理安全問(wèn)題。文件監(jiān)聽(tīng)具有重要的現(xiàn)實(shí)意義,對(duì)文件進(jìn)行監(jiān)聽(tīng),企業(yè)可以及時(shí)發(fā)現(xiàn)員工不當(dāng)處理敏感信息和意外泄漏信息的情況,采取行動(dòng)避免數(shù)據(jù)泄露。

想了解更多關(guān)于開(kāi)源的內(nèi)容,請(qǐng)?jiān)L問(wèn):

51CTO 開(kāi)源基礎(chǔ)軟件社區(qū)

https://ost.51cto.com

OpenHarmony是由開(kāi)放原子開(kāi)源基金會(huì)(OpenAtom Foundation)孵化及運(yùn)營(yíng)的開(kāi)源項(xiàng)目,目標(biāo)是面向全場(chǎng)景、全連接、全智能時(shí)代,基于開(kāi)源的方式,搭建一個(gè)智能終端設(shè)備操作系統(tǒng)的框架和平臺(tái),促進(jìn)萬(wàn)物互聯(lián)產(chǎn)業(yè)的繁榮發(fā)展。

監(jiān)聽(tīng)機(jī)制是一種常見(jiàn)的計(jì)算機(jī)安全技術(shù),它可以監(jiān)控計(jì)算機(jī)系統(tǒng)中的各種活動(dòng),以便及時(shí)發(fā)現(xiàn)和處理安全問(wèn)題。文件監(jiān)聽(tīng)具有重要的現(xiàn)實(shí)意義,對(duì)文件進(jìn)行監(jiān)聽(tīng),企業(yè)可以及時(shí)發(fā)現(xiàn)員工不當(dāng)處理敏感信息和意外泄漏信息的情況,采取行動(dòng)避免數(shù)據(jù)泄露。

本文以文件管理FileManager為例,介紹文件監(jiān)聽(tīng)的應(yīng)用實(shí)現(xiàn)以此了解@ohos.file.fs模塊中涉及的接口的使用方法。

一、開(kāi)發(fā)環(huán)境準(zhǔn)備

下載并安裝DevEco Studio,建議使用DevEco Studio 3.1 Release (Build Version: 3.1.0.500, built on April 28, 2023)及以上版本:

  • DevEco Studio下載鏈接
  • 下載sample倉(cāng)庫(kù)源碼:
  • sample倉(cāng)源碼路徑

下載源碼之后將FileManager工程導(dǎo)入DevEco Studio,首先對(duì)工程進(jìn)行自動(dòng)化簽名,然后進(jìn)行編譯構(gòu)建生成HAP應(yīng)用安裝包,安裝應(yīng)用示例之前,請(qǐng)先查看"README_zh.md"文件來(lái)確認(rèn)應(yīng)用示例是否為stage模型,若為Stage模型需要查看entry/src/main路徑下的module.json5文件中的"deviceType"字段來(lái)確認(rèn)該應(yīng)用支持的設(shè)備類(lèi)型;否則為FA模型,查看entry/src/main路徑下的config.json文件中的"deviceType"字段來(lái)確認(rèn)該應(yīng)用示例支持的設(shè)備類(lèi)型,兩種模型都可嘗試通過(guò)修改該字段使其可以在相應(yīng)類(lèi)型的設(shè)備上運(yùn)行。文件管理FileManager為stage模型,支持設(shè)備為rk3568。安裝運(yùn)行之后,即可在設(shè)備上查看應(yīng)用示例運(yùn)行效果,以及進(jìn)行相關(guān)調(diào)試。

二、實(shí)現(xiàn)

文件管理新增功能為文件監(jiān)聽(tīng),使用@ohos.file.fs模塊watcher接口對(duì)文件、文件夾進(jìn)行監(jiān)控,添加相應(yīng)監(jiān)聽(tīng)后對(duì)文件進(jìn)行增加、刪除、修改操作后日志信息顯示出相應(yīng)監(jiān)聽(tīng)日志,停止相應(yīng)監(jiān)聽(tīng)后對(duì)文件進(jìn)行增加、刪除、修改操作后日志信息無(wú)變化。

在使用接口時(shí)需要查看并配置接口對(duì)應(yīng)的權(quán)限,監(jiān)聽(tīng)文件時(shí)使用了系統(tǒng)接口,需要配置系統(tǒng)應(yīng)用簽名,可以參考特殊權(quán)限配置方法 ,把配置文件中的“app-feature”字段信息改為“hos_system_app”,再將“apl”字段信息改為“system_core”。另外,監(jiān)聽(tīng)文件使用了API10接口,因此本工程僅支持API10版本的SDK。

1、流程圖

OpenHarmony 文件監(jiān)聽(tīng)開(kāi)發(fā)樣例介紹-開(kāi)源基礎(chǔ)軟件社區(qū)OpenHarmony 文件監(jiān)聽(tīng)開(kāi)發(fā)樣例介紹-開(kāi)源基礎(chǔ)軟件社區(qū)

2、實(shí)現(xiàn)過(guò)程

(1)增加監(jiān)聽(tīng)

使用數(shù)組watcherEvent保存監(jiān)聽(tīng)變動(dòng)的事件集,使用組件TextPickerDialog選擇監(jiān)聽(tīng)變動(dòng)事件集,當(dāng)用戶(hù)選擇一個(gè)監(jiān)聽(tīng)事件添加監(jiān)聽(tīng)時(shí),調(diào)用startWatcher()方法進(jìn)行監(jiān)聽(tīng),并將其保存到已添加監(jiān)聽(tīng)事件WatcherList數(shù)組中。

src/main/ets/pages/WatcherFile.ets:

TextPickerDialog.show({
   range: this.watcherEvent,
   selected: this.select,
   onAccept: (value: TextPickerResult) => {
     this.select = Number(value.index);
     this.eachEvent = this.watcherEvent[this.select];
     this.myWatcher.startWatcher(this.eachEvent);
     this.eventArray.push(this.eachEvent); // 新增列表項(xiàng)數(shù)據(jù)
   }
})

在startWatcher()方法中,根據(jù)需要添加的監(jiān)聽(tīng)事件使用fs.createWatcher()創(chuàng)建Watcher對(duì)象,用來(lái)監(jiān)聽(tīng)文件或目錄變動(dòng),調(diào)用watcher.start()開(kāi)啟監(jiān)聽(tīng),當(dāng)監(jiān)聽(tīng)事件發(fā)生一次,回調(diào)將會(huì)獲取監(jiān)聽(tīng)事件相關(guān)日志。

src/main/ets/filemanager/fileFs/MyWatcher.ts:

startWatcher(watcherName: string): void {
    let watcherDir: string = this.baseDir + '/watcherDir';
    Logger.info(TAG, `${watcherName}-startWatcher start path = ${watcherDir}`);
    if (this.watcherCodeMap.has(watcherName)) { // 是否存在key 'watchName': true
      try {
        Logger.info(TAG, `${watcherName}-startWatcher has watchName`);
        let watcher: fs.Watcher = fs.createWatcher(watcherDir, this.watcherCodeMap.get(watcherName), (data) => { //獲取key對(duì)應(yīng)的value
          AppStorage.SetOrCreate('eventLog', JSON.stringify(data.event));
          AppStorage.SetOrCreate('fileNameLog', data.fileName);
          AppStorage.SetOrCreate('cookieLog', JSON.stringify(data.cookie));
          Logger.info(TAG, `${watcherName}-startWatcher :{event: ${data.event}, fileName: ${data.fileName}, cookie: ${data.cookie}}`);
        });
        watcher.start();
        setTimeout(async () => {
          this.watcherList.push(watcher);
        }, this.timeOut);

        Logger.info('watcherList is ' + JSON.stringify(this.watcherList));
      } catch (e) {
        Logger.error(TAG, `${watcherName}-startWatcher has failed for: {message: ${e.message}, code: ${e.code}}`);
      }
    } else {
      Logger.info(TAG, `${watcherName}-startWatcher does not in watcherCodeMap`);
    }
  }

(2)停止監(jiān)聽(tīng)

當(dāng)用戶(hù)選擇一個(gè)監(jiān)聽(tīng)停止監(jiān)聽(tīng)時(shí),調(diào)用stopWatcher()方法停止監(jiān)聽(tīng),并將已添加監(jiān)聽(tīng)事件WatcherList數(shù)組中該監(jiān)聽(tīng)事件移除。

src/main/ets/pages/WatcherFile.ets:

TextPickerDialog.show({
   range: showEventArray,
   selected: this.selectDel,
   onAccept: (value: TextPickerResult) => {
     this.selectDel = Number(value.index);
     this.myWatcher.stopWatcher(this.myWatcher.watcherList[this.selectDel], this.selectDel);
     this.eventArray.splice(this.selectDel, 1);  // 刪除列表項(xiàng)數(shù)據(jù)
     showEventArray.splice(this.selectDel, 1); 
   }
 })

在stopWatcher()方法中,調(diào)用watcher.stop()停止監(jiān)聽(tīng),當(dāng)停止監(jiān)聽(tīng)的事件發(fā)生時(shí),不再產(chǎn)生監(jiān)聽(tīng)日志。

src/main/ets/filemanager/fileFs/MyWatcher.ts:

stopWatcher(watcher: fs.Watcher, index: number): void {
    if (watcher !== null) {
      watcher.stop();
      setTimeout(async () => {
        this.watcherList.splice(index, 1);
      }, this.timeOut);
      Logger.info(TAG, 'stopWatcher successful');
    } else {
      Logger.info(TAG, 'stopWatcher null');
    }
  }

(3)增加文件

當(dāng)對(duì)應(yīng)用沙箱文件或文件夾添加IN_CREATE監(jiān)聽(tīng)后,在沙箱文件路徑下新增一個(gè)文件,應(yīng)用內(nèi)則會(huì)打印出相應(yīng)監(jiān)聽(tīng)日志,使用fs.openSync打開(kāi)文件,fs.writeSync方法對(duì)文件進(jìn)行寫(xiě)入內(nèi)容,fs.closeSync關(guān)閉文件。在沙箱路徑下增加一個(gè)文件的具體實(shí)現(xiàn)方法如下所示:

src/main/ets/filemanager/fileFs/MyWatcher.ts:

addFileToWatcher(path: string): void {
    let content = CONTENT + '\n';
    try {
      let addFile = this.baseDir + '/watcherDir/' + path;
      Logger.info('addFileToWatcher addFile = ' + addFile);
      let file = fs.openSync(addFile, fs.OpenMode.CREATE | fs.OpenMode.READ_WRITE | fs.OpenMode.TRUNC);
      fs.writeSync(file.fd, content);
      fs.closeSync(file);
    } catch (e) {
      Logger.error(TAG, `addFileToWatcher has failed for: {message: ${e.message}, code: ${e.code}}`);
    }
  }

(4)刪除文件

當(dāng)對(duì)應(yīng)用沙箱文件或文件夾添加IN_DELETE監(jiān)聽(tīng)后,在沙箱文件路徑下刪除一個(gè)文件,應(yīng)用內(nèi)則會(huì)打印出相應(yīng)監(jiān)聽(tīng)日志,使用fs.unlink刪除文件。在沙箱路徑下刪除一個(gè)文件的具體實(shí)現(xiàn)方法如下所示:

src/main/ets/filemanager/fileFs/MyWatcher.ts:

deleteFileToWatcher(path: string): void {
    try {
      let deleteFile = this.baseDir + '/watcherDir/' + path;
      Logger.info(TAG, 'deleteFileToWatcher deleteFile = ' + deleteFile);
      fs.unlink(deleteFile).then(() => {
        Logger.info(TAG, 'deleteFileToWatcher file succeed');
      }).catch((err) => {
        Logger.info(TAG, 'deleteFileToWatcher file failed with error message: ' + err.message + ', error code: ' + err.codeor);
      });
    } catch (e) {
      Logger.error(TAG, `readyFileToWatcher has failed for: {message: ${e.message}, code: ${e.code}}`);
    }
  }

(5)修改文件

當(dāng)對(duì)應(yīng)用沙箱文件或文件夾添加IN_MODIFY監(jiān)聽(tīng)后,在沙箱文件路徑下對(duì)一個(gè)文件內(nèi)容進(jìn)行修改,應(yīng)用內(nèi)則會(huì)打印出相應(yīng)監(jiān)聽(tīng)日志,(注意:若只修改文件名,應(yīng)用內(nèi)不會(huì)打印監(jiān)聽(tīng)日志)。使用fs.renameSync對(duì)文件名進(jìn)行修改,使用fs.openSync打開(kāi)文件,fs.writeSync方法對(duì)文件進(jìn)行寫(xiě)入內(nèi)容,fs.closeSync關(guān)閉文件。在沙箱路徑下修改一個(gè)文件的具體實(shí)現(xiàn)方法如下所示:

src/main/ets/filemanager/fileFs/MyWatcher.ts:

modifyFileToWatcher(oldName: string, newName: string, content: string): number {
    // 重命名文件
    let filePath = null;
    if (newName.trim() === oldName.trim()) {
      filePath = this.baseDir + '/watcherDir/' + oldName;
      Logger.info('modifyFileToWatcher The new file name is the same as the old file name');
    } else {
      try {
        let srcFile = this.baseDir + '/watcherDir/' + oldName;
        Logger.info('modifyFileToWatcher srcFile = ' + srcFile);
        let dstFile = this.baseDir + '/watcherDir/' + newName;
        filePath = dstFile;
        Logger.info('modifyFileToWatcher dstFile = ' + dstFile);
        fs.renameSync(srcFile, dstFile);
      } catch (e) {
        Logger.info(`modifyFileToWatcher -readyFileRW-creat has failed for: {message: ${e.message}, code: ${e.code}}`);
      }
    }

    // 修改文件內(nèi)容
    Logger.info('modifyFileContentToWatcher filePath = ' + filePath);
    try {
      /* 修改文件內(nèi)容時(shí)必須以fs.OpenMode.TRUNC打開(kāi)文件,如果文件存在且以只寫(xiě)或讀寫(xiě)的方式打開(kāi)文件,則將其長(zhǎng)度裁剪為零。這樣修改文件內(nèi)容時(shí),若修改后的文件        * 比修改前小,也會(huì)正確修改,否則修改后文件的文件比未修改前小時(shí),則修改錯(cuò)誤。
       */
      let file = fs.openSync(filePath, fs.OpenMode.READ_WRITE | fs.OpenMode.CREATE | fs.OpenMode.TRUNC);
      let writeLen = fs.writeSync(file.fd, content);
      Logger.info('modifyFileContentToWatcher write data to file succeed and size is:' + writeLen);
      fs.closeSync(file);

      let fileRead = fs.openSync(filePath, fs.OpenMode.READ_WRITE);
      let buf = new ArrayBuffer(BUFFER);
      this.fileSize = fs.readSync(fileRead.fd, buf);
      let resultPut = bufferToString(buf);
      Logger.info('modifyFileContentToWatcher Read num = ' + this.fileSize);
      Logger.info('modifyFileContentToWatcher Read resultPut = ' + resultPut);
      fs.closeSync(fileRead);
      Logger.info('modifyFileContentToWatcher write data to file succeed and fileSize is:' + this.fileSize);
    } catch (e) {
      Logger.info(`-readyFileRW-creat has failed for: {message: ${e.message}, code: ${e.code}}`);
    }
    return this.fileSize;
  }
}

測(cè)試

sample功能完成之后,需要對(duì)開(kāi)發(fā)完成的功能進(jìn)行測(cè)試,此時(shí)就需要增加自動(dòng)化測(cè)試用例實(shí)現(xiàn)對(duì)功能的基本測(cè)試。UiTest可以通過(guò)簡(jiǎn)潔易用的API提供查找和操作界面控件能力,支持用戶(hù)開(kāi)發(fā)基于界面操作的自動(dòng)化測(cè)試腳本。如何編寫(xiě)UI測(cè)試用例參考編寫(xiě)UI測(cè)試腳本,測(cè)試執(zhí)行完畢后可直接在DevEco Studio中查看測(cè)試結(jié)果,如下所示:

總結(jié)

通過(guò)使用這個(gè)示例,開(kāi)發(fā)者可以快速掌握文件監(jiān)聽(tīng)的實(shí)現(xiàn)方法,并將其運(yùn)用到自己的應(yīng)用中,實(shí)現(xiàn)更強(qiáng)大和豐富的功能。同時(shí),這個(gè)示例也提供了一個(gè)學(xué)習(xí)和交流的平臺(tái),開(kāi)發(fā)者可以與其他開(kāi)發(fā)者分享經(jīng)驗(yàn)和技巧,共同推動(dòng)OpenHarmony生態(tài)的發(fā)展。

想了解更多關(guān)于開(kāi)源的內(nèi)容,請(qǐng)?jiān)L問(wèn):

51CTO 開(kāi)源基礎(chǔ)軟件社區(qū)

https://ost.51cto.com

責(zé)任編輯:jianghua 來(lái)源: 51CTO 開(kāi)源基礎(chǔ)軟件社區(qū)
相關(guān)推薦

2023-06-09 15:24:50

UiTest接口鴻蒙

2023-06-20 15:40:58

系統(tǒng)開(kāi)發(fā)鴻蒙

2021-06-24 14:41:16

鴻蒙HarmonyOS應(yīng)用

2021-12-22 10:19:47

鴻蒙HarmonyOS應(yīng)用

2024-03-26 15:25:55

應(yīng)用接口文件轉(zhuǎn)換工具鴻蒙

2023-07-17 16:13:21

組件模塊開(kāi)發(fā)的鴻蒙

2010-06-09 13:24:22

UML用例

2023-06-20 15:45:15

服務(wù)卡片鴻蒙

2023-04-26 15:29:35

NAPI模塊鴻蒙

2023-04-21 15:54:08

應(yīng)用開(kāi)發(fā)鴻蒙

2022-10-12 15:00:02

設(shè)備開(kāi)發(fā)應(yīng)用開(kāi)發(fā)

2022-03-02 16:08:31

Harmony應(yīng)用開(kāi)發(fā)鴻蒙

2024-04-09 09:34:36

鴻蒙系統(tǒng)燒錄操作系統(tǒng)

2021-05-06 05:39:30

Inotify監(jiān)聽(tīng)系統(tǒng)

2022-02-15 14:45:14

OpenHarmo系統(tǒng)鴻蒙

2022-05-10 11:02:02

電話子系統(tǒng)鴻蒙

2022-01-21 21:22:24

OpenHarmon操作系統(tǒng)鴻蒙

2023-05-30 14:50:20

界面開(kāi)發(fā)鴻蒙

2024-01-18 15:24:06

Rust開(kāi)發(fā)鴻蒙OH4.0

2024-05-14 08:33:57

Native API鴻蒙工具
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)