• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023 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 #define FUSE_USE_VERSION 34
17 
18 #include "clouddisk_rdb_utils.h"
19 
20 #include <fuse_lowlevel.h>
21 
22 #include "utils_log.h"
23 #include "dfs_error.h"
24 #include "clouddisk_db_const.h"
25 #include "file_column.h"
26 #include "result_set.h"
27 
28 namespace OHOS::FileManagement::CloudDisk {
29 using namespace std;
30 using namespace NativeRdb;
31 
GetInt(const string & key,int32_t & val,const shared_ptr<ResultSet> resultSet)32 int32_t CloudDiskRdbUtils::GetInt(const string &key, int32_t &val,
33                                   const shared_ptr<ResultSet> resultSet)
34 {
35     int32_t index;
36     int32_t err = resultSet->GetColumnIndex(key, index);
37     if (err != E_OK) {
38         LOGE("result set get column index err %{public}d", err);
39         return E_RDB;
40     }
41     err = resultSet->GetInt(index, val);
42     if (err != 0) {
43         LOGE("result set get int err %{public}d", err);
44         return E_RDB;
45     }
46     return E_OK;
47 }
48 
GetLong(const string & key,int64_t & val,const shared_ptr<ResultSet> resultSet)49 int32_t CloudDiskRdbUtils::GetLong(const string &key, int64_t &val,
50                                    const shared_ptr<ResultSet> resultSet)
51 {
52     int32_t index;
53     int32_t err = resultSet->GetColumnIndex(key, index);
54     if (err != E_OK) {
55         LOGE("result set get column index err %{public}d", err);
56         return E_RDB;
57     }
58     err = resultSet->GetLong(index, val);
59     if (err != 0) {
60         LOGE("result set get int err %{public}d", err);
61         return E_RDB;
62     }
63     return E_OK;
64 }
65 
GetString(const string & key,string & val,const shared_ptr<ResultSet> resultSet)66 int32_t CloudDiskRdbUtils::GetString(const string &key, string &val,
67                                      const shared_ptr<ResultSet> resultSet)
68 {
69     int32_t index;
70     int32_t err = resultSet->GetColumnIndex(key, index);
71     if (err != E_OK) {
72         LOGE("result set get column index err %{public}d", err);
73         return E_RDB;
74     }
75     err = resultSet->GetString(index, val);
76     if (err != 0) {
77         LOGE("result set get string err %{public}d", err);
78         return E_RDB;
79     }
80     return E_OK;
81 }
82 
FillFileInfo(const RowEntity & rowEntity,CloudDiskFileInfo & info)83 static void FillFileInfo(const RowEntity &rowEntity, CloudDiskFileInfo &info)
84 {
85     rowEntity.Get(FileColumn::FILE_NAME).GetString(info.fileName);
86     rowEntity.Get(FileColumn::CLOUD_ID).GetString(info.cloudId);
87     rowEntity.Get(FileColumn::PARENT_CLOUD_ID).GetString(info.parentCloudId);
88     int32_t int_variable;
89     rowEntity.Get(FileColumn::POSITION).GetInt(int_variable);
90     info.location = static_cast<uint32_t>(int_variable);
91     int64_t long_variable;
92     rowEntity.Get(FileColumn::FILE_SIZE).GetLong(long_variable);
93     info.size = static_cast<unsigned long long>(long_variable);
94     rowEntity.Get(FileColumn::FILE_TIME_ADDED).GetLong(long_variable);
95     info.atime = static_cast<unsigned long long>(long_variable);
96     rowEntity.Get(FileColumn::META_TIME_EDITED).GetLong(long_variable);
97     info.ctime = static_cast<unsigned long long>(long_variable);
98     rowEntity.Get(FileColumn::FILE_TIME_EDITED).GetLong(long_variable);
99     info.mtime = static_cast<unsigned long long>(long_variable);
100     rowEntity.Get(FileColumn::IS_DIRECTORY).GetInt(int_variable);
101     info.IsDirectory = (int_variable == DIRECTORY);
102 }
103 
ResultSetToFileInfo(const shared_ptr<ResultSet> resultSet,CloudDiskFileInfo & info)104 int32_t CloudDiskRdbUtils::ResultSetToFileInfo(const shared_ptr<ResultSet> resultSet,
105                                                CloudDiskFileInfo &info)
106 {
107     if (resultSet == nullptr) {
108         LOGE("result set is nullptr");
109         return E_RDB;
110     }
111     if (resultSet->GoToNextRow() != E_OK) {
112         LOGE("result set to file info go to next row failed");
113         return E_RDB;
114     }
115     RowEntity rowEntity;
116     if (resultSet->GetRow(rowEntity) != E_OK) {
117         LOGE("result set to file info get row failed");
118         return E_RDB;
119     }
120     FillFileInfo(rowEntity, info);
121     return E_OK;
122 }
123 
FuseDentryAlignSize(const char * name)124 size_t CloudDiskRdbUtils::FuseDentryAlignSize(const char *name)
125 {
126     return fuse_add_direntry({}, nullptr, 0, name, nullptr, 0);
127 }
128 
ResultSetToFileInfos(const shared_ptr<ResultSet> resultSet,vector<CloudDiskFileInfo> & infos)129 int32_t CloudDiskRdbUtils::ResultSetToFileInfos(const shared_ptr<ResultSet> resultSet,
130                                                 vector<CloudDiskFileInfo> &infos)
131 {
132     if (resultSet == nullptr) {
133         LOGE("result set is nullptr");
134         return E_RDB;
135     }
136     off_t nextOff = 0;
137     while (resultSet->GoToNextRow() == E_OK) {
138         CloudDiskFileInfo info;
139         GetString(FileColumn::FILE_NAME, info.fileName, resultSet);
140         int32_t isDirectory;
141         GetInt(FileColumn::IS_DIRECTORY, isDirectory, resultSet);
142         info.IsDirectory = (isDirectory == DIRECTORY);
143         nextOff += FuseDentryAlignSize(info.fileName.c_str());
144         info.nextOff = nextOff;
145         infos.emplace_back(info);
146     }
147     return E_OK;
148 }
149 }