From 7eb36f9021e86a75fd291b6aae424117a230134d Mon Sep 17 00:00:00 2001 From: crunchy Date: Thu, 19 Sep 2002 20:46:02 +0000 Subject: added error handing functions --- ChangeLog | 10 +++++++ README | 9 +++--- libmpio/defs.h | 7 ++++- libmpio/mpio.c | 85 ++++++++++++++++++++++++++++++++++++++++++++++--------- libmpio/mpio.h | 15 +++++++++- mpiosh/callback.c | 9 +++--- 6 files changed, 110 insertions(+), 25 deletions(-) diff --git a/ChangeLog b/ChangeLog index a099610..e075efa 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,13 @@ +2002-09-19 Andreas Buesching + + * mpiosh/callback.c (mpiosh_cmd_mput): using new error handling + functions + + * libmpio/mpio.c (mpio_errno, mpio_strerror, mpio_perror): + add functions for error handling + (mpio_file_get, mpio_file_put, mpio_file_del): using error + handling functions + 2002-09-19 Markus Germeier * libmpio/mpio.c (mpio_init_internal): diff --git a/README b/README index e054e83..0149144 100644 --- a/README +++ b/README @@ -17,10 +17,11 @@ When you want to stop playing, disconnect MPIO and type: - Is there any risk to use this software with my MPIO mp3 player? -We do not really know that, but we do not garantee for anything. If you -use this software and your MPIO mp3 player turns into a useless brick it -is your own fault. If you use any other device with this library it would -be a wonder if does _not_ turn into a brick. So if you use do not like your mp3 player just use this software ;-) +We do not really know that, but we do not garantee for anything. If +you use this software and your MPIO mp3 player turns into a useless +brick it is your own fault. If you use any other device with this +library it would be a wonder if does _not_ turn it into a brick. So if +you do not like your mp3 player just use this software ;-) - I downloaded a current CVS snapshot but there is no configure script, only a configure.in ! What do I have to do?? diff --git a/libmpio/defs.h b/libmpio/defs.h index 7e06524..70f789f 100644 --- a/libmpio/defs.h +++ b/libmpio/defs.h @@ -1,7 +1,7 @@ /* -*- linux-c -*- */ /* - * $Id: defs.h,v 1.6 2002/09/18 20:32:21 crunchy Exp $ + * $Id: defs.h,v 1.7 2002/09/19 20:46:02 crunchy Exp $ * * Library for USB MPIO-* * @@ -93,6 +93,11 @@ typedef enum { FTYPE_CONF = 'C', #define INFO_LINE 81 /* error codes */ +typedef struct { + int id; + char *msg; +} mpio_error_t; + #define MPIO_ERR_FILE_NOT_FOUND -1 #define MPIO_ERR_NOT_ENOUGH_SPACE -2 #define MPIO_ERR_FILE_EXISTS -3 diff --git a/libmpio/mpio.c b/libmpio/mpio.c index e9ab24a..ea0f23c 100644 --- a/libmpio/mpio.c +++ b/libmpio/mpio.c @@ -1,6 +1,6 @@ /* * - * $Id: mpio.c,v 1.20 2002/09/18 23:17:03 germeier Exp $ + * $Id: mpio.c,v 1.21 2002/09/19 20:46:02 crunchy Exp $ * * Library for USB MPIO-* * @@ -49,7 +49,31 @@ static BYTE *mpio_model_name[] = { "MPIO-DMB", "MPIO-DMB+", "MPIO-DMK", - "unknown" }; + "unknown" +}; + +static mpio_error_t mpio_errors[] = { + { MPIO_ERR_FILE_NOT_FOUND, + "The selected file can not be found." }, + { MPIO_ERR_NOT_ENOUGH_SPACE, + "There is not enough space left on the selected memory card." }, + { MPIO_ERR_FILE_EXISTS, + "The selected file already exists and can not be overwritten. Remove it first." }, + { MPIO_ERR_FAT_ERROR, + "Internal error while reading the FAT." }, + { MPIO_ERR_READING_FILE, + "The selected file can not be read." }, + { MPIO_ERR_PERMISSION_DENIED, + "There are not enough rights to access the file/directory." }, + { MPIO_ERR_WRITING_FILE, + "There are no permisson to write to the selected file." } +}; + +static const int mpio_error_num = sizeof mpio_errors / sizeof(mpio_error_t); + +static int _mpio_errno = 0; + +#define MPIO_ERR_RETURN(err) _mpio_errno = err; return -1 void mpio_init_internal(mpio_t *); void mpio_init_external(mpio_t *); @@ -318,8 +342,8 @@ mpio_file_get(mpio_t *m, mpio_mem_t mem, BYTE *filename, /* please fix me sometime */ /* the system entries are kind of special ! */ - if (strncmp("sysdum", filename, 6) == 0) - return MPIO_ERR_PERMISSION_DENIED; + if (strncmp("sysdum", filename, 6) == 0) + MPIO_ERR_RETURN(MPIO_ERR_PERMISSION_DENIED); if (mem == MPIO_INTERNAL_MEM) sm = &m->internal; if (mem == MPIO_EXTERNAL_MEM) sm = &m->external; @@ -354,7 +378,7 @@ mpio_file_get(mpio_t *m, mpio_mem_t mem, BYTE *filename, debug("error writing file data\n"); close(fd); free (f); - return MPIO_ERR_WRITING_FILE; + MPIO_ERR_RETURN(MPIO_ERR_WRITING_FILE); } filesize -= towrite; @@ -404,7 +428,7 @@ mpio_file_put(mpio_t *m, mpio_mem_t mem, BYTE *filename, if (stat((const char *)filename, &file_stat)!=0) { debug("could not find file: %s\n", filename); - return MPIO_ERR_FILE_NOT_FOUND; + MPIO_ERR_RETURN(MPIO_ERR_FILE_NOT_FOUND); } fsize=filesize=file_stat.st_size; debugn(2, "filesize: %d\n", fsize); @@ -413,7 +437,7 @@ mpio_file_put(mpio_t *m, mpio_mem_t mem, BYTE *filename, mpio_memory_free(m, mem, &free); if (free*1024entry; @@ -459,7 +483,7 @@ mpio_file_put(mpio_t *m, mpio_mem_t mem, BYTE *filename, if (fd==-1) { debug("could not open file: %s\n", filename); - return MPIO_ERR_FILE_NOT_FOUND; + MPIO_ERR_RETURN(MPIO_ERR_FILE_NOT_FOUND); } while ((filesize>BLOCK_SIZE) && (!abort)) { @@ -473,7 +497,7 @@ mpio_file_put(mpio_t *m, mpio_mem_t mem, BYTE *filename, if (read(fd, block, toread)!=toread) { debug("error reading file data\n"); close(fd); - return MPIO_ERR_READING_FILE; + MPIO_ERR_RETURN(MPIO_ERR_READING_FILE); } filesize -= toread; @@ -503,7 +527,7 @@ mpio_file_put(mpio_t *m, mpio_mem_t mem, BYTE *filename, if (read(fd, block, toread)!=toread) { debug("error reading file data\n"); close(fd); - return MPIO_ERR_READING_FILE; + MPIO_ERR_RETURN(MPIO_ERR_READING_FILE); } filesize -= toread; @@ -650,7 +674,7 @@ mpio_file_del(mpio_t *m, mpio_mem_t mem, BYTE *filename, /* please fix me sometime */ /* the system entry are kind of special ! */ if (strncmp("sysdum", filename, 6)==0) - return MPIO_ERR_PERMISSION_DENIED; + MPIO_ERR_RETURN(MPIO_ERR_PERMISSION_DENIED); if (mem == MPIO_INTERNAL_MEM) sm = &m->internal; if (mem == MPIO_EXTERNAL_MEM) sm = &m->external; @@ -736,8 +760,41 @@ mpio_memory_debug(mpio_t *m, mpio_mem_t mem) return 0; } +int +mpio_errno(void) +{ + int no = _mpio_errno; + _mpio_errno = 0; + + return no; +} - +char * +mpio_strerror(int errno) +{ + int i; + + printf("mpio_strerror %d\n", errno); + + if (errno >= 0) return NULL; + + for (i = 0; i < mpio_error_num; i++) { + if (mpio_errors[i].id == errno) + return mpio_errors[i].msg; + } + return NULL; +} +void +mpio_perror(char *prefix) +{ + char *msg = mpio_strerror(_mpio_errno); + if (msg == NULL) return; + + if (prefix) + fprintf(stderr, "%s: %s\n", prefix, msg); + else + fprintf(stderr, "%s\n", msg); +} diff --git a/libmpio/mpio.h b/libmpio/mpio.h index b4c381c..7df2349 100644 --- a/libmpio/mpio.h +++ b/libmpio/mpio.h @@ -1,7 +1,7 @@ /* -*- linux-c -*- */ /* - * $Id: mpio.h,v 1.5 2002/09/18 22:18:29 germeier Exp $ + * $Id: mpio.h,v 1.6 2002/09/19 20:46:02 crunchy Exp $ * * Library for USB MPIO-* * @@ -94,6 +94,19 @@ int mpio_sync(mpio_t *, mpio_mem_t); /* context, memory bank */ int mpio_memory_debug(mpio_t *, mpio_mem_t); +/* + * error handling + */ + +/* returns error code of last error */ +int mpio_errno(void); + +/* returns the description of the error */ +char * mpio_strerror(int errno); + +/* prints the error message of the last error*/ +void mpio_perror(char *prefix); + /* * timeline: * --------- diff --git a/mpiosh/callback.c b/mpiosh/callback.c index 798dab6..1a02b04 100644 --- a/mpiosh/callback.c +++ b/mpiosh/callback.c @@ -2,7 +2,7 @@ * * Author: Andreas Büsching * - * $Id: callback.c,v 1.11 2002/09/18 23:17:03 germeier Exp $ + * $Id: callback.c,v 1.12 2002/09/19 20:46:02 crunchy Exp $ * * Copyright (C) 2001 Andreas Büsching * @@ -355,10 +355,9 @@ mpiosh_cmd_mput(char *args[]) for (j = 0; j < size; j++, run++) { if (!(error = regexec(®ex, (*run)->d_name, 0, NULL, 0))) { printf("putting '%s' ... \n", (*run)->d_name); - fsize = mpio_file_put(mpiosh.dev, mpiosh.card, - (*run)->d_name, mpiosh_callback_put); - if ((fsize < 0) && (fsize == MPIO_ERR_NOT_ENOUGH_SPACE)) { - fprintf(stderr, "error: not enough space left on memory card\n"); + if (mpio_file_put(mpiosh.dev, mpiosh.card, + (*run)->d_name, mpiosh_callback_put) == -1) { + mpio_perror("error"); break; } -- cgit v1.2.3