1 /***************************************************************************
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
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 ****************************************************************************/
27 static const struct plugin_api
*rb
;
29 MEM_FUNCTION_WRAPPERS(rb
);
34 static int hash( char *string
, const char *path
)
39 int in
= rb
->open( path
, O_RDONLY
);
40 if( in
< 0 ) return -1;
43 while( ( len
= rb
->read( in
, buffer
, 512 ) ) > 0 )
44 AddMD5( &md5
, buffer
, len
);
47 psz_md5_hash( string
, &md5
);
53 static void hash_file( int out
, const char *path
)
59 char string
[MD5_STRING_LENGTH
+1];
61 rb
->splash( 0, "%d / %d : %s", done
, count
, path
);
62 if( hash( string
, path
) )
63 rb
->write( out
, "error", 5 );
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
)
78 dir
= rb
->opendir( path
);
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
);
98 hash_file( out
, childpath
);
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
);
117 hash_dir( out
, newpath
);
121 hash_file( out
, newpath
);
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];
133 if( list
< 0 ) return;
135 while( ( len
= rb
->read_line( list
, line
, MD5_STRING_LENGTH
+1+MAX_PATH
+1 ) ) > 0 )
141 const char *filename
= rb
->strchr( line
, ' ' );
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
) );
151 char string
[MD5_STRING_LENGTH
+1];
152 while( *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 );
161 rb
->write( out
, "OK", 2 );
163 rb
->write( out
, "\n", 1 );
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
;
180 #ifdef HAVE_ADJUSTABLE_CPU_FREQ
181 rb
->cpu_boost( true );
186 const char *ext
= rb
->strrchr( arg
, '.' );
188 rb
->snprintf( filename
, MAX_PATH
, "%s.md5sum", arg
);
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
),
197 /* Lets check the sums */
200 else if( !rb
->strcmp( ext
, ".md5list" ) ) /* ugly */
202 /* Hash listed files */
209 dir
= rb
->opendir( arg
);
212 api
->closedir( dir
);
214 /* Hash the directory's content recursively */
226 rb
->snprintf( filename
, MAX_PATH
, "/everything.md5sum" );
227 /* Hash the whole filesystem */
232 rb
->lcd_puts( 0, 1, "Output file:" );
233 rb
->lcd_puts( 0, 2, filename
);
239 out
= rb
->open( filename
, O_WRONLY
|O_CREAT
);
240 if( out
< 0 ) return PLUGIN_ERROR
;
243 #ifdef HAVE_ADJUSTABLE_CPU_FREQ
244 rb
->cpu_boost( false );