/** * Usage: -f file -i - create a filesystem with name 'file' * Usage: -f file -o - open a filesystem 'file' * Usage: -f file -c path - create a file with path 'path' in a filesystem 'file' */ int main(int argc, char ** argv) { char *value = 0; int option; while ((option = getopt (argc, argv, "f:ioc:")) != -1) { switch(option) { case 'f': value = optarg; break; case 'i': if(value != 0) { init_file_system(value); } break; case 'o': open_file_system(value); break; case 'c': creat_file(value, optarg); break; } } int fdfs = simplefs_openfs("filesystem"); simplefs_creat("/a.txt", fdfs); int fd = simplefs_open("/a.txt", READ_AND_WRITE, fdfs); printf("Result of simplefs_open %d\n", fd); printf("Wrint to file result %d", simplefs_write(fd, "adam to glupi programista", 15, fdfs)); char result[20]; simplefs_lseek(fd, SEEK_SET,0,fdfs); printf("Readig from file %d", simplefs_read(fd, result, 10, fdfs)); result[10] = '\0'; printf("================Read================\n %s\n", result); return 0; }
void creat_file(char *filesystem, char *path) { int fdfs = open_file_system(filesystem); if(fdfs < -1) { return; } if(simplefs_creat(path, fdfs) == FILE_ALREADY_EXISTS) { printf("Plik już istnieje!\n"); } }
int upload_file(char *name,char *dest_name) { struct dirent de; int retVal=0; do { struct stat st; if (stat(name,&st)) { fprintf(stderr,"ERROR: Could not stat file '%s'\n",name); perror("stat() failed"); } printf("File '%s' is %ld bytes long.\n",name,(long)st.st_size); if (!file_system_found) open_file_system(); if (!file_system_found) { fprintf(stderr,"ERROR: Could not open file system.\n"); retVal=-1; break; } if (fat_opendir("/")) { retVal=-1; break; } printf("Opened directory, dir_sector=%d (absolute sector = %d)\n",dir_sector,partition_start+dir_sector); while(!fat_readdir(&de)) { if (de.d_name[0]) printf("%13s %d\n",de.d_name,(int)de.d_off); // else dump_bytes(0,"empty dirent",&dir_sector_buffer[dir_sector_offset],32); if (!strcasecmp(de.d_name,dest_name)) { // Found file, so will replace it printf("%s already exists on the file system, beginning at cluster %d\n",name,(int)de.d_ino); break; } } if (dir_sector==-1) { // File does not (yet) exist, get ready to create it printf("%s does not yet exist on the file system -- searching for empty directory slot to create it in.\n",name); if (fat_opendir("/")) { retVal=-1; break; } struct dirent de; while(!fat_readdir(&de)) { if (!de.d_name[0]) { printf("Found empty slot at dir_sector=%d, dir_sector_offset=%d\n", dir_sector,dir_sector_offset); // Create directory entry, and write sector back to SD card unsigned char dir[32]; bzero(dir,32); // Write name for(int i=0;i<11;i++) dir[i]=0x20; for(int i=0;i<8;i++) if (dest_name[i]=='.') { // Write out extension for(int j=0;j<3;j++) if (dest_name[i+1+j]) dir[8+j]=dest_name[i+1+j]; break; } else if (!dest_name[i]) break; else dir[i]=dest_name[i]; // Set file attributes (only archive bit) dir[0xb]=0x20; // Store create time and date time_t t=time(0); struct tm *tm=localtime(&t); dir[0xe]=(tm->tm_sec>>1)&0x1F; // 2 second resolution dir[0xe]|=(tm->tm_min&0x7)<<5; dir[0xf]=(tm->tm_min&0x3)>>3; dir[0xf]|=(tm->tm_hour)<<2; dir[0x10]=tm->tm_mday&0x1f; dir[0x10]|=((tm->tm_mon+1)&0x7)<<5; dir[0x11]=((tm->tm_mon+1)&0x1)>>3; dir[0x11]|=(tm->tm_year-80)<<1; dump_bytes(0,"New directory entry",dir,32); // (Cluster and size we set after writing to the file) // Copy back into directory sector, and write it bcopy(dir,&dir_sector_buffer[dir_sector_offset],32); if (write_sector(partition_start+dir_sector,dir_sector_buffer)) { printf("Failed to write updated directory sector.\n"); retVal=-1; break; } break; } } }