FreeNOS
DirectoryScanner.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2015 Niek Linnenbank
3  *
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program. If not, see <http://www.gnu.org/licenses/>.
16  */
17 
18 #include <stdio.h>
19 #include <stdlib.h>
20 #include <dirent.h>
21 #include <string.h>
22 #include <errno.h>
23 #include <String.h>
24 #include "ExternalTest.h"
25 #include "TestSuite.h"
26 #include "DirectoryScanner.h"
27 
28 DirectoryScanner::DirectoryScanner(int argc, char **argv)
29 {
30  m_argc = argc;
31  m_argv = argv;
32 }
33 
35 {
37 }
38 
39 int DirectoryScanner::scan(const char *path)
40 {
41  DIR *d;
42  struct dirent *dent;
43  char subPath[255];
44 
45  // Attempt to open the target directory.
46  if (!(d = opendir(path)))
47  {
48  printf("%s: failed to open '%s': %s\r\n",
49  m_argv[0], path, strerror(errno));
50  return EXIT_FAILURE;
51  }
52  // Read directory.
53  while ((dent = readdir(d)))
54  {
55  snprintf(subPath, sizeof(subPath), "%s/%s", path, dent->d_name);
56  String str = subPath;
57 
58  // Check filetype
59  switch (dent->d_type)
60  {
61  // Directory
62  case DT_DIR:
63  if (dent->d_name[0] != '.')
64  scan(subPath);
65  break;
66 
67  // Regular file
68  case DT_REG:
69  if (str.endsWith((const char *)"Test"))
70  {
71  ExternalTest *test = new ExternalTest(subPath, m_argc, m_argv);
72  if (!m_externalTests.insert(test))
73  {
74  printf("%s: failed to add test '%s' to internal Index\n",
75  m_argv[0], path);
76  return EXIT_FAILURE;
77  }
78  }
79  break;
80 
81  default:
82  break;
83  }
84  }
85  // Close it.
86  closedir(d);
87  return EXIT_SUCCESS;
88 }
EXIT_FAILURE
#define EXIT_FAILURE
Unsuccessful termination.
Definition: stdlib.h:36
errno
C int errno
The lvalue errno is used by many functions to return error values.
string.h
String
Abstraction of strings.
Definition: String.h:41
closedir
int closedir(DIR *dirp)
Close a directory stream.
Definition: closedir.cpp:22
DirectoryScanner::scan
int scan(const char *path)
Scan filesystem path for tests.
Definition: DirectoryScanner.cpp:39
readdir
struct dirent * readdir(DIR *dirp)
Read a directory.
Definition: readdir.cpp:21
DirectoryScanner::m_externalTests
Index< ExternalTest, MaximumExternalTests > m_externalTests
External tests that are detected.
Definition: DirectoryScanner.h:76
DT_REG
#define DT_REG
This is a regular file.
Definition: dirent.h:50
ExternalTest
Represents external test program.
Definition: ExternalTest.h:34
opendir
DIR * opendir(const char *dirname)
Open directory associated with file descriptor.
Definition: opendir.cpp:27
DirectoryScanner.h
dirent.h
Index::deleteAll
void deleteAll()
Removes and delete()'s all items.
Definition: Index.h:166
TestSuite.h
printf
int printf(const char *format,...)
Output a formatted string to standard output.
Definition: printf.cpp:22
dirent
Represents a directory entry.
Definition: dirent.h:64
stdio.h
strerror
char * strerror(int errnum)
The strerror function maps the number in errnum to a message string.
Definition: strerror.cpp:20
DirectoryScanner::DirectoryScanner
DirectoryScanner(int argc, char **argv)
Constructor.
Definition: DirectoryScanner.cpp:28
DirectoryScanner::m_argc
int m_argc
Program argument count.
Definition: DirectoryScanner.h:70
DIR
A type representing a directory stream.
Definition: dirent.h:94
DT_DIR
#define DT_DIR
This is a directory.
Definition: dirent.h:44
DirectoryScanner::~DirectoryScanner
~DirectoryScanner()
Destructor.
Definition: DirectoryScanner.cpp:34
EXIT_SUCCESS
#define EXIT_SUCCESS
Successful termination.
Definition: stdlib.h:33
String.h
String::endsWith
bool endsWith(const String &suffix) const
Tests if this String ends with the specified suffix.
Definition: String.cpp:210
dirent::d_name
char d_name[DIRLEN]
Name of entry.
Definition: dirent.h:67
DirectoryScanner::m_argv
char ** m_argv
Program argument values.
Definition: DirectoryScanner.h:73
stdlib.h
Index::insert
virtual bool insert(Size &position, T *item)
Adds the given item, if possible.
Definition: Index.h:60
ExternalTest.h
snprintf
int snprintf(char *buffer, unsigned int size, const char *fmt,...)
Write a formatted string into a buffer.
Definition: snprintf.cpp:22
errno.h
dirent::d_type
u8 d_type
Type of file.
Definition: dirent.h:70