2008-06-10 13:20:05 +00:00
|
|
|
/***************************************************************************
|
|
|
|
* __________ __ ___.
|
|
|
|
* Open \______ \ ____ ____ | | _\_ |__ _______ ___
|
|
|
|
* Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
|
|
|
|
* Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
|
|
|
|
* Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
|
|
|
|
* \/ \/ \/ \/ \/
|
2009-07-12 15:04:56 +00:00
|
|
|
* $Id$
|
2008-06-10 13:20:05 +00:00
|
|
|
*
|
|
|
|
* Copyright (C) 2008 Antoine Cellerier <dionoea -at- videolan -dot- org>
|
|
|
|
*
|
2008-06-28 18:10:04 +00:00
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* as published by the Free Software Foundation; either version 2
|
|
|
|
* of the License, or (at your option) any later version.
|
2008-06-10 13:20:05 +00:00
|
|
|
*
|
|
|
|
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
|
|
|
|
* KIND, either express or implied.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include "plugin.h"
|
|
|
|
#include "lib/md5.h"
|
|
|
|
|
|
|
|
PLUGIN_HEADER
|
|
|
|
|
2008-10-09 15:09:07 +00:00
|
|
|
#define BUFFERSIZE 16384
|
|
|
|
|
2008-06-10 16:24:25 +00:00
|
|
|
static int count = 0;
|
|
|
|
static int done = 0;
|
2008-10-09 15:09:07 +00:00
|
|
|
static bool quit = false;
|
2008-06-10 16:24:25 +00:00
|
|
|
|
|
|
|
static int hash( char *string, const char *path )
|
2008-06-10 13:20:05 +00:00
|
|
|
{
|
2008-10-09 15:09:07 +00:00
|
|
|
static char buffer[BUFFERSIZE];
|
2008-06-10 13:20:05 +00:00
|
|
|
ssize_t len;
|
|
|
|
struct md5_s md5;
|
|
|
|
int in = rb->open( path, O_RDONLY );
|
|
|
|
if( in < 0 ) return -1;
|
|
|
|
|
|
|
|
InitMD5( &md5 );
|
2008-10-09 15:09:07 +00:00
|
|
|
while( !quit && ( len = rb->read( in, buffer, sizeof(buffer) ) ) > 0 )
|
|
|
|
{
|
2008-06-10 13:20:05 +00:00
|
|
|
AddMD5( &md5, buffer, len );
|
2008-10-09 15:09:07 +00:00
|
|
|
|
|
|
|
if( rb->get_action(CONTEXT_STD, TIMEOUT_NOBLOCK) == ACTION_STD_CANCEL )
|
|
|
|
quit = true;
|
|
|
|
}
|
|
|
|
|
2008-06-10 13:20:05 +00:00
|
|
|
EndMD5( &md5 );
|
|
|
|
|
|
|
|
psz_md5_hash( string, &md5 );
|
|
|
|
|
|
|
|
rb->close( in );
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2008-06-10 16:24:25 +00:00
|
|
|
static void hash_file( int out, const char *path )
|
2008-06-10 13:20:05 +00:00
|
|
|
{
|
2008-06-10 16:24:25 +00:00
|
|
|
if( out < 0 )
|
|
|
|
count++;
|
2008-06-10 13:20:05 +00:00
|
|
|
else
|
2008-06-10 16:24:25 +00:00
|
|
|
{
|
|
|
|
char string[MD5_STRING_LENGTH+1];
|
2008-10-09 15:09:07 +00:00
|
|
|
int status;
|
2008-06-10 16:24:25 +00:00
|
|
|
done++;
|
2008-08-15 08:27:39 +00:00
|
|
|
rb->splashf( 0, "%d / %d : %s", done, count, path );
|
2008-10-09 15:09:07 +00:00
|
|
|
status = hash( string, path );
|
|
|
|
|
|
|
|
if( quit )
|
|
|
|
return;
|
|
|
|
|
|
|
|
if( status )
|
2008-06-10 16:24:25 +00:00
|
|
|
rb->write( out, "error", 5 );
|
|
|
|
else
|
|
|
|
rb->write( out, string, MD5_STRING_LENGTH );
|
|
|
|
rb->write( out, " ", 2 );
|
|
|
|
rb->write( out, path, rb->strlen( path ) );
|
|
|
|
rb->write( out, "\n", 1 );
|
2008-10-09 15:09:07 +00:00
|
|
|
|
|
|
|
rb->yield();
|
2008-06-10 16:24:25 +00:00
|
|
|
}
|
2008-06-10 13:20:05 +00:00
|
|
|
}
|
|
|
|
|
2008-06-10 16:24:25 +00:00
|
|
|
static void hash_dir( int out, const char *path )
|
2008-06-10 13:20:05 +00:00
|
|
|
{
|
|
|
|
DIR *dir;
|
|
|
|
struct dirent *entry;
|
|
|
|
|
|
|
|
dir = rb->opendir( path );
|
|
|
|
if( dir )
|
|
|
|
{
|
2008-10-09 15:09:07 +00:00
|
|
|
while( !quit && ( entry = rb->readdir( dir ) ) )
|
2008-06-10 13:20:05 +00:00
|
|
|
{
|
|
|
|
char childpath[MAX_PATH];
|
|
|
|
rb->snprintf( childpath, MAX_PATH, "%s/%s",
|
2008-10-09 15:09:07 +00:00
|
|
|
rb->strcmp( path, "/" ) ? path : "", entry->d_name );
|
2008-06-10 13:20:05 +00:00
|
|
|
if( entry->attribute & ATTR_DIRECTORY )
|
|
|
|
{
|
|
|
|
if( rb->strcmp( entry->d_name, "." )
|
|
|
|
&& rb->strcmp( entry->d_name, ".." ) )
|
|
|
|
{
|
|
|
|
/* Got a sub directory */
|
|
|
|
hash_dir( out, childpath );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Got a file */
|
|
|
|
hash_file( out, childpath );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
rb->closedir( dir );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-06-10 16:24:25 +00:00
|
|
|
static void hash_list( int out, const char *path )
|
2008-06-10 13:20:05 +00:00
|
|
|
{
|
|
|
|
int list = rb->open( path, O_RDONLY );
|
|
|
|
char newpath[MAX_PATH];
|
|
|
|
if( list < 0 ) return;
|
|
|
|
|
2008-10-09 15:09:07 +00:00
|
|
|
while( !quit && rb->read_line( list, newpath, MAX_PATH ) > 0 )
|
2008-06-10 13:20:05 +00:00
|
|
|
{
|
|
|
|
DIR *dir = rb->opendir( newpath );
|
|
|
|
if( dir )
|
|
|
|
{
|
|
|
|
rb->closedir( dir );
|
|
|
|
hash_dir( out, newpath );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
hash_file( out, newpath );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
rb->close( list );
|
|
|
|
}
|
|
|
|
|
2008-06-10 16:24:25 +00:00
|
|
|
static void hash_check( int out, const char *path )
|
2008-06-10 13:20:05 +00:00
|
|
|
{
|
|
|
|
int list = rb->open( path, O_RDONLY );
|
|
|
|
char line[MD5_STRING_LENGTH+1+MAX_PATH+1];
|
|
|
|
int len;
|
|
|
|
if( list < 0 ) return;
|
|
|
|
|
2008-10-09 15:09:07 +00:00
|
|
|
while( !quit && ( len = rb->read_line( list, line, MD5_STRING_LENGTH+1+MAX_PATH+1 ) ) > 0 )
|
2008-06-10 13:20:05 +00:00
|
|
|
{
|
2008-06-10 16:24:25 +00:00
|
|
|
if( out < 0 )
|
|
|
|
count++;
|
2008-06-10 13:20:05 +00:00
|
|
|
else
|
|
|
|
{
|
2008-06-10 16:24:25 +00:00
|
|
|
const char *filename = rb->strchr( line, ' ' );
|
|
|
|
done++;
|
2008-08-15 08:27:39 +00:00
|
|
|
rb->splashf( 0, "%d / %d : %s", done, count, filename );
|
2008-06-10 16:24:25 +00:00
|
|
|
if( !filename || len < MD5_STRING_LENGTH + 2 )
|
|
|
|
{
|
|
|
|
const char error[] = "Malformed input line ... skipping";
|
|
|
|
rb->write( out, error, rb->strlen( error ) );
|
|
|
|
}
|
2008-06-10 13:20:05 +00:00
|
|
|
else
|
2008-06-10 16:24:25 +00:00
|
|
|
{
|
|
|
|
char string[MD5_STRING_LENGTH+1];
|
|
|
|
while( *filename == ' ' )
|
|
|
|
filename++;
|
|
|
|
rb->write( out, filename, rb->strlen( filename ) );
|
|
|
|
rb->write( out, ": ", 2 );
|
|
|
|
if( hash( string, filename ) )
|
|
|
|
rb->write( out, "FAILED open or read", 19 );
|
|
|
|
else if( rb->strncasecmp( line, string, MD5_STRING_LENGTH ) )
|
|
|
|
rb->write( out, "FAILED", 6 );
|
|
|
|
else
|
|
|
|
rb->write( out, "OK", 2 );
|
|
|
|
}
|
|
|
|
rb->write( out, "\n", 1 );
|
2008-06-10 13:20:05 +00:00
|
|
|
}
|
|
|
|
}
|
2008-06-10 16:24:25 +00:00
|
|
|
|
|
|
|
rb->close( list );
|
2008-06-10 13:20:05 +00:00
|
|
|
}
|
|
|
|
|
2009-01-16 10:34:40 +00:00
|
|
|
enum plugin_status plugin_start(const void* parameter)
|
2008-06-10 13:20:05 +00:00
|
|
|
{
|
|
|
|
const char *arg = (const char *)parameter; /* input file name, if any */
|
|
|
|
int out = -1; /* output file descriptor */
|
|
|
|
char filename[MAX_PATH]; /* output file name */
|
|
|
|
|
2008-06-10 16:24:25 +00:00
|
|
|
void (*action)( int, const char * ) = NULL;
|
|
|
|
|
2008-06-10 13:55:47 +00:00
|
|
|
#ifdef HAVE_ADJUSTABLE_CPU_FREQ
|
|
|
|
rb->cpu_boost( true );
|
|
|
|
#endif
|
2008-06-10 13:20:05 +00:00
|
|
|
|
|
|
|
if( arg && *arg )
|
|
|
|
{
|
|
|
|
const char *ext = rb->strrchr( arg, '.' );
|
|
|
|
DIR *dir;
|
|
|
|
rb->snprintf( filename, MAX_PATH, "%s.md5sum", arg );
|
|
|
|
|
|
|
|
if( ext )
|
|
|
|
{
|
|
|
|
if( !rb->strcmp( ext, ".md5" ) || !rb->strcmp( ext, ".md5sum" ) )
|
|
|
|
{
|
2008-06-10 16:24:25 +00:00
|
|
|
rb->snprintf( filename + ( ext - arg ),
|
|
|
|
MAX_PATH + rb->strlen( ext ) - rb->strlen( arg ),
|
|
|
|
".md5check" );
|
2008-06-10 13:20:05 +00:00
|
|
|
/* Lets check the sums */
|
2008-06-10 16:24:25 +00:00
|
|
|
action = hash_check;
|
2008-06-10 13:20:05 +00:00
|
|
|
}
|
|
|
|
else if( !rb->strcmp( ext, ".md5list" ) ) /* ugly */
|
|
|
|
{
|
|
|
|
/* Hash listed files */
|
2008-06-10 16:24:25 +00:00
|
|
|
action = hash_list;
|
2008-06-10 13:20:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-06-10 16:24:25 +00:00
|
|
|
if( !action )
|
2008-06-10 13:20:05 +00:00
|
|
|
{
|
2008-06-10 16:24:25 +00:00
|
|
|
dir = rb->opendir( arg );
|
|
|
|
if( dir )
|
|
|
|
{
|
2009-01-16 10:34:40 +00:00
|
|
|
rb->closedir( dir );
|
2008-06-10 13:20:05 +00:00
|
|
|
|
2008-06-10 16:24:25 +00:00
|
|
|
/* Hash the directory's content recursively */
|
|
|
|
action = hash_dir;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Hash the file */
|
|
|
|
action = hash_file;
|
|
|
|
}
|
2008-06-10 13:20:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
rb->snprintf( filename, MAX_PATH, "/everything.md5sum" );
|
|
|
|
/* Hash the whole filesystem */
|
2008-06-10 16:24:25 +00:00
|
|
|
action = hash_dir;
|
|
|
|
arg = "/";
|
2008-06-10 13:20:05 +00:00
|
|
|
}
|
|
|
|
|
2008-06-10 16:24:25 +00:00
|
|
|
rb->lcd_puts( 0, 1, "Output file:" );
|
|
|
|
rb->lcd_puts( 0, 2, filename );
|
|
|
|
|
|
|
|
count = 0;
|
|
|
|
done = 0;
|
|
|
|
action( out, arg );
|
|
|
|
|
2008-10-09 15:09:07 +00:00
|
|
|
out = rb->open( filename, O_WRONLY|O_CREAT|O_TRUNC );
|
2008-06-10 16:24:25 +00:00
|
|
|
if( out < 0 ) return PLUGIN_ERROR;
|
|
|
|
action( out, arg );
|
|
|
|
rb->close( out );
|
2008-06-10 13:55:47 +00:00
|
|
|
#ifdef HAVE_ADJUSTABLE_CPU_FREQ
|
2008-06-10 16:24:25 +00:00
|
|
|
rb->cpu_boost( false );
|
2008-06-10 13:55:47 +00:00
|
|
|
#endif
|
2008-06-10 16:24:25 +00:00
|
|
|
return PLUGIN_OK;
|
2008-06-10 13:20:05 +00:00
|
|
|
}
|