📄 simpleffs.c
字号:
sprintf(c256DebugBuff,"create_file: insert %s int cache entry = %d\r\n",kname,entry);
SysLog(LOG_DEBUG, c256DebugBuff, 0);
#endif
strcpy(cachename[entry],kname);
cacheuse[entry] = 1;
cachepacket[entry] = packet;
cachesector[entry] = workingsector;
// need to mark the rest of the file as used
if (length > PAYLOAD)
length -= PAYLOAD;
else
length = 0;
addr = sectortoaddress(workingsector);
addr1 = (long)(INFOLENGTH + ((long)(packet-1) * PACKETLENGTH));
addr = addr + addr1;
oldpacket = packet;
while (length) {
if (length > PAYLOAD) {
// link old packet in
packet = get_unused_packet_from(workingsector);
emb_program(addr+2,workingsector & 0xFF);
emb_program(addr+3,packet & 0xFF);
// set new as used
if (packet > NUMPACKETS) {
sprintf(c256DebugBuff, "ERROR: no unused packets in sector\r\n");
SysLog(LOG_EMERG, c256DebugBuff, 0);
system_error = ILLEGALPACKET;
return ERR_SFFS_ILLEBAL_PACKET;
}
addr = sectortoaddress(workingsector) + (packet * INFOENTRY);
emb_program(addr, USEDPACKET); // marked as used
emb_program(addr+1, 0xFF); // marked as used
length -= PAYLOAD;
// set up for next run
addr = sectortoaddress(workingsector);
addr1 = (long)(INFOLENGTH + ((long)(packet-1) * PACKETLENGTH));
addr = addr + addr1;
oldpacket = packet;
} else {
// link old packet in
packet = get_unused_packet_from(workingsector);
emb_program(addr+2,workingsector & 0xFF);
emb_program(addr+3,packet & 0xFF);
packet = get_unused_packet_from(workingsector);
if (packet > NUMPACKETS)
{
sprintf(c256DebugBuff, "ERROR: no unused packets in sector\r\n");
SysLog(LOG_EMERG, c256DebugBuff, 0);
system_error = ILLEGALPACKET;
return ERR_SFFS_ILLEBAL_PACKET;
}
addr = sectortoaddress(workingsector) + (packet * INFOENTRY);
emb_program(addr, USEDPACKET); // marked as used
emb_program(addr+1, 0xFF); // marked as used
length = 0;
}
}
break;
case 'a':
// this deletes an existing file
packet = find_file(kname);
if (packet != MAXSECTORS && delbfrwrite) {
//delete_file(kname,packet);
del_file(kname);
}
#if DEBUG_CONSOL
sprintf(c256DebugBuff, "Create append file length = %d\r\n",length);
SysLog(LOG_DEBUG, c256DebugBuff, 0);
#endif
wrapped = (workingsector-1) & (MAXSECTORS-1); //tips!!!
addr = (((long)number_of_packets(RETFREE,workingsector) & 0xFF) * ((long)PAYLOAD & 0xFF)) & 0xFFFF;
while (addr < length &&
(workingsector != wrapped)) {
//Search for the sector whose free space is enough
workingsector++;
workingsector %= MAXSECTORS;
addr = (((long)number_of_packets(RETFREE,workingsector) & 0xFF) * ((long)PAYLOAD & 0xFF)) & 0xFFFF;
}
if ((workingsector == wrapped) && (addr < length)) {
#if DEBUG_CONSOL
sprintf(c256DebugBuff, "consolidate: befor consolidate workingsector=%d,\r\n",workingsector);
SysLog(LOG_DEBUG, c256DebugBuff, 0);
#endif
workingsector=my_consolidate();
if(workingsector == -1)
return -1;
//my_consolidate(); //I won't update the working setor.
#if DEBUG_CONSOL
sprintf(c256DebugBuff, "consolidate: after consolidate workingsector=%d,\r\n",workingsector);
SysLog(LOG_DEBUG, c256DebugBuff, 0);
#endif
wrapped = (workingsector-1) & (MAXSECTORS-1);
addr = (((long)number_of_packets(RETFREE,workingsector) & 0xFF) * ((long)PAYLOAD & 0xFF)) & 0xFFFF;
while (addr < length &&
(workingsector != wrapped)) {
workingsector++;
workingsector %= MAXSECTORS;
addr = (((long)number_of_packets(RETFREE,workingsector) & 0xFF) * ((long)PAYLOAD & 0xFF)) & 0xFFFF;
}
} else {
// get number of erased sectors
sector = 0;
for (i=0; i<MAXSECTORS; i++) {
if (sectorerased[i]) {
sector++;
}
}
// if need to consolidate sectors do it.
if (sector < 3) {
doconsolidate = 1; //??? I think uncomment the following part is better.
/*
workingsector = consolidate();
wrapped = (workingsector-1) & (MAXSECTORS-1);
addr = (((long)number_of_packets(RETFREE,workingsector) & 0xFF) * ((long)PAYLOAD & 0xFF)) & 0xFFFF;
while (addr < length &&
(workingsector != wrapped)) {
workingsector++;
workingsector %= MAXSECTORS;
addr = (((long)number_of_packets(RETFREE,workingsector) & 0xFF) * ((long)PAYLOAD & 0xFF)) & 0xFFFF;
}
*/
}
}
sectorerased[workingsector] = 0;
#if DEBUG_CREATE
sprintf(c256DebugBuff, "create_file: workingsector = %d\r\n",workingsector);
SysLog(LOG_DEBUG, c256DebugBuff, 0);
#endif
// find first unused packet and wr
packet = get_unused_packet_from(workingsector);
#if DEBUG_CREATE
sprintf(c256DebugBuff, "create_file: packet = %d\r\n",packet);
SysLog(LOG_DEBUG, c256DebugBuff, 0);
#endif
if (packet > NUMPACKETS)
{
sprintf(c256DebugBuff, "Illegal packet\r\n");
SysLog(LOG_EMERG, c256DebugBuff, 0);
system_error = ERR_SFFS_ILLEBAL_PACKET;
return ERR_SFFS_ILLEBAL_PACKET;
}
addr = sectortoaddress(workingsector) + (packet * INFOENTRY);
// write file name
for (i=0; i<INFOENTRY; i++)
emb_program(addr+i, kname[i]);
// put name into cache
// go find an empty entry in the cache
cacheentry = 0;
done = 0;
while (cacheentry < MAXCACHE && !done) {
if (!cacheuse[cacheentry]) {
done = 1;
} else {
cacheentry++;
}
}
// if didn't find an empty entry in the cache take the least used
if (cacheentry == MAXCACHE) {
cacheentry = 0;
smallest = 255;
entry = 0;
while (cacheentry < MAXCACHE) {
entry = cacheuse[cacheentry] < smallest ? cacheentry : entry;
smallest = cacheuse[cacheentry] < smallest ? cacheuse[cacheentry] : smallest;
cacheentry++;
}
} else {
entry = cacheentry;
}
#if DEBUG_CREATE
sprintf(c256DebugBuff, "create_file: insert %s int cache entry = %d\r\n",kname,entry);
SysLog(LOG_DEBUG, c256DebugBuff, 0);
#endif
strcpy(cachename[entry],kname);
cacheuse[entry] = 1;
cachepacket[entry] = packet;
cachesector[entry] = workingsector;
// need to mark the rest of the file as used
if (length > PAYLOAD)
length -= PAYLOAD;
else
length = 0;
addr = sectortoaddress(workingsector);
addr1 = (long)(INFOLENGTH + ((long)(packet-1) * PACKETLENGTH));
addr = addr + addr1;
oldpacket = packet;
while (length) {
if (length > PAYLOAD) {
// link old packet in
packet = get_unused_packet_from(workingsector);
emb_program(addr+2,workingsector & 0xFF);
emb_program(addr+3,packet & 0xFF);
// set new as used
if (packet > NUMPACKETS) {
sprintf(c256DebugBuff, "ERROR: no unused packets in sector\r\n");
SysLog(LOG_EMERG, c256DebugBuff, 0);
system_error = ILLEGALPACKET;
return ERR_SFFS_ILLEBAL_PACKET;
}
addr = sectortoaddress(workingsector) + (packet * INFOENTRY);
emb_program(addr, USEDPACKET); // marked as used
emb_program(addr+1, 0xFF); // marked as used
length -= PAYLOAD;
// set up for next run
addr = sectortoaddress(workingsector);
addr1 = (long)(INFOLENGTH + ((long)(packet-1) * PACKETLENGTH));
addr = addr + addr1;
oldpacket = packet;
} else {
// link old packet in
packet = get_unused_packet_from(workingsector);
emb_program(addr+2,workingsector & 0xFF);
emb_program(addr+3,packet & 0xFF);
packet = get_unused_packet_from(workingsector);
if (packet > NUMPACKETS)
{
sprintf(c256DebugBuff, "ERROR: no unused packets in sector\r\n");
SysLog(LOG_EMERG, c256DebugBuff, 0);
system_error = ILLEGALPACKET;
return ERR_SFFS_ILLEBAL_PACKET;
}
addr = sectortoaddress(workingsector) + (packet * INFOENTRY);
emb_program(addr, USEDPACKET); // marked as used
emb_program(addr+1, 0xFF); // marked as used
length = 0;
}
}
break;
default:
return ERR_PARAM;
}
#if DEBUG_CREATE
printf("Create %s success!\r\n",filename);
#endif
return OK;
}
int createfile4java(char *filename, int offset, char attribute,unsigned int length)
{
return create_file(filename+offset, attribute, length);
}
/*
* filename: valid file name
* string: C string, must have \0
* This API is not good, it should be changed to
* char* filename, char* pBuff, int bufflength
* It could only append the space which had already reserved for the file
* return how many bytes is written.
* -1 means failed
*/
int append_to_file(char *filename, char *string)
{
int j;
int sector;
int packet;
long addr;
long addr1;
int dataptr;
unsigned char data;
unsigned char olddata;
int packetlen;
int done;
int link;
int eof;
if(!fdiskReady){
SysLog(LOG_DEBUG, "flash disk not ready!", 0);
return -1;
}
sector = find_file(filename);
if (sector == MAXSECTORS)
{
return -1;
}
packet = find_packet(filename,sector);
addr = sectortoaddress(sector);
addr1 = (long)(INFOLENGTH + ((long)(packet-1) * PACKETLENGTH));
addr = addr + addr1;
packetlen = PAYLOAD;
dataptr = 6;
done = 0;
data = 0;
eof = 0;
//locate the end of the file
while (!done && !eof) {
olddata = data;
data = read_flash(addr+(long)dataptr);
if (data == 0xFF) {
done = 1;
} else {
packetlen--;
dataptr++;
}
if (!packetlen) {
packetlen = PAYLOAD;
dataptr = 6;
link = read_flash(addr+2) * PACKETLENGTH + read_flash(addr+3);
if (link == 0xFFFF) {
eof = 1;
} else {
packet = link & 0xFF;
addr = sectortoaddress(sector);
addr1 = (long)(INFOLENGTH + ((long)(packet-1) * PACKETLENGTH));
addr = addr + addr1;
}
}
}
//I am at the end of the file
j = 0;
if ((dataptr & 1) && (string[j]) && !eof) {
emb_program(addr+(long)dataptr-1,olddata);
emb_program(addr+(long)dataptr++, string[j]);
packetlen--;
j++;
}
//we are at the even address now
while (string[j] && !eof)
{
if (packetlen) {
emb_program(addr+(long)dataptr++, string[j]);
if (string[j+1] == 0) {
if (!((addr+((long)dataptr-1)) & 1)) {
emb_program(addr+(long)dataptr,0xFF);
}
}
j++;
packetlen--;
} else {
packetlen = PAYLOAD;
dataptr = 6;
link = read_flash(addr+2) * PACKETLENGTH + read_flash(addr+3);
if (link == 0xFFFF) {
eof = 1;
} else {
packet = link & 0xFF;
addr = sectortoaddress(sector);
addr1 = (long)(INFOLENGTH + ((long)(packet-1) * PACKETLENGTH));
addr = addr + addr1;
}
}
}
if (string[j]) {
system_error = 1;
}
return j;
}
int appendfile4java(char* filename, int nameoffset, char* string, int stringoffset, int datalength)
{
return append_to_file(filename+nameoffset, string+stringoffset);
}
/*
* return -1: not exists
* 1: exists
*/
int file_exists(char *filename)
{
if (find_file(filename) == MAXSECTORS)
{
return -1;
}
return 1;
}
int file_exists4java(char *filename, int offset)
{
return file_exists(filename+offset);
}
/*
* Have not been test yet
*/
int read_app_file(char *filename, char *data, int start, int length)
{
long addr;
long addr1;
int link;
int done;
int tsector;
int dataptr;
int packetlen;
int packetptr;
int packet;
if(!fdiskReady){
SysLog(LOG_DEBUG, "flash disk not ready!", 0);
return -1;
}
tsector = find_file(filename);
if (tsector == MAXSECTORS) {
return -1;
}
packet = find_packet(filename,tsector);
if (packet > NUMPACKETS) {
return -1;
}
addr = sectortoaddress(tsector);
addr1 = (long)(INFOLENGTH + ((long)(packet-1) * PACKETLENGTH));
addr = addr + addr1;
done = 0;
dataptr = 0;
packetptr = 6;
packetlen = PAYLOAD;
while (dataptr < start && !done) {
link = read_flash(addr+packetptr);
if (link == 0xFF) {
done = 1;
} else {
packetlen--;
dataptr++;
packetptr++;
}
if (!packetlen) {
packetlen = PAYLOAD;
packetptr = 6;
link = read_flash(addr+2) * PACKETLENGTH + read_flash(addr+3);
if (link == 0xFFFF) {
done = 1;
} else {
packet = link;
addr = sectortoaddress(tsector);
addr1 = (long)(INFOLENGTH + ((long)(packet-1) * PACKETLENGTH));
addr = addr + addr1;
}
}
}
dataptr = 0;
packetptr = 6;
while (dataptr < length && !done) {
link = read_flash(addr+(long)packetptr);
if ((link & 0xFF) == 0xFF) {
done = 1;
} else {
data[dataptr++] = link;
packetptr++;
packetlen--;
}
if (!packetlen) {
packetlen = PAYLOAD;
packetptr = 6;
link = read_flash(addr+2) * PACKETLENGTH + read_flash(addr+3);
if (link == 0xFFFF) {
done = 1;
} else {
packet = link;
addr = sectortoaddress(tsector);
addr1 = (long)(INFOLENGTH + ((long)(packet-1) * PACKETLENGTH));
addr = addr + addr1;
}
}
}
return dataptr;
}
/*
* get currnet free space
*/
int get_avaible_space(void)
{
int i;
int space;
// if(!fdiskReady){
// SysLog(LOG_DEBUG, "flash disk not ready!", 0);
// return -1;
// }
space=0;
for(i=0;i<MAXSECTORS;i++)
{
space+=number_of_packets(RETFREE, i)*250;
//space+=number_of_packets(RETDELETED, i)*250;
}
return space;
}
int del_file4java(char *filename
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -