1 /* 2 * Copyright (c) 2022-2024 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 16 #include "file_access_ext_stub.h" 17 18 #include <cstdint> 19 #include <map> 20 #include <memory> 21 #include <string> 22 #include <utility> 23 #include <vector> 24 #include "user_access_tracer.h" 25 #include "access_token.h" 26 #include "accesstoken_kit.h" 27 #include "file_access_extension_info.h" 28 #include "file_access_framework_errno.h" 29 #include "hilog_wrapper.h" 30 #include "hitrace_meter.h" 31 #include "ipc_object_stub.h" 32 #include "ipc_skeleton.h" 33 #include "ipc_types.h" 34 #include "unique_fd.h" 35 #include "uri.h" 36 37 namespace OHOS { 38 namespace FileAccessFwk { 39 namespace { 40 const std::string FILE_ACCESS_PERMISSION = "ohos.permission.FILE_ACCESS_MANAGER"; 41 } FileAccessExtStub()42 FileAccessExtStub::FileAccessExtStub() 43 { 44 stubFuncMap_[CMD_OPEN_FILE] = &FileAccessExtStub::CmdOpenFile; 45 stubFuncMap_[CMD_CREATE_FILE] = &FileAccessExtStub::CmdCreateFile; 46 stubFuncMap_[CMD_START_WATCHER] = &FileAccessExtStub::CmdStartWatcher; 47 stubFuncMap_[CMD_STOP_WATCHER] = &FileAccessExtStub::CmdStopWatcher; 48 stubFuncMap_[CMD_MKDIR] = &FileAccessExtStub::CmdMkdir; 49 stubFuncMap_[CMD_DELETE] = &FileAccessExtStub::CmdDelete; 50 stubFuncMap_[CMD_MOVE] = &FileAccessExtStub::CmdMove; 51 stubFuncMap_[CMD_COPY] = &FileAccessExtStub::CmdCopy; 52 stubFuncMap_[CMD_RENAME] = &FileAccessExtStub::CmdRename; 53 stubFuncMap_[CMD_LIST_FILE] = &FileAccessExtStub::CmdListFile; 54 stubFuncMap_[CMD_SCAN_FILE] = &FileAccessExtStub::CmdScanFile; 55 stubFuncMap_[CMD_QUERY] = &FileAccessExtStub::CmdQuery; 56 stubFuncMap_[CMD_GET_ROOTS] = &FileAccessExtStub::CmdGetRoots; 57 stubFuncMap_[CMD_ACCESS] = &FileAccessExtStub::CmdAccess; 58 stubFuncMap_[CMD_GET_FILEINFO_FROM_URI] = &FileAccessExtStub::CmdGetFileInfoFromUri; 59 stubFuncMap_[CMD_GET_FILEINFO_FROM_RELATIVE_PATH] = &FileAccessExtStub::CmdGetFileInfoFromRelativePath; 60 stubFuncMap_[CMD_COPY_FILE] = &FileAccessExtStub::CmdCopyFile; 61 stubFuncMap_[CMD_MOVE_ITEM] = &FileAccessExtStub::CmdMoveItem; 62 stubFuncMap_[CMD_MOVE_FILE] = &FileAccessExtStub::CmdMoveFile; 63 } 64 ~FileAccessExtStub()65 FileAccessExtStub::~FileAccessExtStub() 66 { 67 stubFuncMap_.clear(); 68 } 69 OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)70 int FileAccessExtStub::OnRemoteRequest(uint32_t code, MessageParcel& data, MessageParcel& reply, 71 MessageOption& option) 72 { 73 UserAccessTracer trace; 74 trace.Start("OnRemoteRequest"); 75 std::u16string descriptor = FileAccessExtStub::GetDescriptor(); 76 std::u16string remoteDescriptor = data.ReadInterfaceToken(); 77 if (descriptor != remoteDescriptor) { 78 return ERR_INVALID_STATE; 79 } 80 81 if (!CheckCallingPermission(FILE_ACCESS_PERMISSION)) { 82 HILOG_ERROR("permission error"); 83 return E_PERMISSION; 84 } 85 86 const auto &itFunc = stubFuncMap_.find(code); 87 if (itFunc != stubFuncMap_.end()) { 88 return (this->*(itFunc->second))(data, reply); 89 } 90 return IPCObjectStub::OnRemoteRequest(code, data, reply, option); 91 } 92 CmdOpenFile(MessageParcel & data,MessageParcel & reply)93 ErrCode FileAccessExtStub::CmdOpenFile(MessageParcel &data, MessageParcel &reply) 94 { 95 UserAccessTracer trace; 96 trace.Start("CmdOpenFile"); 97 std::shared_ptr<Uri> uri(data.ReadParcelable<Uri>()); 98 if (uri == nullptr) { 99 HILOG_ERROR("Parameter OpenFile fail to ReadParcelable uri"); 100 return E_IPCS; 101 } 102 103 int flags = E_IPCS; 104 if (!data.ReadInt32(flags)) { 105 HILOG_ERROR("Parameter OpenFile fail to ReadInt32 flags"); 106 return E_IPCS; 107 } 108 109 if (flags < 0) { 110 HILOG_ERROR("Parameter OpenFile flags is invalid"); 111 return EINVAL; 112 } 113 114 int fd = -1; 115 int ret = OpenFile(*uri, flags, fd); 116 UniqueFd uniqueFd(fd); 117 if (!reply.WriteInt32(ret)) { 118 HILOG_ERROR("Parameter OpenFile fail to WriteInt32 ret"); 119 return E_IPCS; 120 } 121 122 if (!reply.WriteFileDescriptor(fd)) { 123 HILOG_ERROR("Parameter OpenFile fail to WriteFileDescriptor fd"); 124 return E_IPCS; 125 } 126 127 return ERR_OK; 128 } 129 CmdCreateFile(MessageParcel & data,MessageParcel & reply)130 ErrCode FileAccessExtStub::CmdCreateFile(MessageParcel &data, MessageParcel &reply) 131 { 132 UserAccessTracer trace; 133 trace.Start("CmdCreateFile"); 134 std::shared_ptr<Uri> parent(data.ReadParcelable<Uri>()); 135 if (parent == nullptr) { 136 HILOG_ERROR("Parameter CreateFile fail to ReadParcelable parent"); 137 return E_IPCS; 138 } 139 140 std::string displayName = ""; 141 if (!data.ReadString(displayName)) { 142 HILOG_ERROR("Parameter CreateFile fail to ReadString displayName"); 143 return E_IPCS; 144 } 145 146 if (displayName.empty()) { 147 HILOG_ERROR("Parameter CreateFile displayName is empty"); 148 return EINVAL; 149 } 150 151 std::string newFile = ""; 152 OHOS::Uri newFileUri(newFile); 153 int ret = CreateFile(*parent, displayName, newFileUri); 154 if (!reply.WriteInt32(ret)) { 155 HILOG_ERROR("Parameter CreateFile fail to WriteInt32 ret"); 156 return E_IPCS; 157 } 158 159 if (!reply.WriteParcelable(&newFileUri)) { 160 HILOG_ERROR("Parameter CreateFile fail to WriteParcelable newFileUri"); 161 return E_IPCS; 162 } 163 164 return ERR_OK; 165 } 166 CmdMkdir(MessageParcel & data,MessageParcel & reply)167 ErrCode FileAccessExtStub::CmdMkdir(MessageParcel &data, MessageParcel &reply) 168 { 169 UserAccessTracer trace; 170 trace.Start("CmdMkdir"); 171 std::shared_ptr<Uri> parent(data.ReadParcelable<Uri>()); 172 if (parent == nullptr) { 173 HILOG_ERROR("Parameter Mkdir fail to ReadParcelable parent"); 174 return E_IPCS; 175 } 176 177 std::string displayName = ""; 178 if (!data.ReadString(displayName)) { 179 HILOG_ERROR("Parameter Mkdir fail to ReadString displayName"); 180 return E_IPCS; 181 } 182 183 if (displayName.empty()) { 184 HILOG_ERROR("Parameter Mkdir displayName is empty"); 185 return EINVAL; 186 } 187 188 std::string newFile = ""; 189 OHOS::Uri newFileUri(newFile); 190 int ret = Mkdir(*parent, displayName, newFileUri); 191 if (!reply.WriteInt32(ret)) { 192 HILOG_ERROR("Parameter Mkdir fail to WriteInt32 ret"); 193 return E_IPCS; 194 } 195 196 if (!reply.WriteParcelable(&newFileUri)) { 197 HILOG_ERROR("Parameter Mkdir fail to WriteParcelable newFileUri"); 198 return E_IPCS; 199 } 200 201 return ERR_OK; 202 } 203 CmdDelete(MessageParcel & data,MessageParcel & reply)204 ErrCode FileAccessExtStub::CmdDelete(MessageParcel &data, MessageParcel &reply) 205 { 206 UserAccessTracer trace; 207 trace.Start("CmdDelete"); 208 std::shared_ptr<Uri> uri(data.ReadParcelable<Uri>()); 209 if (uri == nullptr) { 210 HILOG_ERROR("Parameter Delete fail to ReadParcelable uri"); 211 return E_IPCS; 212 } 213 214 int ret = Delete(*uri); 215 if (!reply.WriteInt32(ret)) { 216 HILOG_ERROR("Parameter Delete fail to WriteInt32 ret"); 217 return E_IPCS; 218 } 219 220 return ERR_OK; 221 } 222 CmdMove(MessageParcel & data,MessageParcel & reply)223 ErrCode FileAccessExtStub::CmdMove(MessageParcel &data, MessageParcel &reply) 224 { 225 UserAccessTracer trace; 226 trace.Start("CmdMove"); 227 std::shared_ptr<Uri> sourceFile(data.ReadParcelable<Uri>()); 228 if (sourceFile == nullptr) { 229 HILOG_ERROR("Parameter Move fail to ReadParcelable sourceFile"); 230 return E_IPCS; 231 } 232 233 std::shared_ptr<Uri> targetParent(data.ReadParcelable<Uri>()); 234 if (targetParent == nullptr) { 235 HILOG_ERROR("Parameter Move fail to ReadParcelable targetParent"); 236 return E_IPCS; 237 } 238 239 std::string newFile = ""; 240 OHOS::Uri newFileUri(newFile); 241 int ret = Move(*sourceFile, *targetParent, newFileUri); 242 if (!reply.WriteInt32(ret)) { 243 HILOG_ERROR("Parameter Move fail to WriteInt32 ret"); 244 return E_IPCS; 245 } 246 247 if (!reply.WriteParcelable(&newFileUri)) { 248 HILOG_ERROR("Parameter Move fail to WriteParcelable newFileUri"); 249 return E_IPCS; 250 } 251 252 return ERR_OK; 253 } 254 CmdCopy(MessageParcel & data,MessageParcel & reply)255 ErrCode FileAccessExtStub::CmdCopy(MessageParcel &data, MessageParcel &reply) 256 { 257 UserAccessTracer trace; 258 trace.Start("CmdCopy"); 259 std::shared_ptr<Uri> sourceUri(data.ReadParcelable<Uri>()); 260 if (sourceUri == nullptr) { 261 HILOG_ERROR("Parameter Copy fail to ReadParcelable sourceUri"); 262 return E_IPCS; 263 } 264 265 std::shared_ptr<Uri> destUri(data.ReadParcelable<Uri>()); 266 if (destUri == nullptr) { 267 HILOG_ERROR("Parameter Copy fail to ReadParcelable destUri"); 268 return E_IPCS; 269 } 270 271 bool force = false; 272 if (!data.ReadBool(force)) { 273 HILOG_ERROR("Parameter Copy fail to ReadBool force"); 274 return E_IPCS; 275 } 276 277 std::vector<Result> copyResult; 278 int ret = Copy(*sourceUri, *destUri, copyResult, force); 279 if (!reply.WriteInt32(ret)) { 280 HILOG_ERROR("Parameter Copy fail to WriteInt32 ret"); 281 return E_IPCS; 282 } 283 284 if (!reply.WriteUint32(copyResult.size())) { 285 HILOG_ERROR("Parameter Copy fail to WriteInt32 size of copyResult"); 286 return E_IPCS; 287 } 288 289 for (auto &result : copyResult) { 290 if (!reply.WriteParcelable(&result)) { 291 HILOG_ERROR("Parameter Copy fail to WriteParcelable copyResult"); 292 return E_IPCS; 293 } 294 } 295 296 return ERR_OK; 297 } 298 CmdCopyFile(MessageParcel & data,MessageParcel & reply)299 ErrCode FileAccessExtStub::CmdCopyFile(MessageParcel &data, MessageParcel &reply) 300 { 301 UserAccessTracer trace; 302 trace.Start("CmdCopyFile"); 303 std::string sourceUri = ""; 304 if (!data.ReadString(sourceUri)) { 305 HILOG_ERROR("Parameter Copy file fail to ReadParcelable sourceUri"); 306 return E_IPCS; 307 } 308 309 std::string destUri = ""; 310 if (!data.ReadString(destUri)) { 311 HILOG_ERROR("Parameter Copy file fail to ReadParcelable destUri"); 312 return E_IPCS; 313 } 314 315 std::string fileName = ""; 316 if (!data.ReadString(fileName)) { 317 HILOG_ERROR("Parameter Copy file fail to ReadString fileName"); 318 return E_IPCS; 319 } 320 321 OHOS::Uri newFileUri(""); 322 Uri source(sourceUri); 323 Uri dest(destUri); 324 int ret = CopyFile(source, dest, fileName, newFileUri); 325 if (!reply.WriteInt32(ret)) { 326 HILOG_ERROR("Parameter Copy file fail to WriteInt32 ret"); 327 return E_IPCS; 328 } 329 if (!reply.WriteString(newFileUri.ToString())) { 330 HILOG_ERROR("Parameter Copy file fail to WriteString newFileUri"); 331 return E_IPCS; 332 } 333 334 return ERR_OK; 335 } 336 CmdRename(MessageParcel & data,MessageParcel & reply)337 ErrCode FileAccessExtStub::CmdRename(MessageParcel &data, MessageParcel &reply) 338 { 339 UserAccessTracer trace; 340 trace.Start("CmdRename"); 341 std::shared_ptr<Uri> sourceFile(data.ReadParcelable<Uri>()); 342 if (sourceFile == nullptr) { 343 HILOG_ERROR("Parameter Rename fail to ReadParcelable sourceFile"); 344 return E_IPCS; 345 } 346 347 std::string displayName = ""; 348 if (!data.ReadString(displayName)) { 349 HILOG_ERROR("Parameter Rename fail to ReadString displayName"); 350 return E_IPCS; 351 } 352 353 if (displayName.empty()) { 354 HILOG_ERROR("Parameter Rename displayName is empty"); 355 return EINVAL; 356 } 357 358 std::string newFile = ""; 359 OHOS::Uri newFileUri(newFile); 360 int ret = Rename(*sourceFile, displayName, newFileUri); 361 if (!reply.WriteInt32(ret)) { 362 HILOG_ERROR("Parameter Rename fail to WriteInt32 ret"); 363 return E_IPCS; 364 } 365 366 if (!reply.WriteParcelable(&newFileUri)) { 367 HILOG_ERROR("Parameter Rename fail to WriteParcelable newFileUri"); 368 return E_IPCS; 369 } 370 371 return ERR_OK; 372 } 373 374 std::tuple<int, std::shared_ptr<FileInfo>, int64_t, std::shared_ptr<FileFilter>, std::shared_ptr<SharedMemoryInfo>> ReadFileFilterFuncArguments(MessageParcel & data)375 ReadFileFilterFuncArguments(MessageParcel &data) 376 { 377 std::shared_ptr<FileInfo> fileInfo(data.ReadParcelable<FileInfo>()); 378 if (fileInfo == nullptr) { 379 HILOG_ERROR("Parameter ListFile fail to ReadParcelable fileInfo"); 380 return std::make_tuple(E_IPCS, nullptr, 0, nullptr, nullptr); 381 } 382 383 int64_t offset = 0; 384 if (!data.ReadInt64(offset)) { 385 HILOG_ERROR("parameter ListFile offset is invalid"); 386 return std::make_tuple(E_IPCS, nullptr, 0, nullptr, nullptr); 387 } 388 389 std::shared_ptr<FileFilter> filter(data.ReadParcelable<FileFilter>()); 390 if (filter == nullptr) { 391 HILOG_ERROR("parameter ListFile FileFilter is invalid"); 392 return std::make_tuple(E_IPCS, nullptr, 0, nullptr, nullptr); 393 } 394 395 std::shared_ptr<SharedMemoryInfo> memInfo(data.ReadParcelable<SharedMemoryInfo>()); 396 if (memInfo == nullptr) { 397 HILOG_ERROR("parameter ListFile SharedMemoryInfo is invalid"); 398 return std::make_tuple(E_IPCS, nullptr, 0, nullptr, nullptr); 399 } 400 401 return std::make_tuple(ERR_OK, fileInfo, offset, filter, memInfo); 402 } 403 WriteFileFilterResults(MessageParcel & reply,SharedMemoryInfo & memInfo)404 int WriteFileFilterResults(MessageParcel &reply, SharedMemoryInfo &memInfo) 405 { 406 if (!reply.WriteUint32(memInfo.dataCounts)) { 407 HILOG_ERROR("fail to WriteUint32 dataCounts"); 408 return E_IPCS; 409 } 410 411 if (!reply.WriteUint64(memInfo.dataSize)) { 412 HILOG_ERROR("fail to WriteUint32 dataSize"); 413 return E_IPCS; 414 } 415 416 if (!reply.WriteUint32(memInfo.leftDataCounts)) { 417 HILOG_ERROR("fail to WriteUint32 leftDataCounts"); 418 return E_IPCS; 419 } 420 421 if (!reply.WriteBool(memInfo.isOver)) { 422 HILOG_ERROR("fail to WriteBool isOver"); 423 return E_IPCS; 424 } 425 426 return ERR_OK; 427 } 428 CmdListFile(MessageParcel & data,MessageParcel & reply)429 ErrCode FileAccessExtStub::CmdListFile(MessageParcel &data, MessageParcel &reply) 430 { 431 UserAccessTracer trace; 432 trace.Start("CmdListFile"); 433 int ret = E_IPCS; 434 std::shared_ptr<FileInfo> fileInfo = nullptr; 435 int64_t offset = 0; 436 std::shared_ptr<FileFilter> filter = nullptr; 437 std::shared_ptr<SharedMemoryInfo> memInfo = nullptr; 438 439 std::tie(ret, fileInfo, offset, filter, memInfo) = ReadFileFilterFuncArguments(data); 440 if (ret != ERR_OK) { 441 return ret; 442 } 443 ret = SharedMemoryOperation::MapSharedMemory(*memInfo); 444 if (ret != ERR_OK) { 445 return ret; 446 } 447 448 ret = ListFile(*fileInfo, offset, *filter, *memInfo); 449 if (!reply.WriteInt32(ret)) { 450 HILOG_ERROR("ListFile fail to WriteInt32 ret"); 451 SharedMemoryOperation::DestroySharedMemory(*memInfo); 452 return E_IPCS; 453 } 454 455 ret = WriteFileFilterResults(reply, *memInfo); 456 SharedMemoryOperation::DestroySharedMemory(*memInfo); 457 return ret; 458 } 459 CmdScanFile(MessageParcel & data,MessageParcel & reply)460 ErrCode FileAccessExtStub::CmdScanFile(MessageParcel &data, MessageParcel &reply) 461 { 462 UserAccessTracer trace; 463 trace.Start("CmdScanFile"); 464 std::shared_ptr<FileInfo> fileInfo(data.ReadParcelable<FileInfo>()); 465 if (fileInfo == nullptr) { 466 HILOG_ERROR("Parameter ScanFile fail to ReadParcelable fileInfo"); 467 return E_IPCS; 468 } 469 470 int64_t offset = 0; 471 if (!data.ReadInt64(offset)) { 472 HILOG_ERROR("parameter ScanFile offset is invalid"); 473 return E_IPCS; 474 } 475 476 int64_t maxCount = 0; 477 if (!data.ReadInt64(maxCount)) { 478 HILOG_ERROR("parameter ScanFile maxCount is invalid"); 479 return E_IPCS; 480 } 481 482 std::shared_ptr<FileFilter> filter(data.ReadParcelable<FileFilter>()); 483 if (filter == nullptr) { 484 HILOG_ERROR("parameter ScanFile FileFilter is invalid"); 485 return E_IPCS; 486 } 487 488 std::vector<FileInfo> fileInfoVec; 489 int ret = ScanFile(*fileInfo, offset, maxCount, *filter, fileInfoVec); 490 if (!reply.WriteInt32(ret)) { 491 HILOG_ERROR("Parameter ScanFile fail to WriteInt32 ret"); 492 return E_IPCS; 493 } 494 495 int64_t count {fileInfoVec.size()}; 496 if (!reply.WriteInt64(count)) { 497 HILOG_ERROR("Parameter ScanFile fail to WriteInt64 count"); 498 return E_IPCS; 499 } 500 501 for (const auto &fileInfo : fileInfoVec) { 502 if (!reply.WriteParcelable(&fileInfo)) { 503 HILOG_ERROR("parameter ScanFile fail to WriteParcelable fileInfoVec"); 504 return E_IPCS; 505 } 506 } 507 508 return ERR_OK; 509 } 510 CmdGetRoots(MessageParcel & data,MessageParcel & reply)511 ErrCode FileAccessExtStub::CmdGetRoots(MessageParcel &data, MessageParcel &reply) 512 { 513 UserAccessTracer trace; 514 trace.Start("CmdGetRoots"); 515 516 std::vector<RootInfo> rootInfoVec; 517 int ret = GetRoots(rootInfoVec); 518 if (!reply.WriteInt32(ret)) { 519 HILOG_ERROR("Parameter GetRoots fail to WriteInt32 ret"); 520 return E_IPCS; 521 } 522 523 int64_t count {rootInfoVec.size()}; 524 if (!reply.WriteInt64(count)) { 525 HILOG_ERROR("Parameter GetRoots fail to WriteInt64 count"); 526 return E_IPCS; 527 } 528 529 for (const auto &rootInfo : rootInfoVec) { 530 if (!reply.WriteParcelable(&rootInfo)) { 531 HILOG_ERROR("parameter ListFile fail to WriteParcelable rootInfo"); 532 return E_IPCS; 533 } 534 } 535 536 return ERR_OK; 537 } 538 CmdQuery(MessageParcel & data,MessageParcel & reply)539 ErrCode FileAccessExtStub::CmdQuery(MessageParcel &data, MessageParcel &reply) 540 { 541 UserAccessTracer trace; 542 trace.Start("CmdQuery"); 543 std::shared_ptr<Uri> uri(data.ReadParcelable<Uri>()); 544 if (uri == nullptr) { 545 HILOG_ERROR("Parameter Query fail to ReadParcelable uri"); 546 return E_IPCS; 547 } 548 549 int64_t count = 0; 550 if (!data.ReadInt64(count)) { 551 HILOG_ERROR("Query operation failed to Read count"); 552 return E_IPCS; 553 } 554 if (count > static_cast<int64_t>(FILE_RESULT_TYPE.size())) { 555 HILOG_ERROR(" The number of query operations exceeds %{public}zu ", FILE_RESULT_TYPE.size()); 556 return EINVAL; 557 } 558 std::vector<std::string> columns; 559 for (int64_t i = 0; i < count; i++) { 560 columns.push_back(data.ReadString()); 561 } 562 std::vector<std::string> results; 563 int ret = Query(*uri, columns, results); 564 if (!reply.WriteInt32(ret)) { 565 HILOG_ERROR("Parameter Query fail to WriteInt32 ret"); 566 return E_IPCS; 567 } 568 569 int64_t resCount {results.size()}; 570 if (!reply.WriteInt64(resCount)) { 571 HILOG_ERROR("Parameter Query fail to WriteInt64 count"); 572 return E_IPCS; 573 } 574 575 for (const auto &result : results) { 576 if (!reply.WriteString(result)) { 577 HILOG_ERROR("parameter Query fail to WriteParcelable column"); 578 return E_IPCS; 579 } 580 } 581 582 return ERR_OK; 583 } 584 CmdGetFileInfoFromUri(MessageParcel & data,MessageParcel & reply)585 ErrCode FileAccessExtStub::CmdGetFileInfoFromUri(MessageParcel &data, MessageParcel &reply) 586 { 587 UserAccessTracer trace; 588 trace.Start("CmdGetFileInfoFromUri"); 589 std::shared_ptr<Uri> uri(data.ReadParcelable<Uri>()); 590 if (uri == nullptr) { 591 HILOG_ERROR("SelectFile uri is nullptr"); 592 return E_URIS; 593 } 594 595 FileInfo fileInfoTemp; 596 int ret = GetFileInfoFromUri(*uri, fileInfoTemp); 597 if (!reply.WriteInt32(ret)) { 598 HILOG_ERROR("Parameter GetFileInfoFromUri fail to WriteInt32 ret"); 599 return E_IPCS; 600 } 601 602 if (!reply.WriteParcelable(&fileInfoTemp)) { 603 HILOG_ERROR("Parameter GetFileInfoFromUri fail to WriteParcelable fileInfoTemp"); 604 return E_IPCS; 605 } 606 607 return ERR_OK; 608 } 609 CmdGetFileInfoFromRelativePath(MessageParcel & data,MessageParcel & reply)610 ErrCode FileAccessExtStub::CmdGetFileInfoFromRelativePath(MessageParcel &data, MessageParcel &reply) 611 { 612 UserAccessTracer trace; 613 trace.Start("CmdGetFileInfoFromRelativePath"); 614 std::string relativePath(data.ReadString()); 615 616 FileInfo fileInfoTemp; 617 int ret = GetFileInfoFromRelativePath(relativePath, fileInfoTemp); 618 if (!reply.WriteInt32(ret)) { 619 HILOG_ERROR("Parameter CmdGetFileInfoFromRelativePath fail to WriteInt32 ret"); 620 return E_IPCS; 621 } 622 623 if (!reply.WriteParcelable(&fileInfoTemp)) { 624 HILOG_ERROR("Parameter CmdGetFileInfoFromRelativePath fail to WriteParcelable fileInfoTemp"); 625 return E_IPCS; 626 } 627 628 return ERR_OK; 629 } 630 CmdAccess(MessageParcel & data,MessageParcel & reply)631 ErrCode FileAccessExtStub::CmdAccess(MessageParcel &data, MessageParcel &reply) 632 { 633 UserAccessTracer trace; 634 trace.Start("CmdAccess"); 635 std::shared_ptr<Uri> uri(data.ReadParcelable<Uri>()); 636 if (uri == nullptr) { 637 HILOG_ERROR("Access uri is nullptr"); 638 return E_URIS; 639 } 640 641 bool isExist = false; 642 int ret = Access(*uri, isExist); 643 if (!reply.WriteInt32(ret)) { 644 HILOG_ERROR("Parameter Access fail to WriteInt32 ret"); 645 return E_IPCS; 646 } 647 648 if (!reply.WriteBool(isExist)) { 649 HILOG_ERROR("Parameter Access fail to WriteBool isExist"); 650 return E_IPCS; 651 } 652 653 return ERR_OK; 654 } 655 CmdStartWatcher(MessageParcel & data,MessageParcel & reply)656 ErrCode FileAccessExtStub::CmdStartWatcher(MessageParcel &data, MessageParcel &reply) 657 { 658 UserAccessTracer trace; 659 trace.Start("CmdStartWatcher"); 660 std::string uriString; 661 if (!data.ReadString(uriString)) { 662 HILOG_ERROR("Parameter StartWatcher fail to ReadParcelable uri"); 663 return E_IPCS; 664 } 665 666 if (uriString.empty()) { 667 HILOG_ERROR("Parameter StartWatcher insideInputUri is empty"); 668 return EINVAL; 669 } 670 671 Uri uri(uriString); 672 int ret = StartWatcher(uri); 673 if (!reply.WriteInt32(ret)) { 674 HILOG_ERROR("Parameter StartWatcher fail to WriteInt32 ret"); 675 return E_IPCS; 676 } 677 678 return ERR_OK; 679 } 680 CmdStopWatcher(MessageParcel & data,MessageParcel & reply)681 ErrCode FileAccessExtStub::CmdStopWatcher(MessageParcel &data, MessageParcel &reply) 682 { 683 UserAccessTracer trace; 684 trace.Start("CmdStopWatcher"); 685 std::string uriString; 686 if (!data.ReadString(uriString)) { 687 HILOG_ERROR("Parameter StopWatcher fail to ReadParcelable uri"); 688 return E_IPCS; 689 } 690 691 if (uriString.empty()) { 692 HILOG_ERROR("Parameter StopWatcher insideInputUri is empty"); 693 return EINVAL; 694 } 695 696 Uri uri(uriString); 697 int ret = StopWatcher(uri); 698 if (!reply.WriteInt32(ret)) { 699 HILOG_ERROR("Parameter StopWatcher fail to WriteInt32 ret"); 700 return E_IPCS; 701 } 702 703 return ERR_OK; 704 } CheckCallingPermission(const std::string & permission)705 bool FileAccessExtStub::CheckCallingPermission(const std::string &permission) 706 { 707 UserAccessTracer trace; 708 trace.Start("CheckCallingPermission"); 709 Security::AccessToken::AccessTokenID tokenCaller = IPCSkeleton::GetCallingTokenID(); 710 int res = Security::AccessToken::AccessTokenKit::VerifyAccessToken(tokenCaller, permission); 711 if (res != Security::AccessToken::PermissionState::PERMISSION_GRANTED) { 712 HILOG_ERROR("FileAccessExtStub::CheckCallingPermission have no fileAccess permission"); 713 return false; 714 } 715 716 return true; 717 } 718 CmdMoveItem(MessageParcel & data,MessageParcel & reply)719 ErrCode FileAccessExtStub::CmdMoveItem(MessageParcel &data, MessageParcel &reply) 720 { 721 UserAccessTracer trace; 722 trace.Start("CmdMoveItem"); 723 std::string sourceFile; 724 if (!data.ReadString(sourceFile)) { 725 HILOG_ERROR("Parameter Move fail to ReadParcelable uri"); 726 return E_IPCS; 727 } 728 729 std::string targetParent; 730 if (!data.ReadString(targetParent)) { 731 HILOG_ERROR("Parameter Move fail to ReadParcelable uri"); 732 return E_IPCS; 733 } 734 735 bool force = false; 736 if (!data.ReadBool(force)) { 737 HILOG_ERROR("Parameter Copy fail to ReadBool force"); 738 return E_IPCS; 739 } 740 741 std::vector<Result> moveResult; 742 Uri source(sourceFile); 743 Uri dest(targetParent); 744 int ret = MoveItem(source, dest, moveResult, force); 745 if (!reply.WriteInt32(ret)) { 746 HILOG_ERROR("Parameter Copy fail to WriteInt32 ret"); 747 return E_IPCS; 748 } 749 750 if (!reply.WriteUint32(moveResult.size())) { 751 HILOG_ERROR("Parameter Copy fail to WriteInt32 size of copyResult"); 752 return E_IPCS; 753 } 754 755 for (auto &result : moveResult) { 756 if (!reply.WriteParcelable(&result)) { 757 HILOG_ERROR("Parameter Copy fail to WriteParcelable copyResult"); 758 return E_IPCS; 759 } 760 } 761 762 return ERR_OK; 763 } 764 CmdMoveFile(MessageParcel & data,MessageParcel & reply)765 ErrCode FileAccessExtStub::CmdMoveFile(MessageParcel &data, MessageParcel &reply) 766 { 767 UserAccessTracer trace; 768 trace.Start("CmdMoveFile"); 769 std::string sourceFile; 770 if (!data.ReadString(sourceFile)) { 771 HILOG_ERROR("Parameter Move fail to ReadParcelable sourceUri"); 772 return E_IPCS; 773 } 774 775 std::string targetParent; 776 if (!data.ReadString(targetParent)) { 777 HILOG_ERROR("Parameter Move fail to ReadParcelable targetParentUri"); 778 return E_IPCS; 779 } 780 781 std::string fileName; 782 if (!data.ReadString(fileName)) { 783 HILOG_ERROR("Parameter Move fail to ReadParcelable fileName"); 784 return E_IPCS; 785 } 786 std::string newFile; 787 OHOS::Uri newFileUri(newFile); 788 Uri source(sourceFile); 789 Uri target(targetParent); 790 int ret = MoveFile(source, target, fileName, newFileUri); 791 if (!reply.WriteInt32(ret)) { 792 HILOG_ERROR("Parameter Move fail to WriteInt32 ret"); 793 return E_IPCS; 794 } 795 796 std::string insideOutputUri = newFileUri.ToString(); 797 if (!reply.WriteString(insideOutputUri)) { 798 HILOG_ERROR("Parameter Move fail to WriteParcelable newFileUri"); 799 return E_IPCS; 800 } 801 802 return ERR_OK; 803 } 804 } // namespace FileAccessFwk 805 } // namespace OHOS 806