Пример #1
0
/**
 * This function will set a hook function, which will be invoked when a memory 
 * block is allocated from heap memory.
 * 
 * @param hook the hook function
 */
int efs_rename(struct dfs_filesystem* fs, const char* oldpath, const char* newpath)
{
	FileRecord old_entry, new_entry;
	FileLocation old_loc, new_loc;
	efsl_fs* efsfs;
	eint8 fatfilename[11];
	
	efsfs = (efsl_fs*) fs->data ;
	RT_ASSERT(efsfs != RT_NULL);

	dir_getFatFileName((eint8 *)newpath, &fatfilename[0]);	
	
	/* parameters check */
	if (strlen(oldpath) > DFS_PATH_MAX || 
		strlen(newpath) > DFS_PATH_MAX ||
		/* old path is a directory that contains newpath */
		strncmp(oldpath, newpath, strlen(newpath)) == 0) 
		{
			dfs_log(DFS_DEBUG_ERROR, ("old path is a directory that contains newpath"));					
			return -DFS_STATUS_EINVAL;
		}	

	/* if can't find old direntry */
	if(fs_findFile(&efsfs->filesystem, (eint8 *)oldpath, &old_loc, 0) == 0)
	{
		dfs_log(DFS_DEBUG_ERROR, ("can't find old direntry"));		
		return -DFS_STATUS_ENOENT;
	}
	dir_getFileStructure(&efsfs->filesystem, &old_entry, &old_loc);

	/* if the new direntry exist */
	if(fs_findFile(&efsfs->filesystem, (eint8 *)newpath, &new_loc, 0) > 0)
	{
		dfs_log(DFS_DEBUG_ERROR, ("new direntry existed"));
		return -DFS_STATUS_ENOENT;
	}
	if(fs_findFreeFile(&efsfs->filesystem, (eint8 *)newpath, &new_loc, 0))
	{
		memCpy(&old_entry, &new_entry, sizeof(FileRecord));	
		memCpy(fatfilename, new_entry.FileName, 11);
		dir_createDirectoryEntry(&efsfs->filesystem, &new_entry, &new_loc);
	}

	/* delete the old direntry */
	old_entry.FileName[0] = 0xe5;
	dir_updateDirectoryEntry(&efsfs->filesystem, &old_entry, &old_loc);	

	return 0;
}
Пример #2
0
/* ***************************************************************************\
 * signed eint8 file_fopen(FileSystem *fs,File* file,eint8* filename)      
 * Description: This functions opens a file.                               
 * This function is about to be redesigned. No Docs.                       
 * Return value:
*/
esint8 file_fopen(File* file,FileSystem *fs,eint8* filename,eint8 mode)
{
    FileLocation loc;
    FileRecord wtmp;
    eint8 fatfilename[LIST_MAXLENFILENAME];
    euint32 sec;

    dir_getFatFileName(filename,fatfilename);
	
    switch(mode)
	{
        case MODE_READ:
            if(fs_findFile(fs,filename,&loc,0)==1)
			{
                dir_getFileStructure(fs,&(file->DirEntry), &loc);
                file_initFile(file,fs,&loc);
				file_setAttr(file,FILE_STATUS_OPEN,1);
				file_setAttr(file,FILE_STATUS_WRITE,0);
                return(0);
            }
            return(-1);
            break;
        case MODE_WRITE:
            if(fs_findFile(fs,filename,&loc,&sec)) /* File may NOT exist, but parent HAS to exist */
			{
                return(-2);
			}
			if(sec==0){ /* Parent dir does not exist */
 				return(-4);
			}
            if(fs_findFreeFile(fs,filename,&loc,0))
			{
                dir_createDefaultEntry(fs,&wtmp,fatfilename);
                dir_createDirectoryEntry(fs,&wtmp,&loc);
                memCpy(&wtmp,&(file->DirEntry),sizeof(wtmp));
				file_initFile(file,fs,&loc);
                sec=fs_getNextFreeCluster(file->fs,fs_giveFreeClusterHint(file->fs));
                dir_setFirstCluster(file->fs,&(file->Location),sec);
                fs_setFirstClusterInDirEntry(&(file->DirEntry),sec);
                fs_initClusterChain(fs,&(file->Cache),sec);
                fat_setNextClusterAddress(fs,sec,fat_giveEocMarker(fs));
				file_setAttr(file,FILE_STATUS_OPEN,1);
				file_setAttr(file,FILE_STATUS_WRITE,1);
            	return(0);
			}
            else
			{
                return(-3);
			}
            break;
        case MODE_APPEND:
			if(fs_findFile(fs,filename,&loc,0)==1) /* File exists */
			{
				dir_getFileStructure(fs,&(file->DirEntry), &loc);
				file_initFile(file,fs,&loc);
				if(file->Cache.FirstCluster==0){
					sec=fs_getNextFreeCluster(file->fs,fs_giveFreeClusterHint(file->fs));
					dir_setFirstCluster(file->fs,&(file->Location),sec);
					fs_setFirstClusterInDirEntry(&(file->DirEntry),sec);
					fat_setNextClusterAddress(fs,sec,fat_giveEocMarker(fs));
					file_initFile(file,fs,&loc);
				}
				file_setpos(file,file->FileSize);
				file_setAttr(file,FILE_STATUS_OPEN,1);
				file_setAttr(file,FILE_STATUS_WRITE,1);
			}
			else /* File does not excist */
			{
				if(fs_findFreeFile(fs,filename,&loc,0))
				{
					dir_createDefaultEntry(fs,&wtmp,fatfilename);
					dir_createDirectoryEntry(fs,&wtmp,&loc);
					memCpy(&wtmp,&(file->DirEntry),sizeof(wtmp));
					file_initFile(file,fs,&loc);
					sec=fs_getNextFreeCluster(file->fs,fs_giveFreeClusterHint(file->fs));
					dir_setFirstCluster(file->fs,&(file->Location),sec);
	                fs_setFirstClusterInDirEntry(&(file->DirEntry),sec);
    	            fs_initClusterChain(fs,&(file->Cache),sec);
					fat_setNextClusterAddress(fs,sec,fat_giveEocMarker(fs));
					file_setAttr(file,FILE_STATUS_OPEN,1);
					file_setAttr(file,FILE_STATUS_WRITE,1);
				}
				else
				{
					return(-3);
				}
			}
			return(0);
            break;
        default:
            return(-4);
            break;
    }
    return(-5);
}