From 8ca6e0c3d3ad7a80ae5645cbfe007b83c4980c66 Mon Sep 17 00:00:00 2001
From: crunchy <crunchy>
Date: Fri, 21 Feb 2003 18:28:50 +0000
Subject: add patch of Sebastian; tried to fix problems with etc stuff

---
 libmpio/directory.c |  93 +++++++++++++++++++++++++++++-
 libmpio/directory.h |   7 ++-
 libmpio/mpio.c      | 161 ++++++++++++++++++++++++++++++++++------------------
 libmpio/mpio.h      |  20 ++++++-
 4 files changed, 221 insertions(+), 60 deletions(-)

(limited to 'libmpio')

diff --git a/libmpio/directory.c b/libmpio/directory.c
index 4ef08f4..387c31c 100644
--- a/libmpio/directory.c
+++ b/libmpio/directory.c
@@ -1,6 +1,6 @@
 /* 
  *
- * $Id: directory.c,v 1.11 2002/11/13 23:05:28 germeier Exp $
+ * $Id: directory.c,v 1.12 2003/02/21 18:28:54 crunchy Exp $
  *
  * Library for USB MPIO-*
  *
@@ -741,6 +741,97 @@ mpio_dentry_delete(mpio_t *m, BYTE mem, BYTE *filename)
   return 0;
 }
 
+void 
+mpio_dentry_move(mpio_t *m,mpio_mem_t mem,BYTE *m_file,BYTE *a_file) {
+  mpio_smartmedia_t *sm;
+
+  BYTE *t0,*t1,*t2,*t3;
+  
+  int  s0,s1,s2,s3;
+  int  m_file_s,a_file_s;
+  BYTE tmp[DIR_SIZE];
+  BYTE *b_file;
+
+  if (mem == MPIO_INTERNAL_MEM) 
+    sm = &m->internal;
+
+  if (mem == MPIO_EXTERNAL_MEM) 
+    sm = &m->external;
+
+  if (m_file == a_file)
+    return;
+
+  m_file_s = mpio_dentry_get_size(m, mem, m_file);
+  a_file_s = mpio_dentry_get_size(m, mem, a_file);
+
+  // -- we determine the 'befor' file. The start of the region which needs to be moved down
+  // -- if a_file == NULL this is the start of the directory.
+
+  if(a_file==NULL) {
+    b_file = sm->dir;
+  } else {
+    b_file = a_file + a_file_s;
+  }
+  
+  if(b_file == m_file) {
+    return;
+  }
+
+  /** We disect the whole directory in four pieces.
+      in different ways according to the direction 
+      the directoy entry is moved (up or down).
+  */
+
+  if(m_file > b_file) {
+    fprintf(stderr,"U ");
+    t0 = sm->dir;
+    s0 = b_file - sm->dir;
+
+    t1 = m_file;
+    s1 = m_file_s;
+    
+    t2 = b_file;
+    s2 = m_file - b_file;
+
+    t3 = m_file + m_file_s;
+    s3 = DIR_SIZE - (m_file-sm->dir) - m_file_s;
+  } else { 
+    fprintf(stderr,"D ");
+    t0 = sm->dir;
+    s0 = m_file - sm->dir;
+    
+    t1 = m_file + m_file_s;
+    s1 = a_file + a_file_s - (m_file + m_file_s);
+
+    t2 = m_file;
+    s2 = m_file_s;
+
+    t3 = b_file;
+    s3 = DIR_SIZE - (b_file - sm->dir);
+  }
+
+  if(s0) {
+    memcpy(tmp,t0,s0);
+  }
+
+  if(s1) {
+    memcpy(tmp + s0 , t1 , s1 );
+  }
+
+  if(s2) {
+    memcpy(tmp + s0 + s1, t2, s2);
+  }
+
+  if(s3) {
+    memcpy(tmp + s0 + s1 + s2, t3, s3);
+  }
+  
+  fprintf(stderr," -- t0=%ld, s0=%d, t1=%ld, s1=%d, t2=%ld, s2=%d, t3=%ld, s3=%d; sum=%d, DIRSIZE=%d\n",
+	  (long)t0,s0,(long)t1,s1,(long)t2,s2,(long)t3,s3,s0+s1+s2+s3,DIR_SIZE);
+
+  memcpy(sm->dir, tmp, DIR_SIZE);
+}
+
 void    
 mpio_dentry_switch(mpio_t *m, mpio_mem_t mem, BYTE *file1, BYTE *file2)
 {
diff --git a/libmpio/directory.h b/libmpio/directory.h
index b30b305..6e54a04 100644
--- a/libmpio/directory.h
+++ b/libmpio/directory.h
@@ -1,6 +1,6 @@
 /* 
  *
- * $Id: directory.h,v 1.6 2002/10/27 17:37:26 germeier Exp $
+ * $Id: directory.h,v 1.7 2003/02/21 18:28:55 crunchy Exp $
  *
  * Library for USB MPIO-*
  *
@@ -53,6 +53,11 @@ 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 *);
 
+/* Move a given file to a new position in the file
+   list	relative to another file.	
+*/
+void	mpio_dentry_move(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 e284872..691d925 100644
--- a/libmpio/mpio.c
+++ b/libmpio/mpio.c
@@ -1,6 +1,6 @@
 /* 
  *
- * $Id: mpio.c,v 1.38 2003/01/16 17:37:07 germeier Exp $
+ * $Id: mpio.c,v 1.39 2003/02/21 18:28:55 crunchy Exp $
  *
  * Library for USB MPIO-*
  *
@@ -46,10 +46,11 @@ void mpio_init_internal(mpio_t *);
 void mpio_init_external(mpio_t *);
 int  mpio_check_filename(mpio_filename_t);
 
-int mpio_file_get_real(mpio_t *, mpio_mem_t, mpio_filename_t, 
+int mpio_file_get_real(mpio_t *, mpio_mem_t, mpio_filename_t, mpio_filename_t,
 		       mpio_callback_t, BYTE **); 
-int mpio_file_put_real(mpio_t *, mpio_mem_t, mpio_filename_t, mpio_filetype_t,
-		      mpio_callback_t, BYTE *, int);
+
+int mpio_file_put_real(mpio_t *, mpio_mem_t, mpio_filename_t, mpio_filename_t,
+		       mpio_filetype_t, mpio_callback_t, BYTE *, int);
 
 static BYTE *mpio_model_name[] = {
   "MPIO-DME",
@@ -96,7 +97,7 @@ mpio_check_filename(mpio_filename_t filename)
 {
   BYTE *p=filename;
   
-  while (p < (filename+MPIO_FILENAME_LEN))
+  while (p < (filename + MPIO_FILENAME_LEN))
     {
       if (*p)
 	return 1;
@@ -114,7 +115,7 @@ mpio_init_internal(mpio_t *m)
 
   /* init main memory parameters */
   sm->manufacturer = m->version[i_offset];
-  sm->id           = m->version[i_offset+1];
+  sm->id           = m->version[i_offset + 1];
   sm->chips        = 1;
   if (mpio_id_valid(m->version[i_offset])) 
     {      
@@ -129,9 +130,9 @@ mpio_init_internal(mpio_t *m)
   
   
   /* look for a second installed memory chip */
-  if (mpio_id_valid(m->version[i_offset+2]))
+  if (mpio_id_valid(m->version[i_offset + 2]))
     {    
-      if(mpio_id2mem(sm->id) != mpio_id2mem(m->version[i_offset+3]))
+      if(mpio_id2mem(sm->id) != mpio_id2mem(m->version[i_offset + 3]))
 	{
 	  printf("Found a MPIO with two chips with different size!");
 	  printf("I'm utterly confused and aborting now, sorry!");
@@ -146,13 +147,13 @@ mpio_init_internal(mpio_t *m)
   mpio_id2geo(sm->id, &sm->geo);
 
   /* read FAT information from spare area */  
-  sm->max_cluster  = sm->size/16*1024;      /* 1 cluster == 16 KB */
+  sm->max_cluster  = sm->size / 16 * 1024;      /* 1 cluster == 16 KB */
   sm->max_blocks   = sm->max_cluster;
-  debugn(2,"max_cluster: %d\n", sm->max_cluster);
+  debugn(2, "max_cluster: %d\n", sm->max_cluster);
                                             /* 16 bytes per cluster */
-  sm->fat_size     = sm->max_cluster*16/SECTOR_SIZE;   
-  debugn(2,"fat_size: %04x\n", sm->fat_size*SECTOR_SIZE);
-  sm->fat          = malloc(sm->fat_size*SECTOR_SIZE);
+  sm->fat_size     = sm->max_cluster * 16 / SECTOR_SIZE;   
+  debugn(2, "fat_size: %04x\n", sm->fat_size * SECTOR_SIZE);
+  sm->fat          = malloc(sm->fat_size * SECTOR_SIZE);
   /* fat will be read in mpio_init, so we can more easily handle
      a callback function */
 
@@ -164,26 +165,24 @@ mpio_init_internal(mpio_t *m)
 void
 mpio_init_external(mpio_t *m)
 {
-  mpio_smartmedia_t *sm=&(m->external);
-  BYTE e_offset=0x20;
+  mpio_smartmedia_t *sm = &(m->external);
+  BYTE e_offset = 0x20;
 
   /* heuristic to find the right offset for the external memory */
-  while((e_offset<0x3a) && !(mpio_id_valid(m->version[e_offset])))
+  while((e_offset < 0x3a) && !(mpio_id_valid(m->version[e_offset])))
     e_offset++;
   
   if (mpio_id_valid(m->version[e_offset]))
     {
-      sm->manufacturer=m->version[e_offset];
-      sm->id=m->version[e_offset+1];
+      sm->manufacturer = m->version[e_offset];
+      sm->id = m->version[e_offset + 1];
     } else {
-      sm->manufacturer=0;
-      sm->id=0;
-      sm->size=0;
-      sm->chips=0;      
+      sm->manufacturer = 0;
+      sm->id = 0;
     }  
 
   /* init memory parameters if external memory is found */
-  if (sm->id!=0) 
+  if (sm->id != 0) 
     {
       /* Read things from external memory (if available) */
       sm->size  = mpio_id2mem(sm->id);
@@ -200,7 +199,7 @@ mpio_init_external(mpio_t *m)
 	}
       
       /* for reading the spare area later! */
-      sm->max_blocks  = sm->size/16*1024;      /* 1 cluster == 16 KB */
+      sm->max_blocks  = sm->size / 16 * 1024;      /* 1 cluster == 16 KB */
       sm->spare       = malloc(sm->max_blocks * 0x10);
     }
 }
@@ -213,6 +212,7 @@ mpio_init(mpio_callback_init_t progress_callback)
   int id_offset;
 
   new_mpio = malloc(sizeof(mpio_t));
+
   if (!new_mpio) {
     debug ("Error allocating memory for mpio_t");
     return NULL;
@@ -222,26 +222,23 @@ mpio_init(mpio_callback_init_t progress_callback)
 
   if (new_mpio->fd < 0) {
     
-    debug ("Could not open %s\n"
-	   "Verify that the mpio module is loaded and "
-	   "your MPIO is\nconnected and powered up.\n\n" , MPIO_DEVICE);
+    debug("Could not open %s\n"
+	  "Verify that the mpio module is loaded and "
+	  "your MPIO is\nconnected and powered up.\n\n" , MPIO_DEVICE);
     
     return NULL;    
   }
-
-  /* "just" to be sure */
-  memset(new_mpio, 0, sizeof(mpio_t));
   
   /* Read Version Information */
   mpio_io_version_read(new_mpio, new_mpio->version);  
 
   /* fill in values */
-  snprintf(new_mpio->firmware.id, 12, "%s", new_mpio->version);
+  snprintf(new_mpio->firmware.id,   12, "%s", new_mpio->version);
   snprintf(new_mpio->firmware.major, 3, "%s", new_mpio->version + 0x0c);
   snprintf(new_mpio->firmware.minor, 3, "%s", new_mpio->version + 0x0e);
-  snprintf(new_mpio->firmware.year, 5, "%s", new_mpio->version + 0x10);
+  snprintf(new_mpio->firmware.year,  5, "%s", new_mpio->version + 0x10);
   snprintf(new_mpio->firmware.month, 3, "%s", new_mpio->version + 0x14);
-  snprintf(new_mpio->firmware.day, 3, "%s", new_mpio->version + 0x16);
+  snprintf(new_mpio->firmware.day,   3, "%s", new_mpio->version + 0x16);
 
   /* there are different identification strings! */
   if (strncmp(new_mpio->version, "MPIO", 4) != 0)
@@ -414,29 +411,38 @@ mpio_get_info(mpio_t *m, mpio_info_t *info)
       
 }
 
+
+int
+mpio_file_get_as(mpio_t *m, mpio_mem_t mem, mpio_filename_t filename,
+		 mpio_filename_t as, mpio_callback_t progress_callback)
+{
+  return mpio_file_get_real(m, mem, filename, as, progress_callback, NULL);
+}
+
 int
 mpio_file_get(mpio_t *m, mpio_mem_t mem, mpio_filename_t filename, 
 	      mpio_callback_t progress_callback)
 {
-  return mpio_file_get_real(m, mem, filename, progress_callback, NULL);
+  return mpio_file_get_real(m, mem, filename, NULL, progress_callback, NULL);
 }
 
 int
 mpio_file_get_to_memory(mpio_t *m, mpio_mem_t mem, mpio_filename_t filename, 
 			mpio_callback_t progress_callback, BYTE **memory)
 {
-  return mpio_file_get_real(m, mem, filename, progress_callback, memory);
+  return mpio_file_get_real(m, mem, filename, NULL, progress_callback, memory);
 }
 
 int
-mpio_file_get_real(mpio_t *m, mpio_mem_t mem, mpio_filename_t filename, 
-	      mpio_callback_t progress_callback, BYTE **memory)
+mpio_file_get_real(mpio_t *m, mpio_mem_t mem, mpio_filename_t filename,
+		   mpio_filename_t as, mpio_callback_t progress_callback,
+		   BYTE **memory)
 {
   mpio_smartmedia_t *sm;
   BYTE block[BLOCK_SIZE];
   int fd, towrite;
   BYTE   *p;
-  mpio_fatentry_t *f=0;
+  mpio_fatentry_t *f = 0;
   struct utimbuf utbuf;
   long mtime;
   DWORD filesize, fsize;
@@ -448,6 +454,10 @@ mpio_file_get_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(as==NULL) {
+    as = filename;
+  }
+
   /* find file */
   p = mpio_dentry_find_name(m, mem, filename);
   if (!p)
@@ -464,7 +474,7 @@ mpio_file_get_real(mpio_t *m, mpio_mem_t mem, mpio_filename_t filename,
 	*memory = malloc(filesize);
       } else {
 	unlink(filename);    
-	fd = open(filename, (O_RDWR | O_CREAT), (S_IRWXU | S_IRGRP | S_IROTH));
+	fd = open(as, (O_RDWR | O_CREAT), (S_IRWXU | S_IRGRP | S_IROTH));
       }
     
     do
@@ -523,12 +533,20 @@ mpio_file_get_real(mpio_t *m, mpio_mem_t mem, mpio_filename_t filename,
   return (fsize-filesize);
 }
 
+int
+mpio_file_put_as(mpio_t *m, mpio_mem_t mem, mpio_filename_t filename,
+		 mpio_filename_t as, mpio_filetype_t filetype,
+		 mpio_callback_t progress_callback)
+{
+  return mpio_file_put_real(m, mem, filename, as, filetype, 
+			    progress_callback, NULL,0);
+}
+
 int
 mpio_file_put(mpio_t *m, mpio_mem_t mem, mpio_filename_t filename, 
-	      mpio_filetype_t filetype,
-	      mpio_callback_t progress_callback)
+	      mpio_filetype_t filetype, mpio_callback_t progress_callback)
 {
-  return mpio_file_put_real(m, mem, filename, filetype, 
+  return mpio_file_put_real(m, mem, filename, NULL, filetype, 
 			    progress_callback, NULL,0);
 }
 
@@ -538,13 +556,13 @@ mpio_file_put_from_memory(mpio_t *m, mpio_mem_t mem, mpio_filename_t filename,
 			  mpio_callback_t progress_callback,
 			  BYTE *memory, int memory_size)
 {
-  return mpio_file_put_real(m, mem, filename, filetype, 
+  return mpio_file_put_real(m, mem, filename,  NULL, filetype,
 			    progress_callback, memory, memory_size);
 }
 
 int
-mpio_file_put_real(mpio_t *m, mpio_mem_t mem, mpio_filename_t filename, 
-		   mpio_filetype_t filetype,
+mpio_file_put_real(mpio_t *m, mpio_mem_t mem, mpio_filename_t i_filename,
+		   mpio_filename_t o_filename, mpio_filetype_t filetype,
 		   mpio_callback_t progress_callback,
 		   BYTE *memory, int memory_size)
 {
@@ -556,11 +574,15 @@ mpio_file_put_real(mpio_t *m, mpio_mem_t mem, mpio_filename_t filename,
   struct stat file_stat;
   struct tm tt;
 
-  BYTE *p=NULL;
+  BYTE *p = NULL;
   DWORD filesize, fsize, free, blocks;
-  BYTE abort=0;
+  BYTE abort = 0;
 
-  MPIO_CHECK_FILENAME(filename);
+  if(o_filename == NULL) {
+    o_filename = i_filename;
+  }
+
+  MPIO_CHECK_FILENAME(o_filename);
   
   if (mem==MPIO_INTERNAL_MEM) sm=&m->internal;  
   if (mem==MPIO_EXTERNAL_MEM) sm=&m->external;
@@ -570,8 +592,8 @@ mpio_file_put_real(mpio_t *m, mpio_mem_t mem, mpio_filename_t filename,
       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);
+      if (stat((const char *)i_filename, &file_stat)!=0) {
+	debug("could not find file: %s\n", i_filename);
 	MPIO_ERR_RETURN(MPIO_ERR_FILE_NOT_FOUND);
       }
       fsize=filesize=file_stat.st_size;
@@ -586,9 +608,9 @@ mpio_file_put_real(mpio_t *m, mpio_mem_t mem, mpio_filename_t filename,
   }
 
   /* check if filename already exists */
-  p = mpio_dentry_find_name(m, mem, filename);
+  p = mpio_dentry_find_name(m, mem, o_filename);
   if (!p)
-      p = mpio_dentry_find_name_8_3(m, mem, filename);
+      p = mpio_dentry_find_name_8_3(m, mem, o_filename);
   if (p) 
     {
       debug("filename already exists\n");
@@ -626,10 +648,10 @@ mpio_file_put_real(mpio_t *m, mpio_mem_t mem, mpio_filename_t filename,
   if (!memory)
     {      
       /* open file for writing */
-      fd = open(filename, O_RDONLY);    
+      fd = open(i_filename, O_RDONLY);    
       if (fd==-1) 
 	{
-	  debug("could not open file: %s\n", filename);
+	  debug("could not open file: %s\n", i_filename);
 	  MPIO_ERR_RETURN(MPIO_ERR_FILE_NOT_FOUND);
 	}
     }
@@ -742,7 +764,7 @@ mpio_file_put_real(mpio_t *m, mpio_mem_t mem, mpio_filename_t filename,
       
     } else {
       mpio_dentry_put(m, mem,
-		      filename, strlen(filename),
+		      o_filename, strlen(o_filename),
 		      ((memory)?mktime(&tt):file_stat.st_ctime), 
 		      fsize, start);
     }  
@@ -773,8 +795,35 @@ mpio_file_switch(mpio_t *m, mpio_mem_t mem,
   return 0;
 }
 
+int mpio_file_move(mpio_t *m,mpio_mem_t mem, mpio_filename_t file,
+		   mpio_filename_t after)
+{
+  BYTE *p1,*p2;
+
+  if( ( p1 = mpio_dentry_find_name(m,mem,file)) == NULL ) {
+    if( (p1 = mpio_dentry_find_name_8_3(m, mem, file)) == NULL ) {
+      MPIO_ERR_RETURN(MPIO_ERR_FILE_NOT_FOUND);
+    }
+  }
+  
+  if(after!=NULL) {
+    if( ( p2 = mpio_dentry_find_name(m,mem,after)) == NULL ) {
+      if( (p2 = mpio_dentry_find_name_8_3(m, mem, after)) == NULL ) {
+	MPIO_ERR_RETURN(MPIO_ERR_FILE_NOT_FOUND);
+      }
+    }
+  }
+
+  fprintf(stderr," -- moving '%s' after '%s'\n",file,after);
+
+  mpio_dentry_move(m,mem,p1,p2);
+
+  return 0;
+}
+
 int
-mpio_memory_format(mpio_t *m, mpio_mem_t mem, mpio_callback_t progress_callback)
+mpio_memory_format(mpio_t *m, mpio_mem_t mem,
+		   mpio_callback_t progress_callback)
 {
   int data_offset;
   mpio_smartmedia_t *sm;
diff --git a/libmpio/mpio.h b/libmpio/mpio.h
index 230ee9a..152c27a 100644
--- a/libmpio/mpio.h
+++ b/libmpio/mpio.h
@@ -1,7 +1,7 @@
 #/* -*- linux-c -*- */
 
 /* 
- * $Id: mpio.h,v 1.13 2002/11/13 23:05:28 germeier Exp $
+ * $Id: mpio.h,v 1.14 2003/02/21 18:28:56 crunchy Exp $
  *
  * Library for USB MPIO-*
  *
@@ -85,6 +85,9 @@ int	mpio_dentry_get(mpio_t *, mpio_mem_t, BYTE *, BYTE *, int,WORD *,
  * reading/writing/deleting of files
  */
 
+int	mpio_file_get_as(mpio_t *, mpio_mem_t, mpio_filename_t,
+			 mpio_filename_t,mpio_callback_t); 
+
 /* context, memory bank, filename, callback */
 int	mpio_file_get(mpio_t *, mpio_mem_t, mpio_filename_t, mpio_callback_t); 
 
@@ -92,6 +95,11 @@ int	mpio_file_get(mpio_t *, mpio_mem_t, mpio_filename_t, mpio_callback_t);
 int	mpio_file_put(mpio_t *, mpio_mem_t, mpio_filename_t, mpio_filetype_t,
 		      mpio_callback_t); 
 
+/* context, memory bank, filename, as, filetype, callback */
+int	mpio_file_put_as(mpio_t *, mpio_mem_t, mpio_filename_t,
+			 mpio_filename_t, mpio_filetype_t,
+			 mpio_callback_t); 
+
 /* context, memory bank, filename, callback */
 int	mpio_file_del(mpio_t *, mpio_mem_t, mpio_filename_t, mpio_callback_t); 
 
@@ -118,11 +126,19 @@ int	mpio_file_put_from_memory(mpio_t *, mpio_mem_t, mpio_filename_t,
 /* context, memory bank, filename, filename */
 int	mpio_file_switch(mpio_t *, mpio_mem_t, 
 			 mpio_filename_t, mpio_filename_t);
+  
+
+
+/* Move a named file after a given file. If after==NULL move it
+   to the top of the list,
+*/
+  
+int mpio_file_move(mpio_t *,mpio_mem_t m,mpio_filename_t,mpio_filename_t);
 
 /* 
  * formating a memory (internal mem or external SmartMedia card)
  */
-
+  
 /* context, memory bank, callback */
 int	mpio_memory_format(mpio_t *, mpio_mem_t, mpio_callback_t); 
 
-- 
cgit v1.2.3