From 037e02905b802686be908d4f8cd989f0c5002b99 Mon Sep 17 00:00:00 2001 From: germeier Date: Sun, 27 Oct 2002 17:37:24 +0000 Subject: implemented the switch command finished support for writing files directly from memory (for config files) --- ChangeLog | 11 +++++- libmpio/directory.c | 75 +++++++++++++++++++++++++---------- libmpio/directory.h | 5 ++- libmpio/mpio.c | 112 ++++++++++++++++++++++++++++++++++++++++------------ libmpio/mpio.h | 13 ++++-- mpiosh/callback.c | 71 ++++++++++++++++++++++++++++++--- 6 files changed, 231 insertions(+), 56 deletions(-) diff --git a/ChangeLog b/ChangeLog index e3d80e5..e316eb2 100644 --- a/ChangeLog +++ b/ChangeLog @@ -9,7 +9,16 @@ another fix for the 8.3 handling needed for the config file (one day this will be bug free ...) * mpiosh/callback.c (mpiosh_cmd_config): implement the function - + * libmpio/directory.c (mpio_dentry_find_name_8_3): + another fix for the 8.3 handling needed for the config file + (GRR, one day ...) + * libmpio/mpio.c (mpio_get_model): added function + * mpiosh/callback.c (mpiosh_cmd_switch): implement the function + * libmpio/mpio.c (mpio_file_switch): added function + * mpiosh/callback.c (mpiosh_cmd_mget): added error message + * libmpio/mpio.c (mpio_file_put_real): + updates to support reading files into memory + 2002-10-26 Markus Germeier * libmpio/mpio.c (mpio_close): fixed segv diff --git a/libmpio/directory.c b/libmpio/directory.c index 4795b3d..c5bdc8e 100644 --- a/libmpio/directory.c +++ b/libmpio/directory.c @@ -1,6 +1,6 @@ /* * - * $Id: directory.c,v 1.9 2002/10/27 02:45:28 germeier Exp $ + * $Id: directory.c,v 1.10 2002/10/27 17:37:25 germeier Exp $ * * Library for USB MPIO-* * @@ -607,26 +607,10 @@ mpio_dentry_find_name_8_3(mpio_t *m, BYTE mem, BYTE *filename) WORD wdummy; BYTE fname[129]; BYTE fname_8_3[13]; - BYTE filename_8_3[13]; DWORD ddummy; BYTE *found = 0; int i, j, len; - /* format given filename to a "standard" 8.3 filename */ - memset(filename_8_3, 0x20, 13); - filename_8_3[12]=0; - len=strlen(filename); - i=j=0; - while ((i<13) && (j internal; + if (mem == MPIO_EXTERNAL_MEM) sm = &m->external; + + if (file1 == file2) + return; + + if (file1dir) + { + memcpy(current, sm->dir, p1 - sm->dir); + current += (p1 - sm->dir); + } + /* the second file*/ + memcpy(current, p2, size2); + current += size2; + /* between the files */ + memcpy(current, p1+size1, (p2-p1-size1)); + current += (p2-p1-size1); + /* the first file */ + memcpy(current, p1, size1); + current += size1; + /* and the rest */ + memcpy(current, p2+size2, (sm->dir+DIR_SIZE-p2-size2)); + + /* really update the directory */ + memcpy(sm->dir, tmp, DIR_SIZE); + + return; +} diff --git a/libmpio/directory.h b/libmpio/directory.h index e3fe54d..b30b305 100644 --- a/libmpio/directory.h +++ b/libmpio/directory.h @@ -1,6 +1,6 @@ /* * - * $Id: directory.h,v 1.5 2002/10/26 13:07:43 germeier Exp $ + * $Id: directory.h,v 1.6 2002/10/27 17:37:26 germeier Exp $ * * Library for USB MPIO-* * @@ -50,6 +50,9 @@ int mpio_dentry_get_filesize(mpio_t *, mpio_mem_t, BYTE *); long mpio_dentry_get_time(mpio_t *, mpio_mem_t, BYTE *); mpio_fatentry_t *mpio_dentry_get_startcluster(mpio_t *, mpio_mem_t, BYTE *); +/* switch two directory entries */ +void mpio_dentry_switch(mpio_t *, mpio_mem_t, BYTE *, BYTE *); + /* helper functions */ void mpio_dentry_copy_from_slot(BYTE *, mpio_dir_slot_t *); void mpio_dentry_copy_to_slot(BYTE *, mpio_dir_slot_t *); diff --git a/libmpio/mpio.c b/libmpio/mpio.c index e2a6ec5..2bcdd5c 100644 --- a/libmpio/mpio.c +++ b/libmpio/mpio.c @@ -1,6 +1,6 @@ /* * - * $Id: mpio.c,v 1.35 2002/10/27 02:45:28 germeier Exp $ + * $Id: mpio.c,v 1.36 2002/10/27 17:37:27 germeier Exp $ * * Library for USB MPIO-* * @@ -353,6 +353,21 @@ mpio_close(mpio_t *m) } } +mpio_model_t +mpio_get_model(mpio_t *m) +{ + mpio_model_t r; + + if (m) + { + r = m->model; + } else { + r = MPIO_MODEL_UNKNOWN; + } + + return r; +} + void mpio_get_info(mpio_t *m, mpio_info_t *info) { @@ -532,6 +547,7 @@ mpio_file_put_real(mpio_t *m, mpio_mem_t mem, mpio_filename_t filename, BYTE block[BLOCK_SIZE]; int fd, toread; struct stat file_stat; + struct tm tt; BYTE *p=NULL; DWORD filesize, fsize, free, blocks; @@ -542,13 +558,19 @@ mpio_file_put_real(mpio_t *m, mpio_mem_t mem, mpio_filename_t filename, if (mem==MPIO_INTERNAL_MEM) sm=&m->internal; if (mem==MPIO_EXTERNAL_MEM) sm=&m->external; - if (stat((const char *)filename, &file_stat)!=0) { - debug("could not find file: %s\n", filename); - MPIO_ERR_RETURN(MPIO_ERR_FILE_NOT_FOUND); - } - fsize=filesize=file_stat.st_size; - debugn(2, "filesize: %d\n", fsize); - + if (memory) + { + fsize=filesize=memory_size; + tt = (struct tm){ 0, 0, 0, 0, 0, 0, 0, 0, 0}; + } else { + if (stat((const char *)filename, &file_stat)!=0) { + debug("could not find file: %s\n", filename); + MPIO_ERR_RETURN(MPIO_ERR_FILE_NOT_FOUND); + } + fsize=filesize=file_stat.st_size; + debugn(2, "filesize: %d\n", fsize); + } + /* check if there is enough space left */ mpio_memory_free(m, mem, &free); if (free*1024i_fat[0x03]= blocks & 0xff; } - /* open file for writing */ - fd = open(filename, O_RDONLY); - if (fd==-1) - { - debug("could not open file: %s\n", filename); - MPIO_ERR_RETURN(MPIO_ERR_FILE_NOT_FOUND); + if (!memory) + { + /* open file for writing */ + fd = open(filename, O_RDONLY); + if (fd==-1) + { + debug("could not open file: %s\n", filename); + MPIO_ERR_RETURN(MPIO_ERR_FILE_NOT_FOUND); + } } while ((filesize>BLOCK_SIZE) && (!abort)) { @@ -610,11 +635,16 @@ mpio_file_put_real(mpio_t *m, mpio_mem_t mem, mpio_filename_t filename, toread=filesize; } - if (read(fd, block, toread)!=toread) { - debug("error reading file data\n"); - close(fd); - MPIO_ERR_RETURN(MPIO_ERR_READING_FILE); - } + if (memory) + { + memcpy(block, memory+(fsize-filesize), toread); + } else { + if (read(fd, block, toread)!=toread) { + debug("error reading file data\n"); + close(fd); + MPIO_ERR_RETURN(MPIO_ERR_READING_FILE); + } + } filesize -= toread; /* get new free block from FAT and write current block out */ @@ -640,18 +670,24 @@ mpio_file_put_real(mpio_t *m, mpio_mem_t mem, mpio_filename_t filename, toread=filesize; } - if (read(fd, block, toread)!=toread) { - debug("error reading file data\n"); - close(fd); - MPIO_ERR_RETURN(MPIO_ERR_READING_FILE); - } + if (memory) + { + memcpy(block, memory+(fsize-filesize), toread); + } else { + if (read(fd, block, toread)!=toread) { + debug("error reading file data\n"); + close(fd); + MPIO_ERR_RETURN(MPIO_ERR_READING_FILE); + } + } filesize -= toread; /* mark end of FAT chain and write last block */ mpio_fatentry_set_eof(m ,mem, f); mpio_io_block_write(m, mem, f, block); - close(fd); + if (!memory) + close(fd); if (progress_callback) (*progress_callback)((fsize-filesize), fsize); @@ -700,12 +736,36 @@ mpio_file_put_real(mpio_t *m, mpio_mem_t mem, mpio_filename_t filename, } else { mpio_dentry_put(m, mem, filename, strlen(filename), - file_stat.st_ctime, fsize, start); + ((memory)?mktime(&tt):file_stat.st_ctime), + fsize, start); } return fsize-filesize; } +int +mpio_file_switch(mpio_t *m, mpio_mem_t mem, + mpio_filename_t file1, mpio_filename_t file2) +{ + BYTE *p1, *p2; + + /* find files */ + p1 = mpio_dentry_find_name(m, mem, file1); + if (!p1) + p1 = mpio_dentry_find_name_8_3(m, mem, file1); + + p2 = mpio_dentry_find_name(m, mem, file2); + if (!p2) + p2 = mpio_dentry_find_name_8_3(m, mem, file2); + + if ((!p1) || (!p2)) + MPIO_ERR_RETURN(MPIO_ERR_FILE_NOT_FOUND); + + mpio_dentry_switch(m, mem, p1, p2); + + return 0; +} + int mpio_memory_format(mpio_t *m, mpio_mem_t mem, mpio_callback_t progress_callback) { diff --git a/libmpio/mpio.h b/libmpio/mpio.h index 51decef..3f0205c 100644 --- a/libmpio/mpio.h +++ b/libmpio/mpio.h @@ -1,7 +1,7 @@ #/* -*- linux-c -*- */ /* - * $Id: mpio.h,v 1.11 2002/10/27 02:45:28 germeier Exp $ + * $Id: mpio.h,v 1.12 2002/10/27 17:37:27 germeier Exp $ * * Library for USB MPIO-* * @@ -58,8 +58,8 @@ void mpio_close(mpio_t *); /* get version */ void mpio_get_info(mpio_t *, mpio_info_t *); -/* print version: deprecated */ -//void mpio_print_version(mpio_t *); +/* get model */ +mpio_model_t mpio_get_model(mpio_t *); /* retrieves free memory in bytes */ int mpio_memory_free(mpio_t *, mpio_mem_t, int *free); @@ -106,6 +106,13 @@ int mpio_file_put_from_memory(mpio_t *, mpio_mem_t, mpio_filename_t, mpio_filetype_t, mpio_callback_t, BYTE *, int); +/* + * switch position of two files + */ +/* context, memory bank, filename, filename */ +int mpio_file_switch(mpio_t *, mpio_mem_t, + mpio_filename_t, mpio_filename_t); + /* * formating a memory (internal mem or external SmartMedia card) */ diff --git a/mpiosh/callback.c b/mpiosh/callback.c index e9d7a0a..24cb402 100644 --- a/mpiosh/callback.c +++ b/mpiosh/callback.c @@ -2,7 +2,7 @@ * * Author: Andreas Büsching * - * $Id: callback.c,v 1.28 2002/10/27 02:45:28 germeier Exp $ + * $Id: callback.c,v 1.29 2002/10/27 17:37:27 germeier Exp $ * * Copyright (C) 2001 Andreas Büsching * @@ -297,6 +297,7 @@ mpiosh_cmd_mget(char *args[]) BYTE month, day, hour, minute; WORD year; DWORD fsize; + int found; MPIOSH_CHECK_CONNECTION_CLOSED; MPIOSH_CHECK_ARG; @@ -308,6 +309,7 @@ mpiosh_cmd_mget(char *args[]) regerror(error, ®ex, errortext, 100); debugn (2, "error in regular expression: %s (%s)\n", args[i], errortext); } else { + found = 0; p = mpio_directory_open(mpiosh.dev, mpiosh.card); while (p != NULL) { memset(fname, '\0', 100); @@ -315,13 +317,14 @@ mpiosh_cmd_mget(char *args[]) &year, &month, &day, &hour, &minute, &fsize); if (!(error = regexec(®ex, fname, 0, NULL, 0))) { + found = 1; printf("getting '%s' ... \n", fname); if ((mpio_file_get(mpiosh.dev, mpiosh.card, fname, mpiosh_callback_get)) == -1) { debug("cancelled operation\n"); mpio_perror("error"); break; - } + } printf("\n"); if (mpiosh_cancel) { debug("operation cancelled by user\n"); @@ -335,9 +338,12 @@ mpiosh_cmd_mget(char *args[]) p = mpio_dentry_next(mpiosh.dev, mpiosh.card, p); } } + if (!found) + printf("file not found! (%s)\n", args[i]); i++; } + regfree(®ex); } @@ -610,7 +616,19 @@ mpiosh_cmd_switch(char *args[]) { MPIOSH_CHECK_CONNECTION_CLOSED; - UNUSED(args); + if(args[0] && args[1] && !args[2]) { + if ((mpio_file_switch(mpiosh.dev, mpiosh.card, + args[0], args[1])) == -1) { + mpio_perror("error"); + } else { + mpio_sync(mpiosh.dev, mpiosh.card); + } + + } else { + fprintf(stderr, "error: wrong number of arguments given\n"); + printf("switch \n"); + } + } void @@ -655,8 +673,38 @@ mpiosh_cmd_config(char *args[]) MPIO_CONFIG_FILE, NULL, &config_data))<=0) { fprintf(stderr, "Could not read config file\n"); } else { - hexdumpn(0, config_data, size); - fprintf(stderr, "please implement me!\n"); + hexdump(config_data, size); + + printf("current config:\n" + "===============\n"); + + printf("volume:\t\t%-2d\n", config_data[0x00]); + printf("repeat:\t\t"); + switch(config_data[0x01]) + { + case 0: + printf("normal\n"); + break; + case 1: + printf("repeat one\n"); + break; + case 2: + printf("repeat all\n"); + break; + case 3: + printf("shuffel\n"); + break; + case 4: + printf("intro\n"); + break; + default: + printf("unknown\n" ); + } + + + fprintf(stderr, "\nfinish my implementation please!\n"); + fprintf(stderr, "config values seem to be model dependant :-(\n"); + free(config_data); } } else { @@ -719,6 +767,19 @@ mpiosh_cmd_channel(char *args[]) if (!i) printf("no channel defined!\n"); + memcpy(channel_data, "mager", 5); + + printf("deleting old config file ...\n"); + mpio_file_del(mpiosh.dev, MPIO_INTERNAL_MEM, + MPIO_CHANNEL_FILE, NULL); + printf("writing new config file ...\n"); + if (mpio_file_put_from_memory(mpiosh.dev, MPIO_INTERNAL_MEM, + MPIO_CHANNEL_FILE, + FTYPE_CHAN, NULL, + channel_data, size)==-1) + mpio_perror("error"); + mpio_sync(mpiosh.dev, MPIO_INTERNAL_MEM); + free(channel_data); } } else { -- cgit v1.2.3