• 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 #include "copy/distributed_file_fd_guard.h"
17 
18 #include <unistd.h>
19 #include "utils_log.h"
20 
21 #undef LOG_DOMAIN
22 #undef LOG_TAG
23 #define LOG_DOMAIN 0xD004315
24 #define LOG_TAG "distributedfile_daemon"
25 
26 namespace OHOS {
27 namespace Storage {
28 namespace DistributedFile {
FDGuard(int fd)29 FDGuard::FDGuard(int fd) : fd_(fd) {}
30 
FDGuard(int fd,bool autoClose)31 FDGuard::FDGuard(int fd, bool autoClose) : fd_(fd), autoClose_(autoClose) {}
32 
FDGuard(FDGuard && fdg)33 FDGuard::FDGuard(FDGuard &&fdg) : fd_(fdg.fd_), autoClose_(fdg.autoClose_)
34 {
35     fdg.fd_ = -1;
36 }
37 
operator =(FDGuard && fdg)38 FDGuard &FDGuard::operator=(FDGuard &&fdg)
39 {
40     if (this == &fdg) {
41         return *this;
42     }
43     this->fd_ = fdg.fd_;
44     this->autoClose_ = fdg.autoClose_;
45     fdg.fd_ = -1;
46     return *this;
47 }
48 
~FDGuard()49 FDGuard::~FDGuard()
50 {
51     if (fd_ >= 0 && fd_ <= STDERR_FILENO) {
52         LOGI("~FDGuard, fd_ = %{public}d", fd_);
53     }
54     if (fd_ >= 0 && autoClose_) {
55         close(fd_);
56     }
57 }
58 
operator bool() const59 FDGuard::operator bool() const
60 {
61     return fd_ >= 0;
62 }
63 
GetFD() const64 int FDGuard::GetFD() const
65 {
66     return fd_;
67 }
68 
SetFD(int fd,bool autoClose)69 void FDGuard::SetFD(int fd, bool autoClose)
70 {
71     fd_ = fd;
72     autoClose_ = autoClose;
73 }
74 
ClearFD()75 void FDGuard::ClearFD()
76 {
77     fd_ = -1;
78 }
79 } // namespace DistributedFile
80 } // namespace Storage
81 } // namespace OHOS
82