GCC Code Coverage Report
Directory: ../ Exec Total Coverage
File: /home/iojs/build/workspace/node-test-commit-linux-coverage-daily/nodes/benchmark/out/../src/node_file.h Lines: 42 53 79.2 %
Date: 2020-06-24 22:13:30 Branches: 2 2 100.0 %

Line Branch Exec Source
1
#ifndef SRC_NODE_FILE_H_
2
#define SRC_NODE_FILE_H_
3
4
#if defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
5
6
#include "node.h"
7
#include "aliased_buffer.h"
8
#include "node_messaging.h"
9
#include "stream_base.h"
10
#include <iostream>
11
12
namespace node {
13
namespace fs {
14
15
class FileHandleReadWrap;
16
17
8415
class BindingData : public BaseObject {
18
 public:
19
4679
  explicit BindingData(Environment* env, v8::Local<v8::Object> wrap)
20
4679
      : BaseObject(env, wrap),
21
        stats_field_array(env->isolate(), kFsStatsBufferLength),
22
4679
        stats_field_bigint_array(env->isolate(), kFsStatsBufferLength) {}
23
24
  AliasedFloat64Array stats_field_array;
25
  AliasedBigUint64Array stats_field_bigint_array;
26
27
  std::vector<BaseObjectPtr<FileHandleReadWrap>>
28
      file_handle_read_wrap_freelist;
29
30
  static constexpr FastStringKey binding_data_name { "fs" };
31
32
  void MemoryInfo(MemoryTracker* tracker) const override;
33
19
  SET_SELF_SIZE(BindingData)
34
19
  SET_MEMORY_INFO_NAME(BindingData)
35
};
36
37
// structure used to store state during a complex operation, e.g., mkdirp.
38
9596
class FSContinuationData : public MemoryRetainer {
39
 public:
40
  inline FSContinuationData(uv_fs_t* req, int mode, uv_fs_cb done_cb);
41
42
  inline void PushPath(std::string&& path);
43
  inline void PushPath(const std::string& path);
44
  inline std::string PopPath();
45
  // Used by mkdirp to track the first path created:
46
  inline void MaybeSetFirstPath(const std::string& path);
47
  inline void Done(int result);
48
49
10
  int mode() const { return mode_; }
50
9726
  const std::vector<std::string>& paths() const { return paths_; }
51
223
  const std::string& first_path() const { return first_path_; }
52
53
  void MemoryInfo(MemoryTracker* tracker) const override;
54
  SET_MEMORY_INFO_NAME(FSContinuationData)
55
  SET_SELF_SIZE(FSContinuationData)
56
57
 private:
58
  uv_fs_cb done_cb_;
59
  uv_fs_t* req_;
60
  int mode_;
61
  std::vector<std::string> paths_;
62
  std::string first_path_;
63
};
64
65
class FSReqBase : public ReqWrap<uv_fs_t> {
66
 public:
67
  typedef MaybeStackBuffer<char, 64> FSReqBuffer;
68
69
  inline FSReqBase(BindingData* binding_data,
70
                   v8::Local<v8::Object> req,
71
                   AsyncWrap::ProviderType type,
72
                   bool use_bigint);
73
  ~FSReqBase() override;
74
75
  inline void Init(const char* syscall,
76
                   const char* data,
77
                   size_t len,
78
                   enum encoding encoding);
79
  inline FSReqBuffer& Init(const char* syscall, size_t len,
80
                           enum encoding encoding);
81
82
  virtual void Reject(v8::Local<v8::Value> reject) = 0;
83
  virtual void Resolve(v8::Local<v8::Value> value) = 0;
84
  virtual void ResolveStat(const uv_stat_t* stat) = 0;
85
  virtual void SetReturnValue(
86
      const v8::FunctionCallbackInfo<v8::Value>& args) = 0;
87
88
3999
  const char* syscall() const { return syscall_; }
89
3999
  const char* data() const { return has_data_ ? *buffer_ : nullptr; }
90
6700
  enum encoding encoding() const { return encoding_; }
91
3900
  bool use_bigint() const { return use_bigint_; }
92
93
140
  FSContinuationData* continuation_data() const {
94
140
    return continuation_data_.get();
95
  }
96
14
  void set_continuation_data(std::unique_ptr<FSContinuationData> data) {
97
14
    continuation_data_ = std::move(data);
98
14
  }
99
100
50547
  static FSReqBase* from_req(uv_fs_t* req) {
101
50547
    return static_cast<FSReqBase*>(ReqWrap::from_req(req));
102
  }
103
104
  FSReqBase(const FSReqBase&) = delete;
105
  FSReqBase& operator=(const FSReqBase&) = delete;
106
107
  void MemoryInfo(MemoryTracker* tracker) const override;
108
109
4190
  BindingData* binding_data() { return binding_data_.get(); }
110
111
 private:
112
  std::unique_ptr<FSContinuationData> continuation_data_;
113
  enum encoding encoding_ = UTF8;
114
  bool has_data_ = false;
115
  bool use_bigint_ = false;
116
  const char* syscall_ = nullptr;
117
118
  BaseObjectPtr<BindingData> binding_data_;
119
120
  // Typically, the content of buffer_ is something like a file name, so
121
  // something around 64 bytes should be enough.
122
  FSReqBuffer buffer_;
123
};
124
125
98372
class FSReqCallback final : public FSReqBase {
126
 public:
127
  inline FSReqCallback(BindingData* binding_data,
128
                       v8::Local<v8::Object> req,
129
                       bool use_bigint);
130
131
  void Reject(v8::Local<v8::Value> reject) override;
132
  void Resolve(v8::Local<v8::Value> value) override;
133
  void ResolveStat(const uv_stat_t* stat) override;
134
  void SetReturnValue(const v8::FunctionCallbackInfo<v8::Value>& args) override;
135
136
1
  SET_MEMORY_INFO_NAME(FSReqCallback)
137
1
  SET_SELF_SIZE(FSReqCallback)
138
139
  FSReqCallback(const FSReqCallback&) = delete;
140
  FSReqCallback& operator=(const FSReqCallback&) = delete;
141
};
142
143
template <typename NativeT, typename V8T>
144
void FillStatsArray(AliasedBufferBase<NativeT, V8T>* fields,
145
                    const uv_stat_t* s,
146
                    const size_t offset = 0);
147
148
inline v8::Local<v8::Value> FillGlobalStatsArray(BindingData* binding_data,
149
                                                 const bool use_bigint,
150
                                                 const uv_stat_t* s,
151
                                                 const bool second = false);
152
153
template <typename AliasedBufferT>
154
class FSReqPromise final : public FSReqBase {
155
 public:
156
  static inline FSReqPromise* New(BindingData* binding_data,
157
                                  bool use_bigint);
158
  inline ~FSReqPromise() override;
159
160
  inline void Reject(v8::Local<v8::Value> reject) override;
161
  inline void Resolve(v8::Local<v8::Value> value) override;
162
  inline void ResolveStat(const uv_stat_t* stat) override;
163
  inline void SetReturnValue(
164
      const v8::FunctionCallbackInfo<v8::Value>& args) override;
165
  inline void MemoryInfo(MemoryTracker* tracker) const override;
166
167
1
  SET_MEMORY_INFO_NAME(FSReqPromise)
168
1
  SET_SELF_SIZE(FSReqPromise)
169
170
  FSReqPromise(const FSReqPromise&) = delete;
171
  FSReqPromise& operator=(const FSReqPromise&) = delete;
172
  FSReqPromise(const FSReqPromise&&) = delete;
173
  FSReqPromise& operator=(const FSReqPromise&&) = delete;
174
175
 private:
176
  inline FSReqPromise(BindingData* binding_data,
177
                      v8::Local<v8::Object> obj,
178
                      bool use_bigint);
179
180
  bool finished_ = false;
181
  AliasedBufferT stats_field_array_;
182
};
183
184
class FSReqAfterScope final {
185
 public:
186
  FSReqAfterScope(FSReqBase* wrap, uv_fs_t* req);
187
  ~FSReqAfterScope();
188
  void Clear();
189
190
  bool Proceed();
191
192
  void Reject(uv_fs_t* req);
193
194
  FSReqAfterScope(const FSReqAfterScope&) = delete;
195
  FSReqAfterScope& operator=(const FSReqAfterScope&) = delete;
196
  FSReqAfterScope(const FSReqAfterScope&&) = delete;
197
  FSReqAfterScope& operator=(const FSReqAfterScope&&) = delete;
198
199
 private:
200
  BaseObjectPtr<FSReqBase> wrap_;
201
  uv_fs_t* req_ = nullptr;
202
  v8::HandleScope handle_scope_;
203
  v8::Context::Scope context_scope_;
204
};
205
206
class FileHandle;
207
208
// A request wrap specifically for uv_fs_read()s scheduled for reading
209
// from a FileHandle.
210
class FileHandleReadWrap final : public ReqWrap<uv_fs_t> {
211
 public:
212
  FileHandleReadWrap(FileHandle* handle, v8::Local<v8::Object> obj);
213
  ~FileHandleReadWrap() override;
214
215
207
  static inline FileHandleReadWrap* from_req(uv_fs_t* req) {
216
207
    return static_cast<FileHandleReadWrap*>(ReqWrap::from_req(req));
217
  }
218
219
  void MemoryInfo(MemoryTracker* tracker) const override;
220
  SET_MEMORY_INFO_NAME(FileHandleReadWrap)
221
  SET_SELF_SIZE(FileHandleReadWrap)
222
223
 private:
224
  FileHandle* file_handle_;
225
  uv_buf_t buffer_;
226
227
  friend class FileHandle;
228
};
229
230
// A wrapper for a file descriptor that will automatically close the fd when
231
// the object is garbage collected
232
class FileHandle final : public AsyncWrap, public StreamBase {
233
 public:
234
  static FileHandle* New(BindingData* binding_data,
235
                         int fd,
236
                         v8::Local<v8::Object> obj = v8::Local<v8::Object>());
237
  ~FileHandle() override;
238
239
  static void New(const v8::FunctionCallbackInfo<v8::Value>& args);
240
241
292
  int GetFD() override { return fd_; }
242
243
  // Will asynchronously close the FD and return a Promise that will
244
  // be resolved once closing is complete.
245
  static void Close(const v8::FunctionCallbackInfo<v8::Value>& args);
246
247
  // Releases ownership of the FD.
248
  static void ReleaseFD(const v8::FunctionCallbackInfo<v8::Value>& args);
249
250
  // StreamBase interface:
251
  int ReadStart() override;
252
  int ReadStop() override;
253
254
504
  bool IsAlive() override { return !closed_; }
255
212
  bool IsClosing() override { return closing_; }
256
350
  AsyncWrap* GetAsyncWrap() override { return this; }
257
258
  // In the case of file streams, shutting down corresponds to closing.
259
  ShutdownWrap* CreateShutdownWrap(v8::Local<v8::Object> object) override;
260
  int DoShutdown(ShutdownWrap* req_wrap) override;
261
262
  int DoWrite(WriteWrap* w,
263
              uv_buf_t* bufs,
264
              size_t count,
265
              uv_stream_t* send_handle) override;
266
267
  void MemoryInfo(MemoryTracker* tracker) const override;
268
269
  SET_MEMORY_INFO_NAME(FileHandle)
270
  SET_SELF_SIZE(FileHandle)
271
272
  FileHandle(const FileHandle&) = delete;
273
  FileHandle& operator=(const FileHandle&) = delete;
274
  FileHandle(const FileHandle&&) = delete;
275
  FileHandle& operator=(const FileHandle&&) = delete;
276
277
  TransferMode GetTransferMode() const override;
278
  std::unique_ptr<worker::TransferData> TransferForMessaging() override;
279
280
 private:
281
  class TransferData : public worker::TransferData {
282
   public:
283
    explicit TransferData(int fd);
284
    ~TransferData();
285
286
    BaseObjectPtr<BaseObject> Deserialize(
287
        Environment* env,
288
        v8::Local<v8::Context> context,
289
        std::unique_ptr<worker::TransferData> self) override;
290
291
    SET_NO_MEMORY_INFO()
292
    SET_MEMORY_INFO_NAME(FileHandleTransferData)
293
    SET_SELF_SIZE(TransferData)
294
295
   private:
296
    int fd_;
297
  };
298
299
  FileHandle(BindingData* binding_data, v8::Local<v8::Object> obj, int fd);
300
301
  // Synchronous close that emits a warning
302
  void Close();
303
  void AfterClose();
304
305
  class CloseReq final : public ReqWrap<uv_fs_t> {
306
   public:
307
    CloseReq(Environment* env,
308
             v8::Local<v8::Object> obj,
309
             v8::Local<v8::Promise> promise,
310
             v8::Local<v8::Value> ref);
311
    ~CloseReq() override;
312
313
    FileHandle* file_handle();
314
315
    void MemoryInfo(MemoryTracker* tracker) const override;
316
317
    SET_MEMORY_INFO_NAME(CloseReq)
318
    SET_SELF_SIZE(CloseReq)
319
320
    void Resolve();
321
322
    void Reject(v8::Local<v8::Value> reason);
323
324
286
    static CloseReq* from_req(uv_fs_t* req) {
325
286
      return static_cast<CloseReq*>(ReqWrap::from_req(req));
326
    }
327
328
    CloseReq(const CloseReq&) = delete;
329
    CloseReq& operator=(const CloseReq&) = delete;
330
    CloseReq(const CloseReq&&) = delete;
331
    CloseReq& operator=(const CloseReq&&) = delete;
332
333
   private:
334
    v8::Global<v8::Promise> promise_{};
335
    v8::Global<v8::Value> ref_{};
336
  };
337
338
  // Asynchronous close
339
  v8::MaybeLocal<v8::Promise> ClosePromise();
340
341
  int fd_;
342
  bool closing_ = false;
343
  bool closed_ = false;
344
  bool reading_ = false;
345
  int64_t read_offset_ = -1;
346
  int64_t read_length_ = -1;
347
348
  BaseObjectPtr<FileHandleReadWrap> current_read_;
349
350
  BaseObjectPtr<BindingData> binding_data_;
351
};
352
353
int MKDirpSync(uv_loop_t* loop,
354
               uv_fs_t* req,
355
               const std::string& path,
356
               int mode,
357
               uv_fs_cb cb = nullptr);
358
359
class FSReqWrapSync {
360
 public:
361
320355
  FSReqWrapSync() = default;
362
320364
  ~FSReqWrapSync() { uv_fs_req_cleanup(&req); }
363
  uv_fs_t req;
364
365
24629
  FSContinuationData* continuation_data() const {
366
24629
    return continuation_data_.get();
367
  }
368
4784
  void set_continuation_data(std::unique_ptr<FSContinuationData> data) {
369
4784
    continuation_data_ = std::move(data);
370
4784
  }
371
372
  FSReqWrapSync(const FSReqWrapSync&) = delete;
373
  FSReqWrapSync& operator=(const FSReqWrapSync&) = delete;
374
375
 private:
376
  std::unique_ptr<FSContinuationData> continuation_data_;
377
};
378
379
// TODO(addaleax): Currently, callers check the return value and assume
380
// that nullptr indicates a synchronous call, rather than a failure.
381
// Failure conditions should be disambiguated and handled appropriately.
382
inline FSReqBase* GetReqWrap(const v8::FunctionCallbackInfo<v8::Value>& args,
383
                             int index,
384
                             bool use_bigint = false);
385
386
// Returns nullptr if the operation fails from the start.
387
template <typename Func, typename... Args>
388
inline FSReqBase* AsyncDestCall(Environment* env, FSReqBase* req_wrap,
389
                                const v8::FunctionCallbackInfo<v8::Value>& args,
390
                                const char* syscall, const char* dest,
391
                                size_t len, enum encoding enc, uv_fs_cb after,
392
                                Func fn, Args... fn_args);
393
394
// Returns nullptr if the operation fails from the start.
395
template <typename Func, typename... Args>
396
inline FSReqBase* AsyncCall(Environment* env,
397
                            FSReqBase* req_wrap,
398
                            const v8::FunctionCallbackInfo<v8::Value>& args,
399
                            const char* syscall, enum encoding enc,
400
                            uv_fs_cb after, Func fn, Args... fn_args);
401
402
// Template counterpart of SYNC_CALL, except that it only puts
403
// the error number and the syscall in the context instead of
404
// creating an error in the C++ land.
405
// ctx must be checked using value->IsObject() before being passed.
406
template <typename Func, typename... Args>
407
inline int SyncCall(Environment* env, v8::Local<v8::Value> ctx,
408
                    FSReqWrapSync* req_wrap, const char* syscall,
409
                    Func fn, Args... args);
410
411
}  // namespace fs
412
413
}  // namespace node
414
415
#endif  // defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
416
417
#endif  // SRC_NODE_FILE_H_