LCOV - code coverage report
Current view: top level - src - getdents.cc (source / functions) Hit Total Coverage
Test: lcov_final.info Lines: 65 65 100.0 %
Date: 2021-10-30 22:51:46 Functions: 15 15 100.0 %
Branches: 0 0 -

           Branch data     Line data    Source code
       1                 :            : #include <unistd.h>
       2                 :            : #include <sys/syscall.h>
       3                 :            : #include <dirent.h>
       4                 :            : #include <napi.h>
       5                 :            : 
       6                 :            : class Getdents : public Napi::ObjectWrap<Getdents>
       7                 :            : {
       8                 :            : public:
       9                 :            :     Getdents(const Napi::CallbackInfo& info);
      10                 :            :     ~Getdents();
      11                 :            : 
      12                 :            :     static Napi::Object Initialize(Napi::Env env, Napi::Object exports);
      13                 :            : 
      14                 :            :     void Next(const Napi::CallbackInfo& info);
      15                 :            :     Napi::Value NextSync(const Napi::CallbackInfo& info);
      16                 :            : 
      17                 :            :     Napi::Value GetFD(const Napi::CallbackInfo& info);
      18                 :            :     void SetFD(const Napi::CallbackInfo& info, const Napi::Value& value);
      19                 :            : 
      20                 :            : private:
      21                 :            :         friend class GetdentsAsyncWorker;
      22                 :            : 
      23                 :            :     int NextSync(uint8_t *dirp, unsigned int count);
      24                 :            : 
      25                 :            :     unsigned int fd;
      26                 :            : };
      27                 :            : 
      28                 :          2 : Getdents::Getdents(const Napi::CallbackInfo& info) :
      29                 :            :     Napi::ObjectWrap<Getdents>(info),
      30                 :          2 :     fd(0)
      31                 :            : {
      32                 :          2 : }
      33                 :            : 
      34                 :            : //LCOV_EXCL_START
      35                 :            : Getdents::~Getdents()
      36                 :            : {
      37                 :            : }
      38                 :            : //LCOV_EXCL_STOP
      39                 :            : 
      40                 :          4 : Napi::Error ErrnoError(const Napi::Env& env, const int errnum, const char *msg)
      41                 :            : {
      42                 :          4 :     char buf[1024] = {0};
      43                 :            : #if defined(__GLIBC__) && defined(_GNU_SOURCE)
      44                 :          4 :     auto errmsg = strerror_r(errnum, buf, sizeof(buf));
      45                 :            :     static_assert(std::is_same<decltype(errmsg), char*>::value,
      46                 :            :                   "strerror_r must return char*");
      47                 :            : #else
      48                 :            :     char *errmsg = nullptr;
      49                 :            :     auto r = strerror_r(errnum, buf, sizeof(buf));
      50                 :            :     static_assert(std::is_same<decltype(r), int>::value,
      51                 :            :                   "strerror_r must return int");
      52                 :            :     if (r == 0)
      53                 :            :     {
      54                 :            :         errmsg = buf;
      55                 :            :     }
      56                 :            : #endif
      57                 :            :     return Napi::Error::New(env,
      58                 :          8 :         std::string(msg) + ": " + (errmsg ? errmsg : std::to_string(errnum)));
      59                 :            : }
      60                 :            : 
      61                 :          4 : Napi::Error GetdentsErrnoError(const Napi::Env& env, const int errnum)
      62                 :            : {
      63                 :          4 :     return ErrnoError(env, errnum, "getdents64 failed");
      64                 :            : }
      65                 :            : 
      66                 :         18 : int Getdents::NextSync(uint8_t *dirp, unsigned int count)
      67                 :            : {
      68                 :         18 :     return syscall(SYS_getdents64, fd, dirp, count);
      69                 :            : }
      70                 :            : 
      71                 :          9 : Napi::Value Getdents::NextSync(const Napi::CallbackInfo& info)
      72                 :            : {
      73                 :          9 :     Napi::Buffer<uint8_t> b = info[0].As<Napi::Buffer<uint8_t>>();
      74                 :          9 :     int r = NextSync(b.Data(), b.Length());
      75                 :          9 :     if (r < 0)
      76                 :            :     {
      77                 :          2 :         throw GetdentsErrnoError(info.Env(), errno);
      78                 :            :     }
      79                 :          7 :     return Napi::Number::New(info.Env(), r);
      80                 :            : }
      81                 :            : 
      82                 :            : class GetdentsAsyncWorker : public Napi::AsyncWorker
      83                 :            : {
      84                 :            : public:
      85                 :          9 :     GetdentsAsyncWorker(Getdents *getdents,
      86                 :            :                         const Napi::Function& callback,
      87                 :          9 :                         const Napi::Buffer<uint8_t>& buffer) :
      88                 :            :         Napi::AsyncWorker(callback),
      89                 :          9 :                 getdents(getdents), // getdents_ref keeps this around
      90                 :          9 :                 getdents_ref(Napi::Persistent(getdents->Value())),
      91                 :          9 :         buffer_ref(Napi::Persistent(buffer)),
      92                 :          9 :         dirp(buffer.Data()),
      93                 :         18 :         count(buffer.Length())
      94                 :            :         {
      95                 :          9 :         }
      96                 :            : 
      97                 :            : protected:
      98                 :          9 :     void Execute() override
      99                 :            :     {
     100                 :          9 :         result = getdents->NextSync(dirp, count);
     101                 :          9 :         if (result < 0)
     102                 :            :         {
     103                 :          2 :             errnum = errno;
     104                 :            :         }
     105                 :          9 :     }
     106                 :            : 
     107                 :          9 :         void OnOK() override
     108                 :            :         {
     109                 :          9 :                 Napi::Env env = Env();
     110                 :            : 
     111                 :         36 :                 Callback().MakeCallback(
     112                 :          9 :                         Receiver().Value(),
     113                 :            :                         {
     114                 :         11 :                                 result < 0 ? GetdentsErrnoError(env, errnum).Value() : env.Null(),
     115                 :         18 :                                 Napi::Number::New(env, result)
     116                 :            :                         });
     117                 :          9 :         }
     118                 :            : 
     119                 :            : private: Getdents *getdents;
     120                 :            :         Napi::ObjectReference getdents_ref;
     121                 :            :     Napi::Reference<Napi::Buffer<uint8_t>> buffer_ref;
     122                 :            :     uint8_t *dirp;
     123                 :            :     unsigned int count;
     124                 :            :         int result;
     125                 :            :     int errnum;
     126                 :            : };
     127                 :            : 
     128                 :          9 : void Getdents::Next(const Napi::CallbackInfo& info)
     129                 :            : {
     130                 :            :     (new GetdentsAsyncWorker(this,
     131                 :          9 :                                                          info[1].As<Napi::Function>(),
     132                 :          9 :                                                          info[0].As<Napi::Buffer<uint8_t>>()))
     133                 :          9 :                 ->Queue();
     134                 :          9 : }
     135                 :            : 
     136                 :          8 : Napi::Value Getdents::GetFD(const Napi::CallbackInfo& info)
     137                 :            : {
     138                 :          8 :     return Napi::Number::New(info.Env(), fd);
     139                 :            : }
     140                 :            : 
     141                 :         14 : void Getdents::SetFD(const Napi::CallbackInfo& info, const Napi::Value& value)
     142                 :            : {
     143                 :         14 :     fd = value.As<Napi::Number>();
     144                 :         14 : }
     145                 :            : 
     146                 :          1 : Napi::Object Getdents::Initialize(Napi::Env env, Napi::Object exports)
     147                 :            : {
     148                 :          1 :     exports.Set("Getdents", DefineClass(env, "Getdents",
     149                 :            :     {
     150                 :          1 :         InstanceMethod<&Getdents::Next>("next"),
     151                 :          1 :         InstanceMethod<&Getdents::NextSync>("nextSync"),
     152                 :          1 :         InstanceAccessor<&Getdents::GetFD, &Getdents::SetFD>("fd"),
     153                 :          1 :         StaticValue("DT_BLK", Napi::Number::New(env, DT_BLK)),
     154                 :          1 :         StaticValue("DT_CHR", Napi::Number::New(env, DT_CHR)),
     155                 :          1 :         StaticValue("DT_DIR", Napi::Number::New(env, DT_DIR)),
     156                 :          1 :         StaticValue("DT_FIFO", Napi::Number::New(env, DT_FIFO)),
     157                 :          1 :         StaticValue("DT_LNK", Napi::Number::New(env, DT_LNK)),
     158                 :          1 :         StaticValue("DT_REG", Napi::Number::New(env, DT_REG)),
     159                 :          1 :         StaticValue("DT_SOCK", Napi::Number::New(env, DT_SOCK)),
     160                 :          1 :         StaticValue("DT_UNKNOWN", Napi::Number::New(env, DT_UNKNOWN)),
     161                 :          1 :         StaticValue("alignment", Napi::Number::New(env, __alignof__(struct dirent)))
     162                 :            :     }));
     163                 :            : 
     164                 :          1 :     return exports;
     165                 :            : }
     166                 :            : 
     167                 :          1 : Napi::Object Initialize(Napi::Env env, Napi::Object exports)
     168                 :            : {
     169                 :          1 :     return Getdents::Initialize(env, exports);
     170                 :            : }
     171                 :            : 
     172                 :          2 : NODE_API_MODULE(getdents, Initialize)

Generated by: LCOV version 1.14