very minor code police. also fix a possible but unlikely missed cpu_boost(false)
[Rockbox.git] / apps / plugins / md5sum.c
blob084256badf1acc53c6041fdd9c2897572686d0fb
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id: $
10 * Copyright (C) 2008 Antoine Cellerier <dionoea -at- videolan -dot- org>
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version 2
15 * of the License, or (at your option) any later version.
17 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
18 * KIND, either express or implied.
20 ****************************************************************************/
22 #include "plugin.h"
23 #include "lib/md5.h"
25 PLUGIN_HEADER
27 static const struct plugin_api *rb;
29 MEM_FUNCTION_WRAPPERS(rb);
31 static int count = 0;
32 static int done = 0;
34 static int hash( char *string, const char *path )
36 char *buffer[512];
37 ssize_t len;
38 struct md5_s md5;
39 int in = rb->open( path, O_RDONLY );
40 if( in < 0 ) return -1;
42 InitMD5( &md5 );
43 while( ( len = rb->read( in, buffer, 512 ) ) > 0 )
44 AddMD5( &md5, buffer, len );
45 EndMD5( &md5 );
47 psz_md5_hash( string, &md5 );
49 rb->close( in );
50 return 0;
53 static void hash_file( int out, const char *path )
55 if( out < 0 )
56 count++;
57 else
59 char string[MD5_STRING_LENGTH+1];
60 done++;
61 rb->splash( 0, "%d / %d : %s", done, count, path );
62 if( hash( string, path ) )
63 rb->write( out, "error", 5 );
64 else
65 rb->write( out, string, MD5_STRING_LENGTH );
66 rb->write( out, " ", 2 );
67 rb->write( out, path, rb->strlen( path ) );
68 rb->write( out, "\n", 1 );
72 static void hash_dir( int out, const char *path );
73 static void hash_dir( int out, const char *path )
75 DIR *dir;
76 struct dirent *entry;
78 dir = rb->opendir( path );
79 if( dir )
81 while( ( entry = rb->readdir( dir ) ) )
83 char childpath[MAX_PATH];
84 rb->snprintf( childpath, MAX_PATH, "%s/%s",
85 path, entry->d_name );
86 if( entry->attribute & ATTR_DIRECTORY )
88 if( rb->strcmp( entry->d_name, "." )
89 && rb->strcmp( entry->d_name, ".." ) )
91 /* Got a sub directory */
92 hash_dir( out, childpath );
95 else
97 /* Got a file */
98 hash_file( out, childpath );
101 rb->closedir( dir );
105 static void hash_list( int out, const char *path )
107 int list = rb->open( path, O_RDONLY );
108 char newpath[MAX_PATH];
109 if( list < 0 ) return;
111 while( rb->read_line( list, newpath, MAX_PATH ) > 0 )
113 DIR *dir = rb->opendir( newpath );
114 if( dir )
116 rb->closedir( dir );
117 hash_dir( out, newpath );
119 else
121 hash_file( out, newpath );
125 rb->close( list );
128 static void hash_check( int out, const char *path )
130 int list = rb->open( path, O_RDONLY );
131 char line[MD5_STRING_LENGTH+1+MAX_PATH+1];
132 int len;
133 if( list < 0 ) return;
135 while( ( len = rb->read_line( list, line, MD5_STRING_LENGTH+1+MAX_PATH+1 ) ) > 0 )
137 if( out < 0 )
138 count++;
139 else
141 const char *filename = rb->strchr( line, ' ' );
142 done++;
143 rb->splash( 0, "%d / %d : %s", done, count, filename );
144 if( !filename || len < MD5_STRING_LENGTH + 2 )
146 const char error[] = "Malformed input line ... skipping";
147 rb->write( out, error, rb->strlen( error ) );
149 else
151 char string[MD5_STRING_LENGTH+1];
152 while( *filename == ' ' )
153 filename++;
154 rb->write( out, filename, rb->strlen( filename ) );
155 rb->write( out, ": ", 2 );
156 if( hash( string, filename ) )
157 rb->write( out, "FAILED open or read", 19 );
158 else if( rb->strncasecmp( line, string, MD5_STRING_LENGTH ) )
159 rb->write( out, "FAILED", 6 );
160 else
161 rb->write( out, "OK", 2 );
163 rb->write( out, "\n", 1 );
167 rb->close( list );
170 enum plugin_status plugin_start(const struct plugin_api* api, const void* parameter)
172 const char *arg = (const char *)parameter; /* input file name, if any */
173 int out = -1; /* output file descriptor */
174 char filename[MAX_PATH]; /* output file name */
176 void (*action)( int, const char * ) = NULL;
178 md5_init( api );
179 rb = api;
180 #ifdef HAVE_ADJUSTABLE_CPU_FREQ
181 rb->cpu_boost( true );
182 #endif
184 if( arg && *arg )
186 const char *ext = rb->strrchr( arg, '.' );
187 DIR *dir;
188 rb->snprintf( filename, MAX_PATH, "%s.md5sum", arg );
190 if( ext )
192 if( !rb->strcmp( ext, ".md5" ) || !rb->strcmp( ext, ".md5sum" ) )
194 rb->snprintf( filename + ( ext - arg ),
195 MAX_PATH + rb->strlen( ext ) - rb->strlen( arg ),
196 ".md5check" );
197 /* Lets check the sums */
198 action = hash_check;
200 else if( !rb->strcmp( ext, ".md5list" ) ) /* ugly */
202 /* Hash listed files */
203 action = hash_list;
207 if( !action )
209 dir = rb->opendir( arg );
210 if( dir )
212 api->closedir( dir );
214 /* Hash the directory's content recursively */
215 action = hash_dir;
217 else
219 /* Hash the file */
220 action = hash_file;
224 else
226 rb->snprintf( filename, MAX_PATH, "/everything.md5sum" );
227 /* Hash the whole filesystem */
228 action = hash_dir;
229 arg = "/";
232 rb->lcd_puts( 0, 1, "Output file:" );
233 rb->lcd_puts( 0, 2, filename );
235 count = 0;
236 done = 0;
237 action( out, arg );
239 out = rb->open( filename, O_WRONLY|O_CREAT );
240 if( out < 0 ) return PLUGIN_ERROR;
241 action( out, arg );
242 rb->close( out );
243 #ifdef HAVE_ADJUSTABLE_CPU_FREQ
244 rb->cpu_boost( false );
245 #endif
246 return PLUGIN_OK;