blob: 5e8b494f772873823051ddb5e28d56b143b5cf20 [file] [log] [blame]
/*
* Copyright (C) 2008 The Android Open Source Project
* Copyright (C) 2010-2021 Tuxera Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <dirent.h>
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <linux/fs.h>
#include <sys/ioctl.h>
#include <sys/mman.h>
#include <sys/mount.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <linux/kdev_t.h>
#define LOG_TAG "Vold::Apfs"
#include <android-base/logging.h>
#include <android-base/stringprintf.h>
#include <selinux/selinux.h>
#include <logwrap/logwrap.h>
#include "Utils.h"
#include "Apfs.h"
#include "VoldUtil.h"
using android::base::StringPrintf;
namespace android {
namespace vold {
namespace apfs {
status_t Detect(const std::string& source, bool *outResult) {
const char *const fsPath = source.c_str();
int retval = -1;
int fd = open(fsPath, O_RDONLY | O_CLOEXEC);
if (fd != -1) {
loff_t seek_res = lseek64(fd, 0, SEEK_SET);
if (seek_res == 0) {
char sb_region[4096];
ssize_t read_res = read(fd, sb_region, 4096);
if (read_res >= 512) {
if (!memcmp(&sb_region[32], "NXSB", 4)) {
LOG(INFO) << "APFS filesystem detected.";
*outResult = true;
}
else {
LOG(VERBOSE) << "Filesystem detection failed (not an APFS "
<< "filesystem).";
*outResult = false;
}
retval = 0;
}
else if (read_res != -1) {
int err = errno ? errno : EIO;
LOG(ERROR) << "Error while reading 4096 bytes from device "
<< "offset 0: " << strerror(errno) << " (" << errno
<< ")";
errno = err;
}
}
else if (seek_res != -1) {
int err = errno ? errno : EIO;
LOG(ERROR) << "Error while seeking to device offset 0: "
<< strerror(errno) << " (" << errno << ")";
errno = err;
}
close(fd);
}
return retval;
}
} // namespace apfs
} // namespace vold
} // namespace android