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 #define BUFFERSIZE 16384
31 static bool quit
= false;
33 static int hash( char *string
, const char *path
)
35 static char buffer
[BUFFERSIZE
];
38 int in
= rb
->open( path
, O_RDONLY
);
39 if( in
< 0 ) return -1;
42 while( !quit
&& ( len
= rb
->read( in
, buffer
, sizeof(buffer
) ) ) > 0 )
44 AddMD5( &md5
, buffer
, len
);
46 if( rb
->get_action(CONTEXT_STD
, TIMEOUT_NOBLOCK
) == ACTION_STD_CANCEL
)
52 psz_md5_hash( string
, &md5
);
58 static void hash_file( int out
, const char *path
)
64 char string
[MD5_STRING_LENGTH
+1];
67 rb
->splashf( 0, "%d / %d : %s", done
, count
, path
);
68 status
= hash( string
, path
);
74 rb
->write( out
, "error", 5 );
76 rb
->write( out
, string
, MD5_STRING_LENGTH
);
77 rb
->write( out
, " ", 2 );
78 rb
->write( out
, path
, rb
->strlen( path
) );
79 rb
->write( out
, "\n", 1 );
85 static void hash_dir( int out
, const char *path
)
90 dir
= rb
->opendir( path
);
93 while( !quit
&& ( entry
= rb
->readdir( dir
) ) )
95 char childpath
[MAX_PATH
];
96 rb
->snprintf( childpath
, MAX_PATH
, "%s/%s",
97 rb
->strcmp( path
, "/" ) ? path
: "", entry
->d_name
);
99 struct dirinfo info
= rb
->dir_get_info(dir
, entry
);
100 if (info
.attribute
& ATTR_DIRECTORY
)
102 if( rb
->strcmp( entry
->d_name
, "." )
103 && rb
->strcmp( entry
->d_name
, ".." ) )
105 /* Got a sub directory */
106 hash_dir( out
, childpath
);
112 hash_file( out
, childpath
);
119 static void hash_list( int out
, const char *path
)
121 int list
= rb
->open( path
, O_RDONLY
);
122 char newpath
[MAX_PATH
];
123 if( list
< 0 ) return;
125 while( !quit
&& rb
->read_line( list
, newpath
, MAX_PATH
) > 0 )
127 DIR *dir
= rb
->opendir( newpath
);
131 hash_dir( out
, newpath
);
135 hash_file( out
, newpath
);
142 static void hash_check( int out
, const char *path
)
144 int list
= rb
->open( path
, O_RDONLY
);
145 char line
[MD5_STRING_LENGTH
+1+MAX_PATH
+1];
147 if( list
< 0 ) return;
149 while( !quit
&& ( len
= rb
->read_line( list
, line
, MD5_STRING_LENGTH
+1+MAX_PATH
+1 ) ) > 0 )
155 const char *filename
= rb
->strchr( line
, ' ' );
157 rb
->splashf( 0, "%d / %d : %s", done
, count
, filename
);
158 if( !filename
|| len
< MD5_STRING_LENGTH
+ 2 )
160 const char error
[] = "Malformed input line ... skipping";
161 rb
->write( out
, error
, rb
->strlen( error
) );
165 char string
[MD5_STRING_LENGTH
+1];
166 while( *filename
== ' ' )
168 rb
->write( out
, filename
, rb
->strlen( filename
) );
169 rb
->write( out
, ": ", 2 );
170 if( hash( string
, filename
) )
171 rb
->write( out
, "FAILED open or read", 19 );
172 else if( rb
->strncasecmp( line
, string
, MD5_STRING_LENGTH
) )
173 rb
->write( out
, "FAILED", 6 );
175 rb
->write( out
, "OK", 2 );
177 rb
->write( out
, "\n", 1 );
184 enum plugin_status
plugin_start(const void* parameter
)
186 const char *arg
= (const char *)parameter
; /* input file name, if any */
187 int out
= -1; /* output file descriptor */
188 char filename
[MAX_PATH
]; /* output file name */
190 void (*action
)( int, const char * ) = NULL
;
192 #ifdef HAVE_ADJUSTABLE_CPU_FREQ
193 rb
->cpu_boost( true );
198 const char *ext
= rb
->strrchr( arg
, '.' );
200 rb
->snprintf( filename
, MAX_PATH
, "%s.md5sum", arg
);
204 if( !rb
->strcmp( ext
, ".md5" ) || !rb
->strcmp( ext
, ".md5sum" ) )
206 rb
->snprintf( filename
+ ( ext
- arg
),
207 MAX_PATH
+ rb
->strlen( ext
) - rb
->strlen( arg
),
209 /* Lets check the sums */
212 else if( !rb
->strcmp( ext
, ".md5list" ) ) /* ugly */
214 /* Hash listed files */
221 dir
= rb
->opendir( arg
);
226 /* Hash the directory's content recursively */
238 rb
->snprintf( filename
, MAX_PATH
, "/everything.md5sum" );
239 /* Hash the whole filesystem */
244 rb
->lcd_puts( 0, 1, "Output file:" );
245 rb
->lcd_puts( 0, 2, filename
);
251 out
= rb
->open( filename
, O_WRONLY
|O_CREAT
|O_TRUNC
, 0666);
252 if( out
< 0 ) return PLUGIN_ERROR
;
255 #ifdef HAVE_ADJUSTABLE_CPU_FREQ
256 rb
->cpu_boost( false );