loop_device.cpp 1.9 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
/*
 * Copyright (C) 2017 Simon Fels <morphis@gravedo.de>
 *
 * This program is free software: you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 3, as published
 * by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranties of
 * MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
 * PURPOSE.  See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

#include "anbox/common/loop_device.h"
#include "anbox/defer_action.h"

Z
ZhaoPengyuan 已提交
21
#include <limits.h>
22 23
#include <system_error>

24
#include <linux/loop.h>
25
#include <errno.h>
26 27 28 29 30 31 32 33
#include <fcntl.h>
#include <sys/ioctl.h>

namespace anbox {
namespace common {
std::shared_ptr<LoopDevice> LoopDevice::create(const boost::filesystem::path &path) {
  const auto fd = ::open(path.c_str(), O_RDWR);
  if (fd < 0)
34
    throw std::system_error{errno, std::system_category()};
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53

  return std::shared_ptr<LoopDevice>(new LoopDevice(Fd{fd}, path));
}

LoopDevice::LoopDevice(Fd fd, const boost::filesystem::path &path) :
  fd_{fd}, path_{path} {}

LoopDevice::~LoopDevice() {
  if (fd_ < 0)
    return;

  ::ioctl(fd_, LOOP_CLR_FD);
  ::close(fd_);
}

bool LoopDevice::attach_file(const boost::filesystem::path &file_path) {
  if (fd_ < 0)
    return false;

Z
ZhaoPengyuan 已提交
54 55 56 57 58 59 60
  const char* untrustPath = file_path.c_str();
  char path[PATH_MAX] = {0};
  if (realpath(untrustPath, path) == NULL) {
    return false;
  }

  int file_fd = ::open(path, O_RDONLY);
61 62 63 64 65 66 67 68 69 70 71 72
  if (file_fd < 0)
    return false;

  DeferAction close_file_fd{[&]() { ::close(file_fd); }};

  if (::ioctl(fd_, LOOP_SET_FD, file_fd) < 0)
    return false;

  return true;
}
} // namespace common
} // namespace anbox