1 /*
2 * Copyright (c) 2021 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 "dfsu_fd_guard.h"
17
18 #include <unistd.h>
19
20 #include "utils_log.h"
21
22 namespace OHOS {
23 namespace Storage {
24 namespace DistributedFile {
DfsuFDGuard(int fd)25 DfsuFDGuard::DfsuFDGuard(int fd) : fd_(fd) {}
26
DfsuFDGuard(int fd,bool autoClose)27 DfsuFDGuard::DfsuFDGuard(int fd, bool autoClose) : fd_(fd), autoClose_(autoClose) {}
28
~DfsuFDGuard()29 DfsuFDGuard::~DfsuFDGuard()
30 {
31 if (fd_ >= 0 && autoClose_) {
32 LOGD("DfsuFDGuard Deconstruction, fd_ = %{public}d", fd_);
33 close(fd_);
34 }
35 }
36
operator bool() const37 DfsuFDGuard::operator bool() const
38 {
39 return fd_ >= 0;
40 }
41
GetFD() const42 int DfsuFDGuard::GetFD() const
43 {
44 return fd_;
45 }
46
SetFD(int fd,bool autoClose)47 void DfsuFDGuard::SetFD(int fd, bool autoClose)
48 {
49 fd_ = fd;
50 autoClose_ = autoClose;
51 }
52
ClearFD()53 void DfsuFDGuard::ClearFD()
54 {
55 fd_ = -1;
56 }
57 } // namespace DistributedFile
58 } // namespace Storage
59 } // namespace OHOS
60