• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2025 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 #ifndef INTERFACES_KITS_JS_SRC_MOD_FS_FS_UTILS_H
17 #define INTERFACES_KITS_JS_SRC_MOD_FS_FS_UTILS_H
18 
19 #include <cstddef>
20 #include <cstdint>
21 #include <dirent.h>
22 #include <fcntl.h>
23 #include <memory>
24 #include <optional>
25 #include <sstream>
26 #include <unistd.h>
27 #include <vector>
28 
29 #include <sys/stat.h>
30 #include <sys/types.h>
31 
32 #include "fd_guard.h"
33 #include "uv.h"
34 
35 #if !defined(WIN_PLATFORM) && !defined(IOS_PLATFORM)
36 #include "iremote_broker.h"
37 #endif
38 
39 namespace OHOS::FileManagement::ModuleFileIO {
40 using namespace std;
41 
42 constexpr int32_t RDONLY = UV_FS_O_RDONLY;
43 constexpr int32_t WRONLY = UV_FS_O_WRONLY;
44 constexpr int32_t RDWR = UV_FS_O_RDWR;
45 constexpr int32_t CREATE = UV_FS_O_CREAT;
46 constexpr int32_t TRUNC = UV_FS_O_TRUNC;
47 constexpr int32_t APPEND = UV_FS_O_APPEND;
48 constexpr int32_t NONBLOCK = UV_FS_O_NONBLOCK;
49 constexpr int32_t DIRECTORY = UV_FS_O_DIRECTORY;
50 constexpr int32_t NOFOLLOW = UV_FS_O_NOFOLLOW;
51 constexpr int32_t SYNC = UV_FS_O_SYNC;
52 
53 constexpr uint32_t MODE_EXIST = 00;
54 constexpr uint32_t MODE_WRITE = 02;
55 constexpr uint32_t MODE_READ = 04;
56 constexpr uint32_t MODE_READ_WRITE = 06;
57 
58 constexpr uint32_t USR_READ_ONLY = 00;
59 constexpr uint32_t USR_WRITE_ONLY = 01;
60 constexpr uint32_t USR_RDWR = 02;
61 constexpr uint32_t USR_CREATE = 0100;
62 constexpr uint32_t USR_TRUNC = 01000;
63 constexpr uint32_t USR_APPEND = 02000;
64 constexpr uint32_t USR_NONBLOCK = 04000;
65 constexpr uint32_t USR_DIRECTORY = 0200000;
66 constexpr uint32_t USR_NOFOLLOW = 0400000;
67 constexpr uint32_t USR_SYNC = 04010000;
68 
69 const double NS = 1e9;
70 const double MS = 1e3;
71 
72 struct FileInfo {
73     bool isPath = false;
74     unique_ptr<char[]> path = { nullptr };
75     unique_ptr<DistributedFS::FDGuard> fdg = { nullptr };
76 };
77 
78 #if !defined(WIN_PLATFORM) && !defined(IOS_PLATFORM)
79 class FileIoToken : public IRemoteBroker {
80 public:
81     DECLARE_INTERFACE_DESCRIPTOR(u"ohos.fileio.open");
82 
83     FileIoToken() = default;
84     virtual ~FileIoToken() noexcept = default;
85 };
86 #endif
87 
88 class FsUtils {
89 public:
90     static tuple<bool, size_t> GetActualLen(size_t bufLen, size_t bufOff, const optional<int64_t> &length = nullopt);
91     static uint32_t ConvertFlags(const uint32_t &flags);
92     static void FsReqCleanup(uv_fs_t *req);
93     static string GetModeFromFlags(const uint32_t &flags);
94     static bool CheckPublicDirPath(const string &sandboxPath);
95     static string Decode(const string &uri);
96 };
97 
98 } // namespace OHOS::FileManagement::ModuleFileIO
99 #endif // INTERFACES_KITS_JS_SRC_MOD_FS_FS_UTILS_H
100