From 7935e28c8e5956849f1a1432dc7ae8e5ef4ea414 Mon Sep 17 00:00:00 2001 From: germeier Date: Sat, 19 Apr 2003 23:58:02 +0000 Subject: added function(s) to rename files on the player --- ChangeLog | 8 ++ libmpio/directory.c | 339 ++++++++++++++++++++++++++++++---------------------- libmpio/directory.h | 5 +- libmpio/mpio.c | 21 +++- libmpio/mpio.h | 11 +- mpiosh/callback.c | 21 +++- mpiosh/callback.h | 3 +- mpiosh/global.c | 7 +- 8 files changed, 265 insertions(+), 150 deletions(-) diff --git a/ChangeLog b/ChangeLog index 9456f2c..1117487 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,11 @@ +2003-04-20 Markus Germeier + + * libmpio/directory.c (mpio_dentry_filename_write): + split this function from mpio_dentry_put to make a rename easier + * libmpio/mpio.c (mpio_file_rename): + libmpio/directory.c (mpio_dentry_rename): + new functions + 2003-04-19 Markus Germeier * libmpio/id3.h: diff --git a/libmpio/directory.c b/libmpio/directory.c index aa62173..6f0e82d 100644 --- a/libmpio/directory.c +++ b/libmpio/directory.c @@ -1,6 +1,6 @@ /* * - * $Id: directory.c,v 1.17 2003/04/11 21:42:57 germeier Exp $ + * $Id: directory.c,v 1.18 2003/04/19 23:58:02 germeier Exp $ * * Library for USB MPIO-* * @@ -421,6 +421,155 @@ mpio_directory_pwd(mpio_t *m, mpio_mem_t mem, BYTE pwd[INFO_LINE]) return; } +mpio_dir_entry_t * +mpio_dentry_filename_write(mpio_t *m, mpio_mem_t mem, BYTE *p, + BYTE *filename, int filename_size) +{ + BYTE *unicode = 0; + BYTE *back, *fback; + BYTE *fname = 0; + iconv_t ic; + int in = 0, out = 0; + int fin = 0, fout = 0; + int count = 0; + BYTE index; + BYTE f_8_3[13]; + mpio_dir_slot_t *slot; + mpio_dir_entry_t *dentry; + int i, j, points; + + /* generate vfat filename in UNICODE */ + ic = iconv_open(UNICODE, m->charset); + fin = in = filename_size + 1; + fout = out = filename_size * 2 + 2 + 26; + fname = malloc(in); + fback = fname; + unicode = malloc(out); + back = unicode; + + memset(fname, 0, in); + snprintf(fname, in, "%s", filename); + memset(unicode, 0xff, out); + iconv(ic, (char **)&fback, &fin, (char **)&back, &fout); + iconv_close(ic); + hexdump(fname, in); + hexdump(unicode, out); + + back = unicode; + + count = filename_size / 13; + if (filename_size % 13) + count++; + + slot = (mpio_dir_slot_t *)p; + + index = 0x40 + count; + while (count > 0) { + mpio_dentry_copy_to_slot(back + ((count - 1) * 26), slot); + hexdump((char *)back + ((count - 1) * 26), 0x20); + slot->id = index; + slot->attr = 0x0f; + slot->reserved = 0x00; + slot->start[0] = 0x00; + slot->start[1] = 0x00; + /* FIXME: */ + slot->alias_checksum = 0x00; // checksum for 8.3 alias + + hexdump((char *)slot, 0x20); + + slot++; + count--; + index = count; + } + +/* memcpy(p, m->internal.dir+0x220, 0x20); */ + +/* p+=0x20; */ + dentry = (mpio_dir_entry_t *)slot; + + /* find uniq 8.3 filename */ + memset(f_8_3, 0x20, 12); + f_8_3[8]='.'; + f_8_3[12]=0x00; + + i=0; + points=0; + /* count points to later find the correct file extension */ + while (i<(strlen(filename))) + { + if (filename[i] == '.') + points++; + i++; + } + + /* if we do not find any points we set the value ridiculously high, + then everything falls into place */ + if (!points) + points=1024*1024; + + i=j=0; + while ((j<8) && (points) && (i<(strlen(filename)))) + { + if (filename[i] == '.') + { + points--; + } else { + if (filename[i]!=' ') { + f_8_3[j] = toupper(filename[i]); + j++; + } + } + i++; + } + + j=i; + while((points) && (j<(strlen(filename)))) + { + if (filename[j] == '.') + points--; + j++; + } + + i=9; + while ((i<12) && (j<(strlen(filename)))) + { + f_8_3[i] = toupper(filename[j]); + i++; + j++; + } + + /* This seems like a special case to me! */ + if (strcmp(MPIO_MPIO_RECORD, filename)==0) + { + f_8_3[6]='~'; + f_8_3[7]='0'; + } + + if (mpio_dentry_find_name_8_3(m, mem, f_8_3)) + { + f_8_3[6]='~'; + f_8_3[7]='1'; + } + + while(mpio_dentry_find_name_8_3(m, mem, f_8_3)) + f_8_3[7]++; + + +/* memcpy(dentry->name,"AAAAAAAA",8); */ +/* memcpy(dentry->ext,"MP3",3); */ + +/* hexdumpn(0, f_8_3, 13); */ + + memcpy(dentry->name, f_8_3, 8); + memcpy(dentry->ext, f_8_3+9, 3); + + free(unicode); + free(fname); + + return dentry; +} + + int mpio_dentry_get_size(mpio_t *m, mpio_mem_t mem, BYTE *buffer) { @@ -817,19 +966,8 @@ mpio_dentry_put(mpio_t *m, mpio_mem_t mem, BYTE *filename, int filename_size, time_t date, DWORD fsize, WORD ssector, BYTE attr) { - BYTE *unicode = 0; - BYTE *back, *fback; - BYTE *fname = 0; - iconv_t ic; - int in = 0, out = 0; - int fin = 0, fout = 0; - int count = 0; - BYTE index; - BYTE f_8_3[13]; - int i, j, points; BYTE *p; mpio_dir_entry_t *dentry; - mpio_dir_slot_t *slot; /* read and copied code from mtools-3.9.8/directory.c * to make this one right @@ -851,136 +989,11 @@ mpio_dentry_put(mpio_t *m, mpio_mem_t mem, p = m->internal.cdir->dir; } - /* generate vfat filename in UNICODE */ - ic = iconv_open(UNICODE, m->charset); - fin = in = filename_size + 1; - fout = out = filename_size * 2 + 2 + 26; - fname = malloc(in); - fback = fname; - unicode = malloc(out); - back = unicode; - - memset(fname, 0, in); - snprintf(fname, in, "%s", filename); - memset(unicode, 0xff, out); - iconv(ic, (char **)&fback, &fin, (char **)&back, &fout); - iconv_close(ic); - hexdump(fname, in); - hexdump(unicode, out); - - back = unicode; - - count = filename_size / 13; - if (filename_size % 13) - count++; - - slot = (mpio_dir_slot_t *)p; - - index = 0x40 + count; - while (count > 0) { - mpio_dentry_copy_to_slot(back + ((count - 1) * 26), slot); - hexdump((char *)back + ((count - 1) * 26), 0x20); - slot->id = index; - slot->attr = 0x0f; - slot->reserved = 0x00; - slot->start[0] = 0x00; - slot->start[1] = 0x00; - /* FIXME: */ - slot->alias_checksum = 0x00; // checksum for 8.3 alias - - hexdump((char *)slot, 0x20); - - slot++; - count--; - index = count; - } - -/* memcpy(p, m->internal.dir+0x220, 0x20); */ - -/* p+=0x20; */ - dentry = (mpio_dir_entry_t *)slot; - - /* find uniq 8.3 filename */ - memset(f_8_3, 0x20, 12); - f_8_3[8]='.'; - f_8_3[12]=0x00; - - i=0; - points=0; - /* count points to later find the correct file extension */ - while (i<(strlen(filename))) - { - if (filename[i] == '.') - points++; - i++; - } - - /* if we do not find any points we set the value ridiculously high, - then everything falls into place */ - if (!points) - points=1024*1024; - - i=j=0; - while ((j<8) && (points) && (i<(strlen(filename)))) - { - if (filename[i] == '.') - { - points--; - } else { - if (filename[i]!=' ') { - f_8_3[j] = toupper(filename[i]); - j++; - } - } - i++; - } - - j=i; - while((points) && (j<(strlen(filename)))) - { - if (filename[j] == '.') - points--; - j++; - } - - i=9; - while ((i<12) && (j<(strlen(filename)))) - { - f_8_3[i] = toupper(filename[j]); - i++; - j++; - } - - /* This seems like a special case to me! */ - if (strcmp(MPIO_MPIO_RECORD, filename)==0) - { - f_8_3[6]='~'; - f_8_3[7]='0'; - } - - if (mpio_dentry_find_name_8_3(m, mem, f_8_3)) - { - f_8_3[6]='~'; - f_8_3[7]='1'; - } - - while(mpio_dentry_find_name_8_3(m, mem, f_8_3)) - f_8_3[7]++; - - -/* memcpy(dentry->name,"AAAAAAAA",8); */ -/* memcpy(dentry->ext,"MP3",3); */ - -/* hexdumpn(0, f_8_3, 13); */ - - memcpy(dentry->name, f_8_3, 8); - memcpy(dentry->ext, f_8_3+9, 3); + dentry = mpio_dentry_filename_write(m, mem, p, filename, filename_size); dentry->attr = attr; dentry->lcase = 0x00; - - /* read and copied code from mtools-3.9.8/directory.c * to make this one right */ @@ -1006,9 +1019,6 @@ mpio_dentry_put(mpio_t *m, mpio_mem_t mem, dentry->start[0] = ssector & 0xff; dentry->start[1] = ssector / 0x100; - free(unicode); - free(fname); - /* what do we want to return? */ return 0; } @@ -1259,3 +1269,50 @@ mpio_dentry_switch(mpio_t *m, mpio_mem_t mem, BYTE *file1, BYTE *file2) return; } + +void +mpio_dentry_rename(mpio_t *m, mpio_mem_t mem, BYTE *p, BYTE *newfilename) +{ + mpio_smartmedia_t *sm; + BYTE *current; + int size1 , size2, offset, offset_d1, offset_d2; + BYTE tmp[DIR_SIZE]; + + if (mem == MPIO_INTERNAL_MEM) sm = &m->internal; + if (mem == MPIO_EXTERNAL_MEM) sm = &m->external; + + current = sm->cdir->dir; + + size1 = mpio_dentry_get_size(m, mem, p) / 0x20; + size2 = (strlen(newfilename) / 13) + 1; + if ((strlen(newfilename) % 13)) + size2++; + + debugn(2, "size1: %d size2: %d\n", size1, size2); + + /* we want to copy the last dentry to the right location + * so we avoid reading and writing the same information + */ + size1--; /* kludge so we can do compares without worry */ + size2--; + + memcpy(tmp, current, DIR_SIZE); + offset = p - current; + offset_d1 = offset + (size1 * 0x20); + offset_d2 = offset + (size2 * 0x20); + + if (size2>size1) { + /* new filename needs at least one slot more than the old one */ + memcpy(current+offset_d2, tmp+offset_d1, (DIR_SIZE-offset_d1)); + } + + if (size2 * - * $Id: callback.c,v 1.40 2003/04/19 11:41:26 germeier Exp $ + * $Id: callback.c,v 1.41 2003/04/19 23:58:02 germeier Exp $ * * Copyright (C) 2001 Andreas Büsching * @@ -736,6 +736,25 @@ mpiosh_cmd_switch(char *args[]) } +void +mpiosh_cmd_rename(char *args[]) +{ + MPIOSH_CHECK_CONNECTION_CLOSED; + + if(args[0] && args[1] && !args[2]) { + if ((mpio_file_rename(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("rename \n"); + } +} + void mpiosh_cmd_dump_mem(char *args[]) { diff --git a/mpiosh/callback.h b/mpiosh/callback.h index 8d6be74..3d1c8f8 100644 --- a/mpiosh/callback.h +++ b/mpiosh/callback.h @@ -2,7 +2,7 @@ * * Author: Andreas Büsching * - * $Id: callback.h,v 1.10 2003/04/19 09:32:48 germeier Exp $ + * $Id: callback.h,v 1.11 2003/04/19 23:58:02 germeier Exp $ * * Copyright (C) 2001 Andreas Büsching * @@ -49,6 +49,7 @@ void mpiosh_cmd_dump(char *args[]); void mpiosh_cmd_free(char *args[]); void mpiosh_cmd_format(char *args[]); void mpiosh_cmd_switch(char *args[]); +void mpiosh_cmd_rename(char *args[]); void mpiosh_cmd_dump_mem(char *args[]); void mpiosh_cmd_health(char *args[]); void mpiosh_cmd_config(char *args[]); diff --git a/mpiosh/global.c b/mpiosh/global.c index 2f10870..0047627 100644 --- a/mpiosh/global.c +++ b/mpiosh/global.c @@ -2,7 +2,7 @@ * * Author: Andreas Buesching * - * $Id: global.c,v 1.10 2003/04/19 09:32:48 germeier Exp $ + * $Id: global.c,v 1.11 2003/04/19 23:58:02 germeier Exp $ * * Copyright (C) 2001 Andreas Büsching * @@ -50,7 +50,7 @@ struct mpiosh_cmd_t commands[] = { { "ver", NULL, NULL, " version of mpio package", mpiosh_cmd_version, NULL }, - { "help", NULL, "[]", + { "help", (char *[]){ "?", NULL }, "[]", " show information about known commands or just about ", mpiosh_cmd_help, mpiosh_readline_comp_cmd }, { "dir", (char *[]){ "ls", "ll", NULL }, NULL, @@ -105,6 +105,9 @@ struct mpiosh_cmd_t commands[] = { { "switch", NULL, " ", " switches the order of two files", mpiosh_cmd_switch, mpiosh_readline_comp_mpio_file }, + { "rename", (char *[]){ "ren", NULL }, " ", + " renames a file on the current memory card", + mpiosh_cmd_rename, mpiosh_readline_comp_mpio_file }, { "ldir", (char *[]){ "lls", NULL }, NULL, " list local directory", mpiosh_cmd_ldir, NULL }, -- cgit v1.2.3