blob: cada4bd2a0f8cd593d91c5815ef8a1c19294aaa3 [file] [log] [blame]
Doug Zongker18a78e02014-07-10 07:31:46 -07001/*
2 * Copyright (C) 2014 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
Tao Bao91a7aa42017-05-01 15:57:38 -070017#include "fuse_adb_provider.h"
18
Doug Zongker18a78e02014-07-10 07:31:46 -070019#include <errno.h>
Tao Bao91a7aa42017-05-01 15:57:38 -070020#include <stdio.h>
21#include <stdlib.h>
22#include <string.h>
23
24#include <functional>
Doug Zongker18a78e02014-07-10 07:31:46 -070025
26#include "adb.h"
Dan Albert432584f2015-02-24 22:48:25 -080027#include "adb_io.h"
Doug Zongker18a78e02014-07-10 07:31:46 -070028#include "fuse_sideload.h"
Doug Zongker18a78e02014-07-10 07:31:46 -070029
xunchangea2912f2019-03-17 16:45:12 -070030bool FuseAdbDataProvider::ReadBlockAlignedData(uint8_t* buffer, uint32_t fetch_size,
31 uint32_t start_block) const {
32 if (!WriteFdFmt(fd_, "%08u", start_block)) {
Tao Bao91a7aa42017-05-01 15:57:38 -070033 fprintf(stderr, "failed to write to adb host: %s\n", strerror(errno));
xunchangea2912f2019-03-17 16:45:12 -070034 return false;
Tao Bao91a7aa42017-05-01 15:57:38 -070035 }
Doug Zongker18a78e02014-07-10 07:31:46 -070036
xunchangea2912f2019-03-17 16:45:12 -070037 if (!ReadFdExactly(fd_, buffer, fetch_size)) {
Tao Bao91a7aa42017-05-01 15:57:38 -070038 fprintf(stderr, "failed to read from adb host: %s\n", strerror(errno));
xunchangea2912f2019-03-17 16:45:12 -070039 return false;
Tao Bao91a7aa42017-05-01 15:57:38 -070040 }
Doug Zongker18a78e02014-07-10 07:31:46 -070041
xunchangea2912f2019-03-17 16:45:12 -070042 return true;
Doug Zongker18a78e02014-07-10 07:31:46 -070043}
44
xunchangea2912f2019-03-17 16:45:12 -070045void FuseAdbDataProvider::Close() {
46 WriteFdExactly(fd_, "DONEDONE");
47}
48
49int run_adb_fuse(android::base::unique_fd&& sfd, uint64_t file_size, uint32_t block_size) {
50 FuseAdbDataProvider adb_data_reader(std::move(sfd), file_size, block_size);
Doug Zongker18a78e02014-07-10 07:31:46 -070051
Tao Bao91a7aa42017-05-01 15:57:38 -070052 provider_vtab vtab;
xunchangea2912f2019-03-17 16:45:12 -070053 vtab.read_block = std::bind(&FuseAdbDataProvider::ReadBlockAlignedData, &adb_data_reader,
54 std::placeholders::_2, std::placeholders::_3, std::placeholders::_1);
55 vtab.close = [&adb_data_reader]() { adb_data_reader.Close(); };
Doug Zongker18a78e02014-07-10 07:31:46 -070056
Tao Bao91a7aa42017-05-01 15:57:38 -070057 return run_fuse_sideload(vtab, file_size, block_size);
Doug Zongker18a78e02014-07-10 07:31:46 -070058}