File: dropbox.cc

package info (click to toggle)
android-platform-system-extras 10.0.0%2Br36%2Bds-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 21,588 kB
  • sloc: cpp: 45,171; python: 10,047; ansic: 8,169; sh: 4,029; java: 2,239; javascript: 1,752; xml: 991; asm: 169; makefile: 13
file content (129 lines) | stat: -rw-r--r-- 4,325 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
/*
 *
 * Copyright 2017, The Android Open Source Project
 *
 * 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 "dropbox.h"

#include <cstdio>
#include <cstdlib>
#include <memory>

#include <inttypes.h>
#include <unistd.h>

#include <android-base/logging.h>
#include <android-base/stringprintf.h>
#include <android-base/unique_fd.h>
#include <android/os/DropBoxManager.h>
#include <binder/Status.h>
#include <utils/String8.h>

#include "perfprofd_record.pb.h"

#include "perfprofd_io.h"

namespace android {
namespace perfprofd {
namespace dropbox {

namespace {

bool WriteDropboxFile(android::perfprofd::PerfprofdRecord* encodedProfile,
                      const std::string& temp_dir,
                      std::string* error_msg) {
  android::base::unique_fd tmp_fd;
  {
    char path[PATH_MAX];
    snprintf(path, sizeof(path), "%s/dropboxtmp-XXXXXX", temp_dir.c_str());
    tmp_fd.reset(mkstemp(path));
    if (tmp_fd.get() == -1) {
      *error_msg = android::base::StringPrintf("Could not create temp file %s: %s",
                                               path,
                                               strerror(errno));
      return false;
    }
    if (unlink(path) != 0) {
      PLOG(WARNING) << "Could not unlink binder temp file";
    }
  }

  // Dropbox takes ownership of the fd, and if it is not readonly,
  // a selinux violation will occur. Get a read-only version.
  android::base::unique_fd read_only;
  {
    char fdpath[64];
    snprintf(fdpath, arraysize(fdpath), "/proc/self/fd/%d", tmp_fd.get());
    read_only.reset(open(fdpath, O_RDONLY | O_CLOEXEC));
    if (read_only.get() < 0) {
      *error_msg = android::base::StringPrintf("Could not create read-only fd: %s",
                                               strerror(errno));
      return false;
    }
  }

  constexpr bool kCompress = true;  // Ignore the config here. Dropbox will always end up
                                    // compressing the data, might as well make the temp
                                    // file smaller and help it out.
  using DropBoxManager = android::os::DropBoxManager;
  constexpr int kDropboxFlags = DropBoxManager::IS_GZIPPED;

  if (!SerializeProtobuf(encodedProfile, std::move(tmp_fd), kCompress)) {
    *error_msg = "Could not serialize to temp file";
    return false;
  }

  sp<DropBoxManager> dropbox(new DropBoxManager());
  android::binder::Status status =  dropbox->addFile(String16("perfprofd"),
                                                     read_only.release(),
                                                     kDropboxFlags);
  if (!status.isOk()) {
    *error_msg = status.toString8();
    return false;
  }
  return true;
}

}  // namespace

bool SendToDropbox(android::perfprofd::PerfprofdRecord* profile,
                   const std::string& temp_directory,
                   std::string* error_msg) {
  size_t size = profile->ByteSize();
  if (size < 1024 * 1024) {
    // For a small size, send as a byte buffer directly.
    std::unique_ptr<uint8_t[]> data(new uint8_t[size]);
    profile->SerializeWithCachedSizesToArray(data.get());

    using DropBoxManager = android::os::DropBoxManager;
    sp<DropBoxManager> dropbox(new DropBoxManager());
    android::binder::Status status = dropbox->addData(String16("perfprofd"),
                                                      data.get(),
                                                      size,
                                                      0);
    if (!status.isOk()) {
      *error_msg = status.toString8();
      return false;
    }
    return true;
  } else {
    // For larger buffers, we need to go through the filesystem.
    return WriteDropboxFile(profile, temp_directory, error_msg);
  }
}

}  // namespace dropbox
}  // namespace perfprofd
}  // namespace android