1/* 2 * Copyright (c) 2022 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16import rpc from "@ohos.rpc"; 17import fileio from '@ohos.fileio'; 18 19export default { 20 onStart() { 21 console.info('FileioServer: onStart') 22 }, 23 onStop() { 24 console.info('FileioServer: onStop') 25 }, 26 onCommand(want, startId) { 27 console.info('FileioServer: onCommand, want: ' + JSON.stringify(want) + ', startId: ' + startId) 28 }, 29 onConnect(want) { 30 console.info('FileioServer: service onConnect called.') 31 return new Stub("rpcTestAbility") 32 }, 33 onDisconnect(want) { 34 console.info('FileioServer: service onDisConnect called.') 35 }, 36 onReconnect(want) { 37 console.info('FileioServer: service onReConnect called.') 38 } 39} 40 41function checkDirExists(destDirPath) { 42 console.info("start check dir :" + destDirPath); 43 try { 44 fileio.accessSync(destDirPath, 0); 45 console.info('------------------- dest dir exists.'); 46 return true; 47 } catch (e) { 48 console.info('------------------- dest dir is not exists.'); 49 return false; 50 } 51} 52 53function checkFileExists(destFilePath,) { 54 console.info("start check file :" + destFilePath); 55 try { 56 fileio.accessSync(destFilePath, 0); 57 console.info('------------------- ' + destFilePath + ' exists.'); 58 return true; 59 } catch (e) { 60 console.info('------------------- ' + destFilePath + ' not exists.'); 61 return false; 62 } 63} 64 65function getFileContent(fpath) { 66 console.info("start get file content:" + fpath); 67 let content = ""; 68 try { 69 content = fileio.readTextSync(fpath); 70 console.info("-------------- dest file content :" + content); 71 } catch (e) { 72 content = "serverSide readTextSync failed"; 73 console.info("-------------- read dest file content failed." + e); 74 } 75 return content; 76} 77 78const CODE_MK_DIR = 1; 79const CODE_RM_DIR = 2; 80const CODE_CREATE_FILE = 3; 81const CODE_DELETE_FILE = 4; 82const CODE_GET_FILE_CONTENT = 5; 83const CODE_GET_FILE_STAT = 6; 84const CODE_FSYNC_FILE = 7; 85 86class Stub extends rpc.RemoteObject { 87 constructor(descriptor) { 88 super(descriptor); 89 } 90 91 onRemoteRequest(code, data, reply, option) { 92 try { 93 console.info("onRemoteRequest: " + code) 94 switch (code) { 95 case CODE_MK_DIR: 96 { 97 console.info("case CODE_MK_DIR start") 98 let path = data.readString() 99 console.info("The server's readString result is " + path); 100 let checkResult = checkDirExists(path); 101 let result; 102 if (checkResult == true) { 103 result = reply.writeString("SUCCESS"); 104 } else { 105 result = reply.writeString("Server side dir synchronization creation failed."); 106 } 107 108 console.info("The server's writeString result is " + result); 109 return true; 110 } 111 case CODE_RM_DIR: 112 { 113 console.info("case CODE_RM_DIR start"); 114 let path = data.readString(); 115 console.info("The server's readString result is " + path); 116 let result; 117 try { 118 let dir = fileio.openDirSync(path); 119 result = reply.writeString("Server side dir synchronization creation failed!"); 120 dir.closeSync(); 121 } catch (error) { 122 result = reply.writeString("SUCCESS"); 123 } 124 125 console.info("The server's writeString result is " + result); 126 return true; 127 } 128 case CODE_CREATE_FILE: 129 { 130 console.info("case CODE_CREATE_FILE start"); 131 let path = data.readString(); 132 console.info("The server's readString result is " + path); 133 134 let checkResult = checkFileExists(path); 135 let result; 136 if (checkResult == true) { 137 result = reply.writeString("SUCCESS"); 138 } else { 139 result = reply.writeString("Server side file synchronization creation failed!"); 140 } 141 142 console.info("The server's writeString result is " + result); 143 return true; 144 } 145 case CODE_DELETE_FILE: 146 { 147 console.info("case CODE_DELETE_FILE start"); 148 let path = data.readString(); 149 console.info("The server's readString result is " + path); 150 let result; 151 try { 152 let fd = fileio.openSync(path); 153 result = reply.writeString("Server side file synchronization creation failed!"); 154 fileio.closeSync(fd); 155 } catch (error) { 156 result = reply.writeString("SUCCESS"); 157 } 158 159 console.info("The server's writeString result is " + result); 160 return true; 161 } 162 case CODE_GET_FILE_CONTENT: 163 { 164 console.info("case CODE_GET_FILE_CONTENT start"); 165 let path = data.readString(); 166 console.info("The server's readString result is " + path); 167 checkFileExists(path); 168 let content =""; 169 try { 170 console.info("---------------- start get file content:" + path); 171 content = getFileContent(path); 172 console.info("-------------- dest file content :" + content); 173 } catch (e) { 174 console.info("-------------- read dest file content failed." + e); 175 } 176 177 let result = reply.writeString(content); 178 console.info("The server's writeString result is " + result); 179 return true; 180 } 181 case CODE_GET_FILE_STAT: 182 { 183 console.info("case CODE_GET_FILE_STAT start"); 184 let path = data.readString(); 185 console.info("The server's readString result is " + path); 186 let localStat = fileio.statSync(path); 187 let localStatInfo = "localStat.size = " + localStat.size + ",localStat.mode = " + localStat.mode; 188 let result = reply.writeString(localStatInfo); 189 console.info("The server's writeString result is " + result); 190 return true; 191 } 192 case CODE_FSYNC_FILE: 193 { 194 console.info("case CODE_FSYNC_FILE start"); 195 let path = data.readString(); 196 console.info("The server's readString result is " + path); 197 let result; 198 try{ 199 let fd = fileio.openSync(path, 0o2); 200 fileio.fsyncSync(fd); 201 console.info("sync data succeed"); 202 result = reply.writeString("SUCCESS"); 203 } catch (e) { 204 console.info("sync data failed with error:" + e); 205 result = reply.writeString("FAILED"); 206 } 207 console.info("The server's writeString result is " + result); 208 return true; 209 } 210 default: 211 console.error("default case " + code); 212 return super.onRemoteMessageRequest(code, data, reply, option); 213 } 214 } catch (error) { 215 console.info("onRemoteMessageRequest: " + error); 216 } 217 return false 218 } 219}