File: fusebox_copy_to_fd.cc

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (140 lines) | stat: -rw-r--r-- 4,739 bytes parent folder | download | duplicates (6)
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
130
131
132
133
134
135
136
137
138
139
140
// Copyright 2023 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/ash/fusebox/fusebox_copy_to_fd.h"

#include "base/posix/eintr_wrapper.h"
#include "base/task/thread_pool.h"
#include "base/threading/scoped_blocking_call.h"
#include "chrome/browser/ash/fusebox/fusebox_errno.h"
#include "content/public/browser/browser_task_traits.h"
#include "content/public/browser/browser_thread.h"
#include "net/base/io_buffer.h"
#include "net/base/net_errors.h"
#include "storage/browser/file_system/file_stream_reader.h"

namespace fusebox {

namespace {

using Expected = base::expected<base::ScopedFD, int>;

Expected WriteToScopedFDOffTheIOThread(base::ScopedFD scoped_fd,
                                       scoped_refptr<net::IOBuffer> buffer,
                                       int length) {
  base::ScopedBlockingCall scoped_blocking_call(FROM_HERE,
                                                base::BlockingType::MAY_BLOCK);

  int fd = scoped_fd.get();
  char* ptr = buffer->data();
  if (HANDLE_EINTR(write(fd, ptr, static_cast<size_t>(length))) == -1) {
    return base::unexpected(errno);
  }
  return std::move(scoped_fd);
}

class FDCopier {
 public:
  FDCopier(scoped_refptr<storage::FileSystemContext> fs_context,
           const storage::FileSystemURL& fs_url,
           base::ScopedFD scoped_fd,
           base::OnceCallback<void(Expected)> callback);

  void CallRead();

 private:
  // The int is *not* a POSIX error code. If negative, it's a net error code.
  // If non-negative, it's the number of bytes read.
  void OnRead(int result);
  void OnWrite(Expected result);
  void Finish(Expected result);

  static constexpr size_t kBufferLen = 65536;

  std::unique_ptr<storage::FileStreamReader> fs_reader_;
  scoped_refptr<net::IOBuffer> buffer_;
  base::ScopedFD scoped_fd_;
  base::OnceCallback<void(Expected)> callback_;
};

FDCopier::FDCopier(scoped_refptr<storage::FileSystemContext> fs_context,
                   const storage::FileSystemURL& fs_url,
                   base::ScopedFD scoped_fd,
                   base::OnceCallback<void(Expected)> callback)
    : fs_reader_(fs_context->CreateFileStreamReader(fs_url,
                                                    0,
                                                    INT64_MAX,
                                                    base::Time())),
      buffer_(base::MakeRefCounted<net::IOBufferWithSize>(kBufferLen)),
      scoped_fd_(std::move(scoped_fd)),
      callback_(std::move(callback)) {}

void FDCopier::CallRead() {
  int read_result =
      fs_reader_->Read(buffer_.get(), kBufferLen,
                       base::BindOnce(&FDCopier::OnRead,
                                      // base::Unretained is safe because |this|
                                      // isn't deleted until Finish is called.
                                      base::Unretained(this)));
  if (read_result != net::ERR_IO_PENDING) {  // The read was synchronous.
    OnRead(read_result);
  }
}

void FDCopier::OnRead(int result) {
  DCHECK_CURRENTLY_ON(content::BrowserThread::IO);

  if (result == 0) {
    Finish(std::move(scoped_fd_));
    return;
  } else if (result < 0) {
    Finish(base::unexpected(NetErrorToErrno(result)));
    return;
  }

  base::ThreadPool::PostTaskAndReplyWithResult(
      FROM_HERE, {base::MayBlock()},
      base::BindOnce(&WriteToScopedFDOffTheIOThread, std::move(scoped_fd_),
                     buffer_, result),
      base::BindOnce(&FDCopier::OnWrite,
                     // base::Unretained is safe because |this| isn't deleted
                     // until Finish is called.
                     base::Unretained(this)));
}

void FDCopier::OnWrite(Expected result) {
  DCHECK_CURRENTLY_ON(content::BrowserThread::IO);

  if (!result.has_value()) {
    Finish(std::move(result));
    return;
  }
  scoped_fd_ = std::move(result.value());
  CallRead();
}

void FDCopier::Finish(Expected result) {
  DCHECK_CURRENTLY_ON(content::BrowserThread::IO);

  std::move(callback_).Run(std::move(result));
  delete this;
}

}  // namespace

void CopyToFileDescriptor(scoped_refptr<storage::FileSystemContext> fs_context,
                          const storage::FileSystemURL& src_fs_url,
                          base::ScopedFD dst_scoped_fd,
                          base::OnceCallback<void(Expected)> callback) {
  DCHECK_CURRENTLY_ON(content::BrowserThread::IO);

  // This new-ly created object is deleted in FDCopier::Finish.
  FDCopier* copier =
      new FDCopier(std::move(fs_context), src_fs_url, std::move(dst_scoped_fd),
                   std::move(callback));

  copier->CallRead();
}

}  // namespace fusebox