/
scan.c
164 lines (136 loc) · 3.94 KB
/
scan.c
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
160
161
162
163
164
/*
* scan.c - This module provides scans files and inserts them
* into the index.
*/
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <assert.h>
#include <ctype.h>
#include <string.h>
#include <inttypes.h> // for PRIu64
#include "index.h"
#include "diskimg.h"
#include "fileops.h"
#include "scan.h"
#include "debug.h"
#include "proj1/direntv6.h"
static uint64_t numfiles = 0;
static uint64_t numwords = 0;
static uint64_t numchars = 0;
static uint64_t numdups = 0;
static uint64_t numdirs = 0;
static uint64_t numdirents = 0;
#define MAX_WORD_SIZE 64
/*
* Tokenize the specified file and place it in the index.
*/
int
Scan_File(char *inpathname, Index *ind, Pathstore *store, int discardDups)
{
// Save the pathname in the store
char *pathname = Pathstore_path(store, inpathname,discardDups);
if (pathname == NULL) {
numdups++;
DPRINTF('s',("Scan_Pathname discard dup (%s)\n", inpathname));
return 0;
}
numfiles++;
DPRINTF('s', ("Scan_Pathname(%s)\n", pathname));
int fd = Fileops_open(pathname);
if (fd < 0) {
fprintf(stderr, "Can't open pathname %s\n", pathname);
return -1;
}
int size = 0;
int inum = Fileops_getinumber(fd, &size);
printf("Size: %d\n", size);
if(inum < 0) {Fileops_close(fd); return 0; }
unsigned char buf[DISKIMG_SECTOR_SIZE];
int blockNo = Fileops_getblockno(fd);
int bytesMoved = Fileops_getblock(inum, blockNo, buf);
int ch = Fileops_getchar(fd, inum, size, buf, &bytesMoved);
numchars++;
while (!(ch < 0)) { // Process words until we reach the end of the file
while (!isalpha(ch)) { // Skip any leading non-alpha characters
ch = Fileops_getchar(fd, inum, size, buf, &bytesMoved);
if (ch < 0) {Fileops_close(fd); return 0; }
numchars++;
}
// Found a word - record it in the index.
int offset = Fileops_tell(fd);
int pos = 0;
// read off the word until we hit the end of the word buffer
// or the end of the file or we hit an non-alpah characters
char word[MAX_WORD_SIZE+1];
while ((pos < MAX_WORD_SIZE) && !(ch < 0) && isalpha(ch)) {
word[pos++] = ch;
ch = Fileops_getchar(fd, inum, size, buf, &bytesMoved);
numchars++;
}
numwords++;
word[pos] = 0; // terminate string
bool ok = Index_StoreEntry(ind, word, pathname, offset);
assert(ok);
}
Fileops_close(fd);
return 0;
}
int
Scan_TreeAndIndex(char *pathname, Index *ind, Pathstore *store,int discardDups)
{
const uint32_t MAXPATH = 1024;
if (Fileops_isfile(pathname)) {
return Scan_File(pathname, ind, store, discardDups);
}
/* Not a file must be directory, process all entries in the directory */
if (strlen(pathname) > MAXPATH-16) {
fprintf(stderr, "Too deep of directories %s\n", pathname);
return -1;
}
numdirs++;
int dirfd = Fileops_open(pathname);
if (dirfd < 0) {
fprintf(stderr, "Can't open pathname %s\n", pathname);
return -1;
}
if (pathname[1] == 0) {
/* pathame == "/" */
pathname++; /* Delete extra / character */
}
int ret;
while (1) {
struct direntv6 dirent;
ret = Fileops_read(dirfd, (char *)&dirent, sizeof(struct direntv6));
if (ret == 0) {
/* Done with directory */
break;
}
if (ret != sizeof(struct direntv6)) {
fprintf(stderr, "Error reading directory %s\n", pathname);
ret = -1;
break;
}
numdirents++;
char *n = dirent.d_name;
if (n[0] == '.') {
if ((n[1] == 0) || ((n[1] == '.') && (n[2] == 0))) {
/* Skip over "." and ".." */
continue;
}
}
char nextpath[MAXPATH];
sprintf(nextpath, "%s/%s",pathname, n);
Scan_TreeAndIndex(nextpath, ind, store, discardDups);
}
Fileops_close(dirfd);
return ret;
}
void
Scan_dumpstats(FILE *file)
{
fprintf(file,
"Scan: %"PRIu64" files, %"PRIu64" words, %"PRIu64" characters, "
"%"PRIu64" directories, %"PRIu64" dirents, %"PRIu64" duplicates\n",
numfiles, numwords, numchars, numdirs, numdirents, numdups);
}