-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdir.cc
142 lines (126 loc) · 3.12 KB
/
dir.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
/************************************************************************
*
* Filesystem stress and verify
*
* Authors: Goswin von Brederlow <[email protected]>
* Bernd Schubert <[email protected]>
*
* Copyright (C) 2007 Q-leap Networks, Goswin von Brederlow
* 2010 DataDirect Networks, Bernd Schubert
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
* USA
*
************************************************************************/
#include <unistd.h>
#include <getopt.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <stdio.h>
#include <errno.h>
#include <sys/statvfs.h>
#include <stdint.h>
#include <time.h>
#include <fcntl.h>
#include <iostream>
#include <sstream>
#include <cassert>
#include <vector>
#include <cstring>
#include "fstest.h"
#include "dir.h"
using namespace std;
Dir::Dir(Dir *parent, int level) : parent(parent)
{
this->max_files = level * level;
fs = parent->fs;
next = parent->sub;
sub = NULL;
files = NULL;
num_files = 0;
dirname = "d000";
dirname[1] = '0' + level / 10;
dirname[2] = '0' + level % 10;
parent->sub = this;
string dirpath = path();
cout << "Creating dir " << dirpath << endl;
if (mkdir(dirpath.c_str(), 0700) != 0) {
cout << "Creating dir " << path();
perror(": ");
EXIT(1);
}
fs->all_dirs.push_back(this);
fs->active_dirs.push_back(this);
while(--level > 0) {
new Dir(this, level);
}
}
Dir::Dir(string _path, Filesystem * fs): fs(fs)
{
parent = NULL;
next = NULL;
sub = NULL;
files = NULL;
num_files = 1;
root_path = _path;
if (mkdir(path().c_str(), 0700) != 0) {
cout << "Creating working dir " << path();
perror(": ");
EXIT(1);
}
fs->all_dirs.push_back(this);
fs->active_dirs.push_back(this);
}
Dir::~Dir(void)
{
cout << "~Dir(" << path() << ")\n";
if (next != NULL) delete next;
if (sub != NULL) delete sub;
if (files != NULL) {
files->delete_all();
}
int res = rmdir(path().c_str());
if (res != 0 && errno != ENOENT) {
perror(path().c_str());
EXIT(1);
}
}
void Dir::add_file(File *file)
{
file->link(files);
this->files = file;
this->num_files++;
}
void Dir::remove_file(File *file)
{
this->num_files--;
if (this->files == file)
files = files->get_next();
file->unlink();
}
string Dir::path(void) const
{
if (parent == NULL) {
return root_path;
} else {
// recursion!
return parent->path() + dirname + "/";
}
}
uint16_t Dir::get_num_files(void) const
{
RETURN(num_files);
}