blob: 9bd3f23929611f4115db5a096afdf0f4b249da16 [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
Tao Bao91a7aa42017-05-01 15:57:38 -070030int read_block_adb(const adb_data& ad, uint32_t block, uint8_t* buffer, uint32_t fetch_size) {
31 if (!WriteFdFmt(ad.sfd, "%08u", block)) {
32 fprintf(stderr, "failed to write to adb host: %s\n", strerror(errno));
33 return -EIO;
34 }
Doug Zongker18a78e02014-07-10 07:31:46 -070035
Tao Bao91a7aa42017-05-01 15:57:38 -070036 if (!ReadFdExactly(ad.sfd, buffer, fetch_size)) {
37 fprintf(stderr, "failed to read from adb host: %s\n", strerror(errno));
38 return -EIO;
39 }
Doug Zongker18a78e02014-07-10 07:31:46 -070040
Tao Bao91a7aa42017-05-01 15:57:38 -070041 return 0;
Doug Zongker18a78e02014-07-10 07:31:46 -070042}
43
44int run_adb_fuse(int sfd, uint64_t file_size, uint32_t block_size) {
Tao Bao91a7aa42017-05-01 15:57:38 -070045 adb_data ad;
46 ad.sfd = sfd;
47 ad.file_size = file_size;
48 ad.block_size = block_size;
Doug Zongker18a78e02014-07-10 07:31:46 -070049
Tao Bao91a7aa42017-05-01 15:57:38 -070050 provider_vtab vtab;
51 vtab.read_block = std::bind(read_block_adb, ad, std::placeholders::_1, std::placeholders::_2,
52 std::placeholders::_3);
53 vtab.close = [&ad]() { WriteFdExactly(ad.sfd, "DONEDONE"); };
Doug Zongker18a78e02014-07-10 07:31:46 -070054
Tao Bao91a7aa42017-05-01 15:57:38 -070055 return run_fuse_sideload(vtab, file_size, block_size);
Doug Zongker18a78e02014-07-10 07:31:46 -070056}