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