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 FDSAN_FD_H 17 #define FDSAN_FD_H 18 19 #include <cerrno> 20 #include <cstdio> 21 #include <fcntl.h> 22 #include <unistd.h> 23 #include <utility> 24 25 namespace OHOS { 26 namespace Media { 27 28 class FdsanFd { 29 public: 30 FdsanFd() = default; 31 explicit FdsanFd(int32_t fd); 32 ~FdsanFd(); 33 FdsanFd(const FdsanFd ©) = delete; 34 FdsanFd(FdsanFd &&move); 35 FdsanFd &operator=(const FdsanFd ©) = delete; 36 FdsanFd &operator=(FdsanFd &&move); 37 38 int32_t Get() const; 39 void Reset(int32_t newFd = INVALID_FD); 40 41 private: 42 static void ExchangeTag(int32_t fd, uint64_t oldTag, uint64_t newTag); 43 static int32_t Close(int32_t fd, uint64_t tag); 44 // Use the address of object as the file tag 45 uint64_t Tag(); 46 int32_t fd_ = INVALID_FD; 47 static constexpr int32_t INVALID_FD = -1; 48 }; 49 } // namespace Media 50 } // namespace OHOS 51 #endif // FDSAN_FD_H