Skip to content

Commit 7ce8283

Browse files
committed
C++-ify evict.
Only functional change is to exit with exit status 1 on missing directory argument. After this all users of lib.{misc,error}.c are C++-ified and and we can improve/simplify the code there as well.
1 parent 061d77f commit 7ce8283

File tree

2 files changed

+37
-35
lines changed

2 files changed

+37
-35
lines changed

judge/Makefile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ judgehost: $(TARGETS) $(SUBST_FILES)
1313
$(SUBST_FILES): %: %.in $(TOPDIR)/paths.mk
1414
$(substconfigvars)
1515

16-
evict: evict.c $(LIBHEADERS) $(LIBSOURCES)
17-
$(CC) $(CFLAGS) -o $@ $< $(LIBSOURCES)
16+
evict: evict.cc $(LIBHEADERS) $(LIBSOURCES)
17+
$(CXX) $(CXXFLAGS) -o $@ $< $(LIBSOURCES)
1818

1919
runguard: runguard.cc $(LIBHEADERS) $(LIBSOURCES) $(TOPDIR)/etc/runguard-config.h
2020
$(CXX) $(CXXFLAGS) -o $@ $< $(LIBSOURCES) $(LIBCGROUP)

judge/evict.c renamed to judge/evict.cc

Lines changed: 35 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,21 @@
11
#include "config.h"
22

3+
#include <iostream>
4+
#include <string>
5+
#include <vector>
6+
37
#include <dirent.h>
48
#include <fcntl.h>
59
#include <getopt.h>
6-
#include <malloc.h>
7-
#include <stdlib.h>
8-
#include <string.h>
910
#include <unistd.h>
1011
#include <sys/stat.h>
12+
#include <cstring>
13+
#include <cstdlib>
14+
15+
extern "C" {
1116
#include "lib.error.h"
1217
#include "lib.misc.h"
18+
}
1319

1420
#define PROGRAM "evict"
1521
#define VERSION DOMJUDGE_VERSION "/" REVISION
@@ -30,27 +36,23 @@ struct option const long_opts[] = {
3036

3137
void usage()
3238
{
33-
printf("\
34-
Usage: %s [OPTION]... DIRECTORY\n\
35-
Evicts all files in a directory tree from the kernel filesystem cache.\n\
36-
\n\
37-
-v, --verbose display some extra warnings and information\n\
38-
--help display this help and exit\n\
39-
--version output version information and exit\n\
40-
\n", progname);
39+
std::cout << "Usage: " << progname << " [OPTION]... DIRECTORY" << std::endl
40+
<< "Evicts all files in a directory tree from the kernel filesystem cache." << std::endl << std::endl
41+
<< " -v, --verbose display some extra warnings and information" << std::endl
42+
<< " --help display this help and exit" << std::endl
43+
<< " --version output version information and exit" << std::endl << std::endl;
4144
exit(0);
4245
}
4346

44-
void evict_directory(char *dirname) {
47+
void evict_directory(const std::string& dirname) {
4548
DIR *dir;
4649
struct dirent *entry;
47-
int fd;
48-
char *entry_path;
50+
int fd = -1;
4951
struct stat s;
5052

51-
dir = opendir(dirname);
53+
dir = opendir(dirname.c_str());
5254
if (dir != NULL) {
53-
if (be_verbose) logmsg(LOG_INFO, "Evicting all files in directory: %s", dirname);
55+
if (be_verbose) logmsg(LOG_INFO, "Evicting all files in directory: %s", dirname.c_str());
5456

5557
/* Read everything in the directory */
5658
while ( (entry = readdir(dir)) != NULL ) {
@@ -60,47 +62,47 @@ void evict_directory(char *dirname) {
6062
}
6163

6264
/* Construct the full file path */
63-
entry_path = allocstr("%s/%s", dirname, entry->d_name);
64-
fd = open(entry_path, O_RDONLY, 0);
65+
std::string entry_path = dirname + "/" + entry->d_name;
66+
fd = open(entry_path.c_str(), O_RDONLY, 0);
6567
if (fd == -1) {
66-
warning(errno, "Unable to open file: %s", entry_path);
67-
goto entry_done;
68+
warning(errno, "Unable to open file: %s", entry_path.c_str());
69+
continue;
6870
}
6971

7072
if (fstat(fd, &s) < 0) {
71-
if (be_verbose) logerror(errno, "Unable to stat file/directory: %s\n", entry_path);
72-
goto entry_done;
73+
if (be_verbose) logerror(errno, "Unable to stat file/directory: %s\n", entry_path.c_str());
74+
if ( close(fd)!=0 ) {
75+
warning(errno, "Unable to close file: %s", entry_path.c_str());
76+
}
77+
continue;
7378
}
7479
if (S_ISDIR(s.st_mode)) {
7580
/* Recurse into subdirectories */
7681
evict_directory(entry_path);
7782
} else {
7883
/* evict this file from the cache */
7984
if (posix_fadvise(fd, 0, 0, POSIX_FADV_DONTNEED)) {
80-
warning(errno, "Unable to evict file: %s\n", entry_path);
85+
warning(errno, "Unable to evict file: %s\n", entry_path.c_str());
8186
} else {
82-
if (be_verbose) logmsg(LOG_DEBUG, "Evicted file: %s", entry_path);
87+
if (be_verbose) logmsg(LOG_DEBUG, "Evicted file: %s", entry_path.c_str());
8388
}
8489
}
85-
entry_done:
8690

87-
if ( fd!=-1 && close(fd)!=0 ) {
88-
warning(errno, "Unable to close file: %s", entry_path);
91+
if ( close(fd)!=0 ) {
92+
warning(errno, "Unable to close file: %s", entry_path.c_str());
8993
}
90-
free(entry_path);
9194
}
9295
if ( closedir(dir)!=0 ) {
93-
warning(errno, "Unable to close directory: %s", dirname);
96+
warning(errno, "Unable to close directory: %s", dirname.c_str());
9497
}
9598
} else {
96-
warning(errno, "Unable to open directory: %s", dirname);
99+
warning(errno, "Unable to open directory: %s", dirname.c_str());
97100
}
98101
}
99102

100103
int main(int argc, char *argv[])
101104
{
102105
int opt;
103-
char* dirname;
104106

105107
progname = argv[0];
106108

@@ -129,11 +131,11 @@ int main(int argc, char *argv[])
129131

130132
if ( argc<=optind ) {
131133
logmsg(LOG_ERR, "no directory specified");
132-
return 0;
134+
return 1;
133135
}
134136

135137
/* directory to evict */
136-
dirname = argv[optind];
138+
std::string dirname = argv[optind];
137139

138140
evict_directory(dirname);
139141

0 commit comments

Comments
 (0)