Fix static analysis issue
[platform/core/appfw/pkgmgr-info.git] / src / server / pkg_request.cc
1 /*
2  * Copyright (c) 2021 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #include "pkg_request.hh"
18
19 #include "utils/logging.hh"
20
21 #include "pkgmgrinfo_debug.h"
22
23 namespace pkgmgr_server {
24
25 PkgRequest::PkgRequest()
26     : request_type_(pkgmgr_common::REQ_TYPE_NONE), data_size_(-1) {}
27
28 PkgRequest::PkgRequest(int fd)
29     : request_type_(pkgmgr_common::REQ_TYPE_NONE), data_size_(-1) {
30   socket_ = std::unique_ptr<pkgmgr_common::socket::DataSocket>(
31       new (std::nothrow) pkgmgr_common::socket::DataSocket(fd));
32   if (socket_ == nullptr)
33     LOG(ERROR) << "Out of memory";
34 }
35
36 PkgRequest::~PkgRequest() {
37   if (data_ != nullptr)
38     delete[] data_;
39 }
40
41 unsigned char* PkgRequest::GetData() {
42   return data_;
43 }
44
45 int PkgRequest::GetSize() {
46   return data_size_;
47 }
48
49 int PkgRequest::GetFd() {
50   return socket_->GetFd();
51 }
52
53 pid_t PkgRequest::GetSenderPID() {
54   return socket_->GetPID();
55 }
56
57 uid_t PkgRequest::GetSenderUID() {
58   return socket_->GetUID();
59 }
60
61 pkgmgr_common::ReqType PkgRequest::GetRequestType() {
62   return request_type_;
63 }
64
65 bool PkgRequest::ReceiveData() {
66   int ret = socket_->ReceiveData(&request_type_, sizeof(request_type_));
67   if (ret < 0 || request_type_ == pkgmgr_common::REQ_TYPE_NONE) {
68     LOG(ERROR) << "Failed to ReceiveData";
69     return false;
70   }
71   ret = socket_->ReceiveData(&data_size_, sizeof(data_size_));
72   if (ret < 0) {
73     LOG(ERROR) << "Failed to ReceiveData";
74     request_type_ = pkgmgr_common::REQ_TYPE_NONE;
75     return false;
76   }
77   if (data_size_ <= 0) {
78     LOG(ERROR) << "Invalid data";
79     request_type_ = pkgmgr_common::REQ_TYPE_NONE;
80     return false;
81   }
82
83   data_ = new (std::nothrow) unsigned char[data_size_];
84   if (data_ == nullptr) {
85     LOG(ERROR) << "Out of memory";
86     request_type_ = pkgmgr_common::REQ_TYPE_NONE;
87     return false;
88   }
89   ret = socket_->ReceiveData(reinterpret_cast<void*>(data_), data_size_);
90   if (ret < 0) {
91     LOG(ERROR) << "Failed to ReceiveData";
92     delete[] data_;
93     data_ = nullptr;
94     request_type_ = pkgmgr_common::REQ_TYPE_NONE;
95     return false;
96   }
97
98   return true;
99 }
100
101 bool PkgRequest::SendData(unsigned char* data, int size) {
102   if (socket_->SendData(&size, sizeof(size)) < 0)
103     return false;
104
105   return (socket_->SendData(data, size) == 0);
106 }
107
108 }  // namespace pkgmgr_server