|
1 /* |
|
2 * Copyright (c) 2005-2009 Nokia Corporation and/or its subsidiary(-ies). |
|
3 * All rights reserved. |
|
4 * This component and the accompanying materials are made available |
|
5 * under the terms of "Eclipse Public License v1.0" |
|
6 * which accompanies this distribution, and is available |
|
7 * at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
8 * |
|
9 * Initial Contributors: |
|
10 * Nokia Corporation - initial contribution. |
|
11 * |
|
12 * Contributors: |
|
13 * |
|
14 * Description: |
|
15 * System Include |
|
16 * |
|
17 */ |
|
18 |
|
19 |
|
20 |
|
21 #include <stdio.h> |
|
22 #include <windows.h> |
|
23 #include <direct.h> |
|
24 #include <assert.h> |
|
25 |
|
26 /******************************************************************************* |
|
27 * |
|
28 * Local Include |
|
29 * |
|
30 ******************************************************************************/ |
|
31 #include "cleantree.h" |
|
32 |
|
33 /******************************************************************************* |
|
34 * |
|
35 * Definitions |
|
36 * |
|
37 ******************************************************************************/ |
|
38 #define MAXPATHLEN 1024 |
|
39 |
|
40 /******************************************************************************* |
|
41 * |
|
42 * Types |
|
43 * |
|
44 ******************************************************************************/ |
|
45 typedef void (*TProcessFunction)(char*,int); |
|
46 |
|
47 /******************************************************************************* |
|
48 * |
|
49 * Prototypes |
|
50 * |
|
51 ******************************************************************************/ |
|
52 int ProcessDirectoryTree( char *path, int depth, TProcessFunction func ); |
|
53 int UpdateWorkingDirectory( char *previous, char *next, char *path, int op ); |
|
54 void PrintPath( char *str, int depth ); |
|
55 void ProcessFile( char *filename, int depth ); |
|
56 |
|
57 /******************************************************************************* |
|
58 * |
|
59 * Filescope statics |
|
60 * |
|
61 ******************************************************************************/ |
|
62 static int debug_flag = 0; |
|
63 static char *dirname = NULL; |
|
64 static int deleted_file_count; |
|
65 static int deleted_directory_count; |
|
66 |
|
67 /******************************************************************************* |
|
68 * |
|
69 * Main |
|
70 * |
|
71 ******************************************************************************/ |
|
72 int main( int argc, char *argv[] ) |
|
73 { |
|
74 char fname[1024]; |
|
75 int slen; |
|
76 |
|
77 // check params |
|
78 if( argc < 3 ) { |
|
79 fprintf( stderr, "usage: cleantree path dirname\n" ); |
|
80 return -1; |
|
81 } |
|
82 dirname = argv[2]; |
|
83 |
|
84 // get the name into the correct format |
|
85 strcpy( fname, argv[1] ); |
|
86 slen = strlen(fname); |
|
87 if( (fname[slen-1] != '*') || (fname[slen-2] != '\\') ) { |
|
88 fprintf( stderr, "ERROR: pathname must end in '\\*'.\n" ); |
|
89 return -1; |
|
90 } |
|
91 |
|
92 // process the directory tree |
|
93 ProcessDirectoryTree( argv[1], 0, ProcessFile ); |
|
94 |
|
95 // print stats |
|
96 printf( "%d files deleted from %d directories\n", deleted_file_count, deleted_directory_count ); |
|
97 |
|
98 // done |
|
99 fflush( stdout ); |
|
100 return 0; |
|
101 } |
|
102 |
|
103 /******************************************************************************* |
|
104 * |
|
105 * ProcessFile |
|
106 * |
|
107 ******************************************************************************/ |
|
108 void ProcessFile( char *filename, int depth ) |
|
109 { |
|
110 if( debug_flag > 0 ) { |
|
111 unlink( filename ); |
|
112 deleted_file_count++; |
|
113 } |
|
114 } |
|
115 |
|
116 /******************************************************************************* |
|
117 * |
|
118 * ProcessDirectoryTree |
|
119 * |
|
120 ******************************************************************************/ |
|
121 int ProcessDirectoryTree( char *path, int depth, TProcessFunction func ) |
|
122 { |
|
123 WIN32_FIND_DATA finfo; |
|
124 HANDLE list; |
|
125 int plen; |
|
126 int err = 1; |
|
127 int match; |
|
128 char previous_working_directory[MAXPATHLEN]; |
|
129 char new_working_directory[MAXPATHLEN]; |
|
130 char fullname[MAXPATHLEN]; |
|
131 |
|
132 // ignore . and .. |
|
133 if( path[0] == '.' ) { |
|
134 return 0; |
|
135 } |
|
136 |
|
137 // ignore any dir beginning with _ |
|
138 if( path[0] == '_' ) { |
|
139 return 0; |
|
140 } |
|
141 |
|
142 // print the path |
|
143 // PrintPath( path, (depth - 1)); |
|
144 |
|
145 // check if we are in a debug directory |
|
146 plen = strlen( path ); |
|
147 assert( path[plen-1] == '*' ); |
|
148 assert( path[plen-2] == '\\' ); |
|
149 path[plen-2] = 0; |
|
150 match = strcmp( path, dirname ); |
|
151 if( match == 0 ) { |
|
152 debug_flag++; |
|
153 } |
|
154 path[plen-2] = '\\'; |
|
155 |
|
156 // get the first file in the directory |
|
157 list = FindFirstFile( path, &finfo ); |
|
158 if( list == INVALID_HANDLE_VALUE ) { |
|
159 return 0; |
|
160 } |
|
161 |
|
162 // update the current working directory |
|
163 UpdateWorkingDirectory( previous_working_directory, new_working_directory, path, ((depth == 0) ? 1 : 0) ); |
|
164 |
|
165 // loop through the rest of the files |
|
166 for( err = 1; err != 0; err ) { |
|
167 |
|
168 // process the file |
|
169 if( finfo.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY ) { |
|
170 strcat( finfo.cFileName, "\\*" ); |
|
171 ProcessDirectoryTree( finfo.cFileName, depth + 1, func ); |
|
172 } else { |
|
173 strcpy( fullname, new_working_directory ); |
|
174 strcat( fullname, "\\" ); |
|
175 strcat( fullname, finfo.cFileName ); |
|
176 func( fullname, depth ); |
|
177 } |
|
178 |
|
179 // get the info for the next file |
|
180 err = FindNextFile( list, &finfo ); |
|
181 } |
|
182 |
|
183 // update the working directory and close the directory |
|
184 UpdateWorkingDirectory( previous_working_directory, new_working_directory, path, 2 ); |
|
185 FindClose( list ); |
|
186 |
|
187 // now delete the directory |
|
188 if( match == 0 ) { |
|
189 err = RemoveDirectory( new_working_directory ); |
|
190 if( err == 0 ) { |
|
191 fprintf( stderr, "ERROR: failed to remove directory (%d)\n", GetLastError() ); |
|
192 } |
|
193 deleted_directory_count++; |
|
194 debug_flag--; |
|
195 } |
|
196 |
|
197 return 0; |
|
198 } |
|
199 |
|
200 |
|
201 /******************************************************************************* |
|
202 * |
|
203 * UpdateWorkingDirectory |
|
204 * |
|
205 ******************************************************************************/ |
|
206 int UpdateWorkingDirectory( char *previous, char *next, char *path, int op ) |
|
207 { |
|
208 int len; |
|
209 |
|
210 // op zero means normal PUSH of the working directory |
|
211 if( op == 0 ) { |
|
212 getcwd( previous, MAXPATHLEN ); |
|
213 getcwd( next, MAXPATHLEN ); |
|
214 len = strlen( next ); |
|
215 next[len] = '\\'; |
|
216 next[len+1] = 0; |
|
217 strcat( next, path ); |
|
218 len = strlen(next); |
|
219 next[len-2] = 0; |
|
220 chdir( next ); |
|
221 } |
|
222 |
|
223 // op one is initial set of the dirs |
|
224 if( op == 1 ) { |
|
225 getcwd( previous, MAXPATHLEN ); |
|
226 strcpy( next, path ); |
|
227 len = strlen(next); |
|
228 next[len-1] = 0; |
|
229 chdir( next ); |
|
230 } |
|
231 |
|
232 // op two is pop |
|
233 if( op == 2 ) { |
|
234 chdir( previous ); |
|
235 } |
|
236 |
|
237 // done |
|
238 return 0; |
|
239 } |
|
240 |
|
241 |
|
242 /******************************************************************************* |
|
243 * |
|
244 * Print Path |
|
245 * |
|
246 ******************************************************************************/ |
|
247 void PrintPath( char *str, int depth ) |
|
248 { |
|
249 for( int i = 0; i < (depth - 1); i++ ) |
|
250 printf( "\t" ); |
|
251 printf( "%s\n", str ); |
|
252 } |
|
253 |
|
254 |