1 // Copyright (C) 2018 The Android Open Source Project
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //      http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #ifndef IORAP_SRC_INODE2FILENAME_SYSTEM_CALL_H_
16 #define IORAP_SRC_INODE2FILENAME_SYSTEM_CALL_H_
17 
18 #include <fruit/fruit.h>
19 
20 #include <dirent.h>
21 #include <sys/types.h>
22 #include <sys/stat.h>
23 #include <unistd.h>
24 
25 // Abstract out the system calls behind a virtual interface:
26 // This enables us to use dependency injection to provide mock implementations
27 // during tests.
28 struct SystemCall {
29   // stat(2)
30   virtual int stat(const char *pathname, struct stat *statbuf) = 0;
31 
32   // opendir(3)
33   virtual DIR *opendir(const char *name) = 0;
34 
35   // readdir(3)
36   virtual struct dirent *readdir(DIR *dirp) = 0;
37 
38   // closedir(3)
39   virtual int closedir(DIR *dirp) = 0;
40 
~SystemCallSystemCall41   virtual ~SystemCall() {}
42 };
43 
44 // "Live" implementation that calls down to libc.
45 struct SystemCallImpl : public SystemCall {
46   // Marks this constructor as the one to use for injection.
47   INJECT(SystemCallImpl()) = default;
48 
49   // stat(2)
statSystemCallImpl50   virtual int stat(const char *pathname, struct stat *statbuf) override {
51     return ::stat(pathname, statbuf);
52   }
53 
54   // opendir(3)
opendirSystemCallImpl55   virtual DIR *opendir(const char *name) override {
56     return ::opendir(name);
57   }
58 
59   // readdir(3)
readdirSystemCallImpl60   virtual struct dirent *readdir(DIR *dirp) override {
61     return ::readdir(dirp);
62   }
63 
64   // closedir(3)
closedirSystemCallImpl65   virtual int closedir(DIR *dirp) override {
66     return ::closedir(dirp);
67   }
68 
~SystemCallImplSystemCallImpl69   virtual ~SystemCallImpl() {}
70 };
71 
72 #endif  // IORAP_SRC_INODE2FILENAME_SYSTEM_CALL_H_
73 
74