Ejemplo n.º 1
0
BOOL MakeAbsolutePath(char* workingdir, char* relative, char* result)
{
    char* rel = relative;
    char* temp;
    
    assert(workingdir && relative && result);
      
    if ((relative[0] == '/') || (relative[0] == '\\'))
    {
       strcpy(result, relative); /* path already absolute */
       return TRUE;
    }
    
    strcpy(result, workingdir);
    if (workingdir[0] == 0) RETURN_FTEERROR(FALSE);	
    
    temp = strchr(result, 0);
    if ((*(temp-1) != '/') && (*(temp-1) != '\\'))
       strcat(result, "\\");     
      
    while ((temp = SkipDots(rel)) != NULL)
    {
       if (!GetPreviousDir(result)) 
          RETURN_FTEERROR(FALSE);	     
          
       rel = temp;   
    }

    if ((strlen(result) + strlen(rel)) > MAXPATH)
       RETURN_FTEERROR(FALSE);	
       
    strcat(result, rel);
    
    return TRUE;
}
Ejemplo n.º 2
0
BOOL CopyConsecutiveClusters(RDWRHandle handle, CLUSTER source, CLUSTER dest, unsigned long length)
{
    SECTOR ssource, sdest;
    unsigned long sectorspercluster, i, blocks;
    unsigned rest;
    
    ssource = ConvertToDataSector(handle, source);
    if (!ssource) RETURN_FTEERROR(FALSE);
	
    sdest = ConvertToDataSector(handle, dest);
    if (!sdest) RETURN_FTEERROR(FALSE);
	
    sectorspercluster = GetSectorsPerCluster(handle);
    if (!sectorspercluster) RETURN_FTEERROR(FALSE);
    
    blocks = (length * sectorspercluster) / 32768L;
    rest   = (unsigned)((length * sectorspercluster) % 32768L);
    
    for (i=0; i<blocks; i++)
    {
	if (!CopySectors(handle, ssource, sdest, 32768U))
	    RETURN_FTEERROR(FALSE);
	
	ssource += 32768L;
	sdest   += 32768L;
    }
    
    if (rest > 0)
    {
	if (!CopySectors(handle, ssource, sdest, rest))
	    RETURN_FTEERROR(FALSE);
    }
    
    return TRUE;    
}
Ejemplo n.º 3
0
BOOL RelocateClusterChain(RDWRHandle handle, CLUSTER source, CLUSTER dest)
{
    unsigned long filesize;
    
    /* Start by relocating the first cluster */
    if (!RelocateCluster(handle, source, dest))
	RETURN_FTEERROR(FALSE);
    
    /* After that, just move all the data from the source to the destination,
       adding fat labels to the destination */

    filesize = CalculateFileSize(handle, source);
    if (filesize == FAIL)
	RETURN_FTEERROR(FALSE);
    
    if (!MarkClusterChain(handle, dest, filesize))
	RETURN_FTEERROR(FALSE);
    
    if (!CopyClusterChain(handle, source, dest))
	RETURN_FTEERROR(FALSE);
    
    /* In the end, remove all the fat labels from the source */
    if (!CleanFatLabelChain(handle, source))
	RETURN_FTEERROR(FALSE);
   
    return TRUE;
}
Ejemplo n.º 4
0
static BOOL PreReadLargeCluster(RDWRHandle handle, 
                                SECTOR start, unsigned char amofsectors,
                                char* buftouse, unsigned bufsize)
{
    unsigned blocks = amofsectors / bufsize;
    unsigned rest  = amofsectors % bufsize;
    unsigned i;
    
    for (i = 0; i < blocks; i++)
    {
        if (ReadSectors(handle, bufsize, start, buftouse) == -1)
        {
           RETURN_FTEERROR(FALSE); 
        }  
        
        start += bufsize;
    }
    
    if (rest)
    {
        if (ReadSectors(handle, rest, start, buftouse) == -1)
        {
           RETURN_FTEERROR(FALSE); 
        }      
    }
  
    return TRUE;
}
Ejemplo n.º 5
0
static BOOL RelocateConsecutiveClusterSeq(RDWRHandle handle, 
                                          CLUSTER source, 
                                          CLUSTER destination,                                           
                                          unsigned long flength)
{
    CLUSTER label;
    CLUSTER pSource = destination - 1; /* Only valid in this context */
    
    assert(flength>0);
    
    if (!Relocate1Cluster(handle, source, destination, pSource))
	RETURN_FTEERROR(FALSE);
    
    if (flength == 1) return TRUE;              /* If we have to relocate just one cluster, we're done */

    /* The first cluster is relocated to the destination, the link to break is now there */
    /* After that, just move all the data from the source to the destination,
       adding fat labels to the destination */
    if (!GetNthCluster(handle, source+flength-1, &label))
	RETURN_FTEERROR(FALSE);
    
    if (!CopyClusters(handle, source+1, destination+1, flength-1))
	RETURN_FTEERROR(FALSE);    
    
    if (!MarkClusterChain(handle, source, destination, flength-1, FAT_LAST(label)))
	RETURN_FTEERROR(FALSE);      
  
    /* In the end, remove all the fat labels from the source */
    if (!CleanFatLabels(handle, source, flength))
	RETURN_FTEERROR(FALSE);        
    
    return TRUE;
}
Ejemplo n.º 6
0
static BOOL MarkClusterChain(RDWRHandle handle, CLUSTER source, CLUSTER dest, unsigned long filesize, BOOL atend)
{
    CLUSTER label;
    unsigned long i;    
    
    for (i=0; i<filesize; i++)
    {
	if (!WriteFatLabel(handle, dest+i, dest+i+1))
	    RETURN_FTEERROR(FALSE);
    }

    if (atend)
    {    		 
        if (!WriteFatLabel(handle, dest+filesize, FAT_LAST_LABEL))
	   RETURN_FTEERROR(FALSE);
    }    
    else
    {
	if (!GetNthCluster(handle, source+filesize, &label))
	    RETURN_FTEERROR(FALSE);

	if (!WriteFatLabel(handle, dest+filesize, label))
	    RETURN_FTEERROR(FALSE);
    }
     
    return TRUE;    
}
Ejemplo n.º 7
0
static BOOL GetNextConsecutiveChain(RDWRHandle handle, CLUSTER source, unsigned long* length)
{
    CLUSTER current = source, label;
    
    if (!GetNthCluster(handle, source, &label))
	RETURN_FTEERROR(FALSE);
    
    *length = 0;
    
    while (FAT_NORMAL(label))
    {
	if (label != current+1)
	{
	   return TRUE;        	    	    
	}	
	
	(*length)++;
	
	if (!GetNthCluster(handle, current, &label))
	    RETURN_FTEERROR(FALSE);
    }
    
    (*length)++;
    return TRUE;    
}
Ejemplo n.º 8
0
BOOL RelocateKnownFile(RDWRHandle handle, 
                       struct DirectoryPosition* pos,
                       struct DirectoryEntry* entry,
                       CLUSTER destination)
{
    /* We assume destination long enough to relocate the complete file */
    unsigned long length, flength;
    CLUSTER firstcluster;
    CLUSTER source;
    
    firstcluster = GetFirstCluster(entry);
    length = CalculateFileSize(handle, firstcluster);
    if (length == FAIL) 
        RETURN_FTEERROR(FALSE);    

    /* Small optimization for when length == 1 */

    if (!RelocateFirstCluster(handle, pos, entry, destination))
        RETURN_FTEERROR(FALSE);

    if (length == 1) return TRUE;    
    length--;
    
    /* Change the structures for the relocation */
    firstcluster = GetFirstCluster(entry);  // Value is now different!
    if (!GetNthCluster(handle, firstcluster, &source))
       return FALSE;
    
    destination++;

    while (length > 0)
    {    
        if (!GetNextConsecutiveChain(handle, source, length, &flength))	    
	   RETURN_FTEERROR(FALSE);		

        if (!RelocateConsecutiveClusterSeq(handle, source, destination, flength))
	    RETURN_FTEERROR(FALSE);

        if (!GetNthCluster(handle, destination + flength-1, &source))
	    return FALSE;

	if (FAT_LAST(source))
	    return length == flength;

	destination += flength;
	length -= flength;
    }
      
    return TRUE;
}
Ejemplo n.º 9
0
BOOL PreReadClusterSequence(RDWRHandle handle, CLUSTER start,
                            unsigned long length)
{
    char* ReadBuf;   
    unsigned BufSize;
    unsigned long blocks, i, sectorstoread;
    unsigned rest;
    unsigned char sectorspercluster;
    SECTOR startsector;
    
    if (!CacheActive()) return TRUE;
    if (length == 0) return TRUE;
    
    sectorspercluster = GetSectorsPerCluster(handle);
    if (!sectorspercluster) RETURN_FTEERROR(FALSE); 
    
    sectorstoread = length * sectorspercluster; 
    startsector = ConvertToDataSector(handle, start);
    if (!startsector) RETURN_FTEERROR(FALSE); 
       
    ReadBuf = AllocateReadBuf(sectorstoread * BYTESPERSECTOR, &BufSize);
    BufSize /= BYTESPERSECTOR;
    
    blocks = (length * sectorspercluster) / BufSize;
    rest   = (unsigned)((length * sectorspercluster) % BufSize);
    
    for (i = 0; i < blocks; i++)
    {   
       if (!OnlyFilledRead(handle, startsector, BufSize, ReadBuf) == -1)
        {
           FreeReadBuf(ReadBuf);     
           RETURN_FTEERROR(FALSE); 
        }
        
        startsector += BufSize;
    }
    
    if (rest)
    {           
        if (!OnlyFilledRead(handle, startsector, rest, ReadBuf) == -1)
        {
           FreeReadBuf(ReadBuf);      
           RETURN_FTEERROR(FALSE); 
        }        
    }
    
    FreeReadBuf(ReadBuf); 
    return TRUE;
}
Ejemplo n.º 10
0
static BOOL ClusterPreReader(RDWRHandle handle, CLUSTER label,
                             SECTOR datasector, void** structure)
{
    SECTOR  firstsector;
    CLUSTER cluster;
    unsigned char sectorspercluster;
    struct Pipe* pipe = *((struct Pipe**) structure);
    
    if (label);
   
    sectorspercluster = GetSectorsPerCluster(handle);
    if (!sectorspercluster) return FAIL;
    
    if (pipe->maxindex == 0) /* cluster > read buf */
    {
       if (!PreReadLargeCluster(handle, datasector, sectorspercluster, 
                                pipe->buf, pipe->bufsize))
          RETURN_FTEERROR(FAIL); 
    }
    else
    {
       cluster = DataSectorToCluster(handle, datasector);
       
       if ((pipe->index > 0) &&
           ((pipe->prevcluster != cluster-1) ||
            (pipe->index == pipe->maxindex)))
       {    
          firstsector = ConvertToDataSector(handle, pipe->firstcluster);
          if (!firstsector) RETURN_FTEERROR(FAIL); 
             
          if (ReadSectors(handle, pipe->index*sectorspercluster, 
                          firstsector, pipe->buf) == -1)
          {
              RETURN_FTEERROR(FAIL); 
          }
            
          pipe->index = 0;
          pipe->firstcluster = cluster;
       }
       else
       {
          pipe->index++;
       }                                      
    }
    
    pipe->prevcluster = cluster;
           
    return TRUE;    
}
Ejemplo n.º 11
0
BOOL PreReadClusterChain(RDWRHandle handle, CLUSTER start)
{
    SECTOR  firstsector;
    char* ReadBuf;   
    struct Pipe pipe, *ppipe = &pipe;
    unsigned char sectorspercluster; 
    unsigned BufSize;
    
    if (!CacheActive()) return TRUE;
        
    sectorspercluster = GetSectorsPerCluster(handle);
    if (!sectorspercluster) RETURN_FTEERROR(FALSE); 
    
    ReadBuf = AllocateReadBuf(MAX_ALLOCATING, &BufSize);
    BufSize /= BYTESPERSECTOR;    
        
    pipe.buf          = ReadBuf;
    pipe.bufsize      = BufSize;
    pipe.index        = 0;
    pipe.maxindex     = BufSize / sectorspercluster;
    pipe.prevcluster  = 0;
    pipe.firstcluster = start;
    
    if (!FileTraverseFat(handle, start, ClusterPreReader, (void**) &ppipe))
    {            
       FreeReadBuf(ReadBuf);  
       RETURN_FTEERROR(FALSE); 
    }
       
    if (pipe.index)
    {
       firstsector = ConvertToDataSector(handle, pipe.firstcluster);
       if (!firstsector)
       {        
          FreeReadBuf(ReadBuf);       
          RETURN_FTEERROR(FALSE); 
       }
       
       if (ReadSectors(handle, pipe.index*sectorspercluster, 
                       firstsector, pipe.buf) == -1)
       {
           FreeReadBuf(ReadBuf);  
           RETURN_FTEERROR(FALSE); 
       }    
    }
    
    FreeReadBuf(ReadBuf);  
    return TRUE;
}          
Ejemplo n.º 12
0
static BOOL MarkClusterChain(RDWRHandle handle, CLUSTER dest, unsigned long filesize)
{
    unsigned long i;
    
    for (i=0; i<filesize; i++)
    {
	if (!WriteFatLabel(handle, dest+i, dest+i+1))
	    RETURN_FTEERROR(FALSE);
    }
    
    if (!WriteFatLabel(handle, dest+filesize, FAT_LAST_LABEL))
	RETURN_FTEERROR(FALSE);
    
    return TRUE;    
}
Ejemplo n.º 13
0
static BOOL CleanFatLabelChain(RDWRHandle handle, CLUSTER source)
{
    CLUSTER label;
    
    do {
	
	if (!GetNthCluster(handle, source, &label))
	    RETURN_FTEERROR(FALSE);	
	
	if (!WriteFatLabel(handle, source, FAT_FREE_LABEL))
	    RETURN_FTEERROR(FALSE);	
	
	source = label;
	
    } while (FAT_NORMAL(label));
    
    return TRUE;
}
Ejemplo n.º 14
0
static BOOL GetNextConsecutiveChain(RDWRHandle handle, CLUSTER source, unsigned long maxlength, unsigned long* length)
{
    CLUSTER current = source, label;
    unsigned long retLength;
    
    if (!GetNthCluster(handle, source, &label))
	RETURN_FTEERROR(FALSE);
    
    for (retLength=1; 
	 FAT_NORMAL(label) && (label == ++current) && (retLength < maxlength);
         retLength++)    
    {
	if (!GetNthCluster(handle, current, &label))
	    RETURN_FTEERROR(FALSE);
    }
    
    *length = retLength;
    return TRUE;    
}
Ejemplo n.º 15
0
static BOOL OnlyFilledRead(RDWRHandle handle, SECTOR lsect, 
                           unsigned numsectors, char* buf)
{
    unsigned i;
    SECTOR  start = lsect;
    CLUSTER cluster, label;
    
    for (i = 0; i < numsectors; i++)
    {
        cluster = DataSectorToCluster(handle, lsect+i);
        if (!cluster) RETURN_FTEERROR(FALSE); 
                
        if (!GetNthCluster(handle, cluster, &label))
        {
           RETURN_FTEERROR(FALSE); 
        }
        
        if (FAT_FREE(label) || FAT_BAD(label))
        {
           if (i)
           {        
              if (ReadSectors(handle, i, start, buf) == -1)
              {
                 RETURN_FTEERROR(FALSE); 
              }
           }
                  
           start = lsect+i+1;
        }
    }

    if (start < lsect+numsectors)
    {
       if (ReadSectors(handle, numsectors - (unsigned)(start - lsect), 
                       start, buf) == -1)
       {
          RETURN_FTEERROR(FALSE); 
       }       
    }
    
    return TRUE;
}                           
Ejemplo n.º 16
0
static BOOL CopyClusterChain(RDWRHandle handle, CLUSTER source, CLUSTER dest)
{
    unsigned long length;
    
    do {
        if (!GetNextConsecutiveChain(handle, source, &length))
	   RETURN_FTEERROR(FALSE);
    
	if (!CopyConsecutiveClusters(handle, source, dest, length))
	    RETURN_FTEERROR(FALSE);
	
	dest += length;
	
	if (!GetNthCluster(handle, source+length-1, &source))
	    RETURN_FTEERROR(FALSE);
	
    } while (FAT_NORMAL(source));
    
    
    return TRUE;
}
Ejemplo n.º 17
0
static BOOL CleanFatLabels(RDWRHandle handle, CLUSTER dest, unsigned long length)
{
    unsigned long i;
    
    for (i=0; i<length; i++)
    {
	if (!WriteFatLabel(handle, dest + i, FAT_FREE_LABEL))
	    RETURN_FTEERROR(FALSE);
    }
	
    return TRUE;       
}
Ejemplo n.º 18
0
/* Returns wether there was enough conventionel memory to contain the logical
   blocks for XMS. */
