-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstorage.cpp
More file actions
159 lines (139 loc) · 3.68 KB
/
storage.cpp
File metadata and controls
159 lines (139 loc) · 3.68 KB
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
/* ************************************************************************ *
* Written by Alex de Kruijff 21 April 2009 *
* ************************************************************************ *
* This source was written with a tabstop every four characters *
* In vi type :set ts=4 *
* ************************************************************************ */
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fts.h>
#include <stdio.h>
#include <unistd.h>
#include "configure.h"
#include "toolkit.h"
#include "filename.h"
#include "filegroup.h"
#include "storage.h"
#include <new>
/* ************************************************************************ */
void removeDirectoryContence(char *storageFile)
{
// Remove any regular files that may exist in the directory
char *const paths[2] = {storageFile, NULL};
FTS *ftsp = fts_open(paths, FTS_PHYSICAL, NULL);
FTSENT *ftsent;
while((ftsent = fts_read(ftsp)) != NULL)
if (ftsent->fts_info & FTS_F)
unlink(ftsent->fts_accpath);
fts_close(ftsp);
}
Storage::Storage(const char *program) throw (std::bad_alloc)
: stream(NULL), s(NULL)
{
// Initial saving space
int error;
pid_t storagePid = getpid();
storageBase = strlen(TEMP_STORAGE_DIR);
storageBase += strlen(program);
storageBase += digits(storagePid);
storageBase += 2;
storageFile = new char[storageBase + 11]; // throws bad_alloc
try
{
line = new char[lineCapacity = PATH_INIT]; // throws bad_alloc
}
catch(const std::bad_alloc &e)
{
delete[] storageFile;
throw(e);
}
// Populating storageFile
sprintf(storageFile, "%s/%s", TEMP_STORAGE_DIR, program);
mkdir(storageFile, S_IRWXU | S_IRWXG | S_IRWXO);
if ((error = chmod(storageFile, S_IRWXU | S_IRWXG | S_IRWXO)) < 0)
return;
sprintf(storageFile + strlen(storageFile), "/%u", storagePid);
if (mkdir(storageFile, S_IRWXU | S_IRWXG | S_IRWXO))
removeDirectoryContence(storageFile);
if ((error = chmod(storageFile, S_IRWXU | S_IRWXG | S_IRWXO)) < 0)
return;
sprintf(storageFile + strlen(storageFile), "/");
storageBase = strlen(storageFile);
}
Storage::~Storage() throw()
{
storageFile[storageBase] = 0;
removeDirectoryContence(storageFile);
rmdir(storageFile);
delete[] storageFile;
delete[] line;
}
int Storage::open(size_t filesize) throw()
{
size_t n = digits(filesize);
char *end = storageFile + storageBase;
char *ptr = end + n;
#ifdef DEBUG
if (ptr >= storageFile + storageBase + 11)
{
fprintf(stderr, "%s:%d pointer out of range\n",
__FILE__, __LINE__);
exit(EXIT_FAILURE);
}
#endif
*ptr = 0;
while(end != ptr)
{
*--ptr = filesize % 10;
filesize /= 10;
}
if (stream != NULL)
close();
else
counter = 0;
stream = fopen(storageFile, "r");
}
const char *Storage::read(struct stat &s) throw (std::bad_alloc)
{
fgetline(line, lineCapacity, stream); // throws bad_alloc
if (sscanf(line, "%u", s.st_dev) == EOF)
return NULL;
char *str = strstr(str, " ");
if (sscanf(++str, "%u", s.st_ino) == EOF)
return NULL;
str = strstr(str, " ");
++counter;
return ++str;
}
size_t Storage::close() throw()
{
fclose(stream);
stream = NULL;
return counter;
}
void Storage::clean() const throw()
{
unlink(storageFile);
}
int Storage::visit(Holder &holder) { return 0; }
int Storage::visit(SizeGroup &sizegroup)
{
sprintf(storageFile + storageBase, "%u", sizegroup.getFileSize());
if (stream != NULL)
close();
else
counter = 0;
stream = fopen(storageFile, "a");
return 0;
}
int Storage::visit(FileGroup &filegroup)
{
s = &filegroup.stat();
return 0;
}
void Storage::visit(Filename &filename)
{
fprintf(stream, "%u %u %s\n", s->st_dev, s->st_ino, filename.data());
++counter;
}