• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright (C) 2021 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 #include <vector>
16 
17 #include <ditto/logger.h>
18 #include <ditto/read_directory.h>
19 #include <ditto/shared_variables.h>
20 
21 namespace dittosuite {
22 
ReadDirectory(SyscallInterface & syscall,int repeat,const std::string & directory_name,int output_key)23 ReadDirectory::ReadDirectory(SyscallInterface& syscall, int repeat,
24                              const std::string& directory_name, int output_key)
25     : Instruction(syscall, kName, repeat),
26       directory_name_(GetAbsolutePath() + directory_name),
27       output_key_(output_key) {}
28 
RunSingle()29 void ReadDirectory::RunSingle() {
30   std::vector<std::string> output;
31 
32   DIR* directory = syscall_.OpenDir(directory_name_);
33 
34   if (directory == nullptr) {
35     PLOGF("Cannot open \"" + directory_name_ + "\"");
36   }
37 
38   struct dirent* entry;
39   while ((entry = syscall_.ReadDir(directory)) != nullptr) {
40     // Only collect regular files
41     if (entry->d_type == DT_REG) {
42       std::string path_name = directory_name_;
43       // Add a slash if the current directory name does not end with a slash
44       if (!path_name.empty() && path_name.back() != '/') {
45         path_name += "/";
46       }
47       path_name += entry->d_name;
48       output.push_back(path_name);
49     }
50   }
51   SharedVariables::Set(output_key_, output);
52 
53   syscall_.CloseDir(directory);
54 }
55 
56 }  // namespace dittosuite
57