unsigned InitialiseLogicalCache(void)
{
    int i, j;
#ifdef USE_EMS
    unsigned int EMSBase;
#endif

#ifdef NDEBUG    
    unsigned NumberOfPhysicalBlocks;
#endif
    
    /* Allocate the memory to contain the pages for XMS (or just a
       conventional cache). Initialise them. */
    for (i = 0; i < 4; i++)
    {
        LogicalBlocks[i].address = (char*) FTEAlloc(CACHEBLOCKSIZE); 
        
        if (!LogicalBlocks[i].address)
        {
           for (j = 0; j < i; j++)
               FTEFree(LogicalBlocks[j].address);
               
           RETURN_FTEERROR(0); 
        }
               
        LogicalBlocks[i].age     = 0;
        LogicalBlocks[i].PhysicalBlock = i;
    }
    
    /* Now initialise the physical extended memory cache */
    NumberOfPhysicalBlocks = InitialisePhysicalCache();
    if (NumberOfPhysicalBlocks < 4) NumberOfPhysicalBlocks = 4;

    /* Initialise the EMS part, according to wether EMS is available */
#ifdef USE_EMS    
    if (IsEMSCached())
    {
       EMSBase = EMSbaseaddress();
       assert(EMSBase);
       
       for (; i < 8; i++)
       {
           LogicalBlocks[i].address = (char far *)MK_FP(EMSBase, i * CACHEBLOCKSIZE);
           LogicalBlocks[i].age     = 0;
           LogicalBlocks[i].PhysicalBlock = i;             
       }
    }
#endif
    
    return NumberOfPhysicalBlocks;
}
Ejemplo n.º 19
0
static BOOL CopyClusters(RDWRHandle handle, CLUSTER source, CLUSTER dest, unsigned long length)
{
    SECTOR ssource, sdest;
    unsigned long sectorspercluster, i, blocks, totallength;
    unsigned rest;    
    
    ssource = ConvertToDataSector(handle, source);
    if (!ssource) RETURN_FTEERROR(FALSE);
	
    sdest = ConvertToDataSector(handle, dest);
    if (!sdest) RETURN_FTEERROR(FALSE);
	
    sectorspercluster = GetSectorsPerCluster(handle);
    if (!sectorspercluster) RETURN_FTEERROR(FALSE);
    
    totallength = length * sectorspercluster;
    
    blocks = totallength / COPY_BLOCK_SIZE;
    rest   = (unsigned)(totallength % COPY_BLOCK_SIZE);    
    
    for (i=0; i<blocks; i++)
    {    
	if (!CopySectors(handle, ssource, sdest, COPY_BLOCK_SIZE))
	    RETURN_FTEERROR(FALSE);

        ssource += COPY_BLOCK_SIZE;
        sdest   += COPY_BLOCK_SIZE;
    }
    
    if (rest > 0)
    {
	if (!CopySectors(handle, ssource, sdest, rest))
	    RETURN_FTEERROR(FALSE);	
    }
    
    return TRUE;    
}
Ejemplo n.º 20
0
BOOL LoFileNameExists(RDWRHandle handle, CLUSTER firstcluster,
                      char* filename, char* extension)
{
   struct Pipe pipe, *ppipe = &pipe;

   pipe.filename  = filename;
   pipe.extension = extension;
   pipe.exists    = FALSE;

   if (!TraverseSubdir(handle, firstcluster, ExistanceChecker,
                       (void**) &ppipe, TRUE))
      RETURN_FTEERROR(FAIL);

   return pipe.exists;
}
Ejemplo n.º 21
0
CLUSTER GetNthFileCluster(RDWRHandle handle, CLUSTER firstclust, unsigned n,
                          BOOL* pasttheend)
{
   struct Pipe pipe, *ppipe = &pipe;
   
   pipe.n = n;
   pipe.count = 0;
   pipe.result = 1;
   pipe.found = FALSE;
   
   if (!FileTraverseFat(handle, firstclust, NthFileClusterFinder, 
                        (void**)&ppipe))
      RETURN_FTEERROR(FALSE);
   
   *pasttheend = ((!pipe.found) && (n >= pipe.count));
         
   return pipe.result;
}
Ejemplo n.º 22
0
static BOOL NthFileClusterFinder(RDWRHandle handle, CLUSTER label,
                                 SECTOR datasector, void** structure)
{
   struct Pipe* pipe = *((struct Pipe**) structure);
   
   label = label;
   
   if (pipe->n == pipe->count)
   {
      pipe->result = DataSectorToCluster(handle, datasector);
      if (!pipe->result) RETURN_FTEERROR(FAIL);
      
      pipe->found = TRUE;
      return FALSE;
   }

   pipe->count++;
   return TRUE;
}
Ejemplo n.º 23
0
static BOOL ExistanceChecker(RDWRHandle handle, struct DirectoryPosition* pos,
                             void** structure)
{
   struct Pipe* pipe = *((struct Pipe**) structure);
   struct DirectoryEntry entry;

   if (!GetDirectory(handle, pos, &entry))
      RETURN_FTEERROR(FAIL);

   if (IsLFNEntry(&entry))
      return TRUE;

   if ((strnicmp(pipe->extension, entry.extension, 3) == 0) &&
       (strnicmp(pipe->filename, entry.filename, 8) == 0))
   {
      pipe->exists = TRUE;
      return FALSE;
   }

   return TRUE;
}
Ejemplo n.º 24
0
static BOOL Relocate1Cluster(RDWRHandle handle,
                             CLUSTER source,
                             CLUSTER destination,
                             CLUSTER pSource)
{
   int labelsize;
   CLUSTER fatpos=0, freecluster, label;
   //BOOL IsInFAT = FALSE;
   SECTOR srcsector, destsector;
   unsigned long sectorspercluster;
   CLUSTER clustervalue;
   BOOL DOTSprocessed=FALSE;
   struct FSInfoStruct FSInfo;

   /* See wether the destination is actually free. */
   if (!GetNthCluster(handle, destination, &label))
      RETURN_FTEERROR(FALSE);
   if (!FAT_FREE(label)) 
      RETURN_FTEERROR(FALSE);
   
   /* Do some preliminary calculations. */
   srcsector = ConvertToDataSector(handle, source);
   if (!srcsector)
      RETURN_FTEERROR(FALSE);
   destsector = ConvertToDataSector(handle, destination);
   if (!destsector)
      RETURN_FTEERROR(FALSE);
   sectorspercluster = GetSectorsPerCluster(handle);
   if (!sectorspercluster)
      RETURN_FTEERROR(FALSE);
         
   /* Get the value that is stored at the source position in the FAT */
   if (!ReadFatLabel(handle, source, &clustervalue))
      RETURN_FTEERROR(FALSE);


   /* Copy all sectors in this cluster to the new position */
   if (!CopySectors(handle, srcsector, destsector, sectorspercluster))
   {
      RETURN_FTEERROR(FALSE);
   }
   
   if (!WriteFatLabel(handle, destination, clustervalue))
   {
      RETURN_FTEERROR(FALSE);
   }

   /* Adjust the pointer to the relocated cluster */
   if (!WriteFatLabel(handle, pSource, destination))
   {
      RETURN_FTEERROR(FALSE);
   }

   if (!WriteFatLabel(handle, source, FAT_FREE_LABEL))
   {
      if (DOTSprocessed)
         AdaptCurrentAndPreviousDirs(handle, source, source);
   
      RETURN_FTEERROR(FALSE);
   }

   /* Adjust FSInfo on FAT32 */
   labelsize = GetFatLabelSize(handle);
   if (labelsize == FAT32)
   {
      if (!GetFreeClusterSearchStart(handle, &freecluster))
         RETURN_FTEERROR(FALSE);
         
      if (source < freecluster) /* source cluster became available */
      {
         if (!ReadFSInfo(handle, &FSInfo))
            RETURN_FTEERROR(FALSE);
    
         WriteFreeClusterStart(&FSInfo, source);
    
         if (!WriteFSInfo(handle, &FSInfo))
            RETURN_FTEERROR(FALSE);          
      }
      
      if ((freecluster == destination) && /* We are relocating to the first */
	  (destination < source))         /* free cluster */
      {
         CLUSTER dummy;     
 
         if (!FindFirstFreeSpace(handle, &dummy, &dummy))
            RETURN_FTEERROR(FALSE);
      }
   }
   
   return TRUE;
}
Ejemplo n.º 25
0
static BOOL RelocateFirstCluster(RDWRHandle handle, 
                                 struct DirectoryPosition* pos,
                                 struct DirectoryEntry* entry,
                                 CLUSTER destination)
{
   int labelsize;
   CLUSTER fatpos=0, freecluster, label;
   BOOL IsInFAT = FALSE;
   SECTOR srcsector, destsector;
   unsigned long sectorspercluster;
   CLUSTER clustervalue;
   BOOL DOTSprocessed=FALSE;
   struct FSInfoStruct FSInfo;
   CLUSTER source;

   /* Do some preliminary calculations. */
   source = GetFirstCluster(entry);

   srcsector = ConvertToDataSector(handle, source);
   if (!srcsector)
      RETURN_FTEERROR(FALSE);
   destsector = ConvertToDataSector(handle, destination);
   if (!destsector)
      RETURN_FTEERROR(FALSE);
   sectorspercluster = GetSectorsPerCluster(handle);
   if (!sectorspercluster)
      RETURN_FTEERROR(FALSE);

   /* Get the value that is stored at the source position in the FAT */
   if (!ReadFatLabel(handle, source, &clustervalue))
      RETURN_FTEERROR(FALSE);
   
   /* We know the first cluster is refered to in the directory entry given */
    /*
    This is the first cluster of some file. See if it is a directory
    and if it is, adjust the '.' entry of this directory and all
    of the '..' entries of all the (direct) subdirectories to point
    to the new cluster.
    */
    if (entry->attribute & FA_DIREC)
    {
        if (!AdaptCurrentAndPreviousDirs(handle, source, destination))
        {
            RETURN_FTEERROR(FALSE);
        }
        DOTSprocessed = TRUE;
    }

   /* Copy all sectors in this cluster to the new position */
   if (!CopySectors(handle, srcsector, destsector, sectorspercluster))
   {
      RETURN_FTEERROR(FALSE);
   }
   
   /* Write the entry in the FAT */
   if (!WriteFatLabel(handle, destination, clustervalue))
   {
      RETURN_FTEERROR(FALSE);
   }

   SetFirstCluster(destination, entry);
   if (!WriteDirectory(handle, pos, entry))
   {
        RETURN_FTEERROR(FALSE);
   }

//   if (!GetNthCluster(handle, source, &label))
//        return FALSE;

   if (!WriteFatLabel(handle, source, FAT_FREE_LABEL))
   {
      RETURN_FTEERROR(FALSE);
   }

   /* Adjust FSInfo on FAT32 */
   labelsize = GetFatLabelSize(handle);
   if (labelsize == FAT32)
   {
      if (!GetFreeClusterSearchStart(handle, &freecluster))
         RETURN_FTEERROR(FALSE);
         
      if (source < freecluster) /* source cluster became available */
      {
         if (!ReadFSInfo(handle, &FSInfo))
            RETURN_FTEERROR(FALSE);
    
         WriteFreeClusterStart(&FSInfo, source);
    
         if (!WriteFSInfo(handle, &FSInfo))
            RETURN_FTEERROR(FALSE);          
      }
      
      if ((freecluster == destination) && /* We are relocating to the first */
	  (destination < source))         /* free cluster */
      {
         CLUSTER dummy;     
 
         if (!FindFirstFreeSpace(handle, &dummy, &dummy))
            RETURN_FTEERROR(FALSE);
      }
   }
   
   return TRUE;
}
Ejemplo n.º 26
0
BOOL GetNthCluster(RDWRHandle handle, CLUSTER n, CLUSTER* label)
{
   CLUSTER result;
   int fatlabelsize = GetFatLabelSize(handle);
   
   if (!ReadFatLabel(handle, n, &result))
      RETURN_FTEERROR(FALSE);
      
   switch (fatlabelsize)
   {
      case FAT12:
           if (FAT12_FREE(result))   
           {
              *label = FAT_FREE_LABEL;
              return TRUE;
           }
           if (FAT12_BAD(result))
           {
              *label = FAT_BAD_LABEL;
              return TRUE;
           }
           if (FAT12_LAST(result))
           {
              *label = FAT_LAST_LABEL;
              return TRUE;
           }
           break;
           
      case FAT16:
           if (FAT16_FREE(result))   
           {
              *label = FAT_FREE_LABEL;      
              return TRUE;
           }
           if (FAT16_BAD(result))
           {
              *label = FAT_BAD_LABEL;
              return TRUE;
           }
           if (FAT16_LAST(result))
           {
              *label = FAT_LAST_LABEL;             
              return TRUE;
           }
           break;
              
      case FAT32:
           if (FAT32_FREE(result))   
           {
              *label = FAT_FREE_LABEL;      
              return TRUE;
           }
           if (FAT32_BAD(result))
           {
              *label = FAT_BAD_LABEL;
              return TRUE;
           }
           if (FAT32_LAST(result))
           {
              *label = FAT_LAST_LABEL;             
              return TRUE;
           } 
           break; 
   }

   *label = result;
   return TRUE;
}
Ejemplo n.º 27
0
/* This function returns FALSE if the destination is not free. */
BOOL RelocateCluster(RDWRHandle handle, CLUSTER source, CLUSTER destination)
{
   int labelsize, value;
   CLUSTER fatpos=0, freecluster, dircluster, label;
   struct DirectoryPosition dirpos;
   struct DirectoryEntry entry;
   BOOL IsInFAT = FALSE;
   SECTOR srcsector, destsector;
   unsigned long sectorspercluster;
   CLUSTER clustervalue;
   BOOL found, DOTSprocessed=FALSE;
   struct FSInfoStruct FSInfo;

   if (!FatReferedMap)
   {
	if (!CreateFatReferedMap(handle))
	    return FALSE;
   }

   /* See wether the destination is actually free. */
   if (!GetNthCluster(handle, destination, &label))
      RETURN_FTEERROR(FALSE);
   if (!FAT_FREE(label)) 
      RETURN_FTEERROR(FALSE);
   
   /* Do some preliminary calculations. */
   srcsector = ConvertToDataSector(handle, source);
   if (!srcsector)
      RETURN_FTEERROR(FALSE);
   destsector = ConvertToDataSector(handle, destination);
   if (!destsector)
      RETURN_FTEERROR(FALSE);
   sectorspercluster = GetSectorsPerCluster(handle);
   if (!sectorspercluster)
      RETURN_FTEERROR(FALSE);
         
   /* Get the value that is stored at the source position in the FAT */
   if (!ReadFatLabel(handle, source, &clustervalue))
      RETURN_FTEERROR(FALSE);
   
   /* See where the cluster is refered */
   if (!GetVFSBitfieldBit(FatReferedMap, source, &value))
      return FALSE;

   if (value)
   {//CLUSTER fatpos1;

	if (!FindClusterInFAT(handle, source, &fatpos))
	    RETURN_FTEERROR(FALSE);
/*
        if (!FindClusterInFAT1(handle, source, &fatpos1))
            RETURN_FTEERROR(FALSE);

        if (fatpos != fatpos1)
            printf("hola");
*/
   }
      
   if (!fatpos)
   {
      if (!FindClusterInDirectories(handle, source, &dirpos, &found))
         RETURN_FTEERROR(FALSE);
      if (!found)
      {
          /* Note: on FAT32 this cluster may be pointing to the root directory.
                   We do not support relocating the root cluster at this time.          
          */
          
          
          RETURN_FTEERROR(FALSE);                /* Non valid cluster! */
      }
      else
      {
         /*
            This is the first cluster of some file. See if it is a directory
            and if it is, adjust the '.' entry of this directory and all
            of the '..' entries of all the (direct) subdirectories to point
            to the new cluster.
         */
         if (!GetDirectory(handle, &dirpos, &entry))
            RETURN_FTEERROR(FALSE);
            
         if (entry.attribute & FA_DIREC)
         {
            dircluster = GetFirstCluster(&entry);
            if (!AdaptCurrentAndPreviousDirs(handle, dircluster, destination))
            {
               RETURN_FTEERROR(FALSE);
            }
            DOTSprocessed = TRUE;
         }
      }
   }
   else
   {
      IsInFAT = TRUE;
   }

   /* Copy all sectors in this cluster to the new position */
   if (!CopySectors(handle, srcsector, destsector, sectorspercluster))
   {
      if (DOTSprocessed)
         AdaptCurrentAndPreviousDirs(handle, dircluster, source);
      RETURN_FTEERROR(FALSE);
   }
   
   /* Write the entry in the FAT */
   if (!WriteFatReference(handle, destination, clustervalue))
   {
      if (DOTSprocessed)
         AdaptCurrentAndPreviousDirs(handle, dircluster, source);
      RETURN_FTEERROR(FALSE);
   }

   if (!WriteFatLabel(handle, destination, clustervalue))
   {
      if (DOTSprocessed)
         AdaptCurrentAndPreviousDirs(handle, dircluster, source);
      RETURN_FTEERROR(FALSE);
   }

   /* Adjust the pointer to the relocated cluster */
   if (IsInFAT)
   {
      if (!WriteFatReference(handle, fatpos, destination))
      {
         RETURN_FTEERROR(FALSE);
      }

      if (!WriteFatLabel(handle, fatpos, destination))
      {
         RETURN_FTEERROR(FALSE);
      }

      if (!ClearVFSBitfieldBit(FatReferedMap, source))
	  return FALSE;
      if (!SetVFSBitfieldBit(FatReferedMap, destination))
	  return FALSE;
      //if (!IndicateFatClusterMoved(fatpos, source, destination))
      //   return FALSE;
   }
   else
   {
      CLUSTER label;

      if (!GetDirectory(handle, &dirpos, &entry))
      {
         if (DOTSprocessed)
            AdaptCurrentAndPreviousDirs(handle, dircluster, source);
      
         RETURN_FTEERROR(FALSE);
      }
      
      SetFirstCluster(destination, &entry);
      if (!WriteDirectory(handle, &dirpos, &entry))
      {
         if (DOTSprocessed)
            AdaptCurrentAndPreviousDirs(handle, dircluster, source);
      
         RETURN_FTEERROR(FALSE);
      }

      if (!IndicateDirEntryMoved(source, destination))
	  return FALSE;

     if (!GetNthCluster(handle, source, &label))
         return FALSE;

     //if (!IndicateFatClusterMoved(label, source, destination))
     //   return FALSE;
   }

   if (!WriteFatReference(handle, source, FAT_FREE_LABEL))
   {
      if (DOTSprocessed)
         AdaptCurrentAndPreviousDirs(handle, dircluster, source);
   
      RETURN_FTEERROR(FALSE);
   }

   if (!WriteFatLabel(handle, source, FAT_FREE_LABEL))
   {
      if (DOTSprocessed)
         AdaptCurrentAndPreviousDirs(handle, dircluster, source);
   
      RETURN_FTEERROR(FALSE);
   }

   if (!IndicateDirClusterMoved(handle, source, destination))
      RETURN_FTEERROR(FALSE);
      
   /* Adjust FSInfo on FAT32 */
   labelsize = GetFatLabelSize(handle);
   if (labelsize == FAT32)
   {
      if (!GetFreeClusterSearchStart(handle, &freecluster))
         RETURN_FTEERROR(FALSE);
         
      if (source < freecluster) /* source cluster became available */
      {
         if (!ReadFSInfo(handle, &FSInfo))
            RETURN_FTEERROR(FALSE);
    
         WriteFreeClusterStart(&FSInfo, source);
    
         if (!WriteFSInfo(handle, &FSInfo))
            RETURN_FTEERROR(FALSE);          
      }
      
      if ((freecluster == destination) && /* We are relocating to the first */
	  (destination < source))         /* free cluster */
      {
         CLUSTER dummy;     
 
         if (!FindFirstFreeSpace(handle, &dummy, &dummy))
            RETURN_FTEERROR(FALSE);
      }
   }
   
   return TRUE;
}