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
);
31 #define BUFFERSIZE 16384
35 static bool quit
= false;
37 static int hash( char *string
, const char *path
)
39 static char buffer
[BUFFERSIZE
];
42 int in
= rb
->open( path
, O_RDONLY
);
43 if( in
< 0 ) return -1;
46 while( !quit
&& ( len
= rb
->read( in
, buffer
, sizeof(buffer
) ) ) > 0 )
48 AddMD5( &md5
, buffer
, len
);
50 if( rb
->get_action(CONTEXT_STD
, TIMEOUT_NOBLOCK
) == ACTION_STD_CANCEL
)
56 psz_md5_hash( string
, &md5
);
62 static void hash_file( int out
, const char *path
)
68 char string
[MD5_STRING_LENGTH
+1];
71 rb
->splashf( 0, "%d / %d : %s", done
, count
, path
);
72 status
= hash( string
, path
);
78 rb
->write( out
, "error", 5 );
80 rb
->write( out
, string
, MD5_STRING_LENGTH
);
81 rb
->write( out
, " ", 2 );
82 rb
->write( out
, path
, rb
->strlen( path
) );
83 rb
->write( out
, "\n", 1 );
89 static void hash_dir( int out
, const char *path
)
94 dir
= rb
->opendir( path
);
97 while( !quit
&& ( entry
= rb
->readdir( dir
) ) )
99 char childpath
[MAX_PATH
];
100 rb
->snprintf( childpath
, MAX_PATH
, "%s/%s",
101 rb
->strcmp( path
, "/" ) ? path
: "", entry
->d_name
);
102 if( entry
->attribute
& ATTR_DIRECTORY
)
104 if( rb
->strcmp( entry
->d_name
, "." )
105 && rb
->strcmp( entry
->d_name
, ".." ) )
107 /* Got a sub directory */
108 hash_dir( out
, childpath
);
114 hash_file( out
, childpath
);
121 static void hash_list( int out
, const char *path
)
123 int list
= rb
->open( path
, O_RDONLY
);
124 char newpath
[MAX_PATH
];
125 if( list
< 0 ) return;
127 while( !quit
&& rb
->read_line( list
, newpath
, MAX_PATH
) > 0 )
129 DIR *dir
= rb
->opendir( newpath
);
133 hash_dir( out
, newpath
);
137 hash_file( out
, newpath
);
144 static void hash_check( int out
, const char *path
)
146 int list
= rb
->open( path
, O_RDONLY
);
147 char line
[MD5_STRING_LENGTH
+1+MAX_PATH
+1];
149 if( list
< 0 ) return;
151 while( !quit
&& ( len
= rb
->read_line( list
, line
, MD5_STRING_LENGTH
+1+MAX_PATH
+1 ) ) > 0 )
157 const char *filename
= rb
->strchr( line
, ' ' );
159 rb
->splashf( 0, "%d / %d : %s", done
, count
, filename
);
160 if( !filename
|| len
< MD5_STRING_LENGTH
+ 2 )
162 const char error
[] = "Malformed input line ... skipping";
163 rb
->write( out
, error
, rb
->strlen( error
) );
167 char string
[MD5_STRING_LENGTH
+1];
168 while( *filename
== ' ' )
170 rb
->write( out
, filename
, rb
->strlen( filename
) );
171 rb
->write( out
, ": ", 2 );
172 if( hash( string
, filename
) )
173 rb
->write( out
, "FAILED open or read", 19 );
174 else if( rb
->strncasecmp( line
, string
, MD5_STRING_LENGTH
) )
175 rb
->write( out
, "FAILED", 6 );
177 rb
->write( out
, "OK", 2 );
179 rb
->write( out
, "\n", 1 );
186 enum plugin_status
plugin_start(const struct plugin_api
* api
, const void* parameter
)
188 const char *arg
= (const char *)parameter
; /* input file name, if any */
189 int out
= -1; /* output file descriptor */
190 char filename
[MAX_PATH
]; /* output file name */
192 void (*action
)( int, const char * ) = NULL
;
196 #ifdef HAVE_ADJUSTABLE_CPU_FREQ
197 rb
->cpu_boost( true );
202 const char *ext
= rb
->strrchr( arg
, '.' );
204 rb
->snprintf( filename
, MAX_PATH
, "%s.md5sum", arg
);
208 if( !rb
->strcmp( ext
, ".md5" ) || !rb
->strcmp( ext
, ".md5sum" ) )
210 rb
->snprintf( filename
+ ( ext
- arg
),
211 MAX_PATH
+ rb
->strlen( ext
) - rb
->strlen( arg
),
213 /* Lets check the sums */
216 else if( !rb
->strcmp( ext
, ".md5list" ) ) /* ugly */
218 /* Hash listed files */
225 dir
= rb
->opendir( arg
);
228 api
->closedir( dir
);
230 /* Hash the directory's content recursively */
242 rb
->snprintf( filename
, MAX_PATH
, "/everything.md5sum" );
243 /* Hash the whole filesystem */
248 rb
->lcd_puts( 0, 1, "Output file:" );
249 rb
->lcd_puts( 0, 2, filename
);
255 out
= rb
->open( filename
, O_WRONLY
|O_CREAT
|O_TRUNC
);
256 if( out
< 0 ) return PLUGIN_ERROR
;
259 #ifdef HAVE_ADJUSTABLE_CPU_FREQ
260 rb
->cpu_boost( false );