-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathfind.java
More file actions
188 lines (161 loc) · 5.86 KB
/
find.java
File metadata and controls
188 lines (161 loc) · 5.86 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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
import java.util.ArrayList;
/**
* @author Ivan Ramyk
*/
public class find
{
/**
* The name of this program.
* This is the program name that is used
* when displaying error messages.
*/
public static String PROGRAM_NAME = "find" ;
/**
* Lists that path name and all files in all directories.
* @exception java.lang.Exception if an exception is thrown
* by an underlying operation
*/
private static void printAllInPathRec(String path) throws Exception {
int fd = Kernel.open( path , Kernel.O_RDONLY ) ;
if( fd < 0 )
{
return;
}
// print a heading for this directory
System.out.println(path) ;
// create a directory entry structure to hold data as we read
DirectoryEntry directoryEntry = new DirectoryEntry() ;
// while we can read, print the information on each entry
while( true )
{
// read an entry; quit loop if error or nothing read
int status = Kernel.readdir( fd , directoryEntry ) ;
if( status <= 0 )
break ;
// get the name from the entry
String entryName = directoryEntry.getName() ;
// call stat() to get info about the file
Stat stat = new Stat();
status = Kernel.stat( path + "/" + entryName , stat ) ;
if( status < 0 )
{
Kernel.perror( PROGRAM_NAME ) ;
Kernel.exit( 1 ) ;
}
short type = (short)( stat.getMode() & Kernel.S_IFMT ) ;
if( type == Kernel.S_IFDIR )
{
if (!entryName.equals(".") && !entryName.equals("..")) {
if (path.charAt(path.length() - 1) == '/')
printAllInPathRec(path + entryName);
else
printAllInPathRec(path + '/' + entryName);
}
} else {
if (path.charAt(path.length() - 1) == '/')
System.out.println(path + entryName);
else
System.out.println(path + "/" + entryName);
}
}
Kernel.close( fd ) ;
}
private static void printAllInPathQueue(String rootPath) throws Exception {
ArrayList<String> queue = new ArrayList<>();
queue.add(rootPath);
int currentPathIndex = 0;
while (queue.size() > currentPathIndex) {
String path = queue.get(currentPathIndex);
currentPathIndex++;
//check if file is a dir
int status = 0 ;
// stat the name to get information about the file or directory
Stat stat = new Stat() ;
status = Kernel.stat( path , stat ) ;
if( status < 0 )
{
Kernel.perror( PROGRAM_NAME ) ;
Kernel.exit( 1 ) ;
}
// mask the file type from the mode
short type = (short)( stat.getMode() & Kernel.S_IFMT ) ;
// if name is a regular file, just print path
if (type != Kernel.S_IFDIR)
{
System.out.println(path) ;
continue;
}
int fd = Kernel.open( path , Kernel.O_RDONLY ) ;
if( fd < 0 )
{
return;
}
System.out.println(path) ;
// create a directory entry structure to hold data as we read
DirectoryEntry directoryEntry = new DirectoryEntry() ;
// while we can read, print the information on each entry
while( true )
{
// read an entry; quit loop if error or nothing read
status = Kernel.readdir( fd , directoryEntry ) ;
if( status <= 0 )
break ;
// get the name from the entry
String entryName = directoryEntry.getName() ;
if (!entryName.equals(".") && !entryName.equals("..")) {
if (path.charAt(path.length() - 1) == '/')
queue.add(path + entryName);
else
queue.add(path + '/' + entryName);
}
}
Kernel.close( fd ) ;
}
}
/**
* Checks if the path exists, and lists that path name and all files in all directories.
* @exception java.lang.Exception if an exception is thrown
* by an underlying operation
*/
public static void main( String[] args ) throws Exception
{
// initialize the file system simulator kernel
Kernel.initialize() ;
boolean isRec = false;
// for each path-name given
for( int i = 0 ; i < args.length ; i ++ )
{
String name = args[i];
if (args[i].equals("-r")) {
isRec = true;
continue;
}
int status = 0 ;
// stat the name to get information about the file or directory
Stat stat = new Stat() ;
status = Kernel.stat( name , stat ) ;
if( status < 0 )
{
Kernel.perror( PROGRAM_NAME ) ;
Kernel.exit( 1 ) ;
}
// mask the file type from the mode
short type = (short)( stat.getMode() & Kernel.S_IFMT ) ;
// if name is a regular file, print the info
if( type == Kernel.S_IFREG )
{
System.out.println(name) ;
}
// if name is a directory open it and read the contents
else if( type == Kernel.S_IFDIR )
{
if (isRec)
printAllInPathRec(name);
else
printAllInPathQueue(name);
}
}
// exit with success if we process all the arguments
Kernel.exit( 0 ) ;
}
}