1 /* 2 * Copyright (C) 2021 The Android Open Source Project 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 #pragma once 18 19 #ifdef __BIONIC__ 20 #include <linux/incrementalfs.h> 21 #include <sys/vfs.h> 22 #endif 23 24 namespace incfs::util { 25 26 // Some tools useful for writing hardened code 27 28 // Clear the passed container and make sure it frees all allocated memory. 29 // Useful for signal handling code where any remaining memory would leak. 30 template <class Container> clearAndFree(Container & c)31void clearAndFree(Container& c) { 32 Container().swap(c); 33 } 34 isIncfsFd(int fd)35bool isIncfsFd([[maybe_unused]] int fd) { 36 #ifdef __BIONIC__ 37 struct statfs fs = {}; 38 if (::fstatfs(fd, &fs) != 0) { 39 return false; 40 } 41 return fs.f_type == static_cast<decltype(fs.f_type)>(INCFS_MAGIC_NUMBER); 42 #else 43 return false; // incfs is linux-specific, and only matters on Android. 44 #endif 45 } 46 47 } // namespace incfs::util 48