int getDirSize( const char * path, int includeSubdirs, unsigned int * dirSize ) { char dirPath[MAX_FILENAME_LENGTH]; unsigned int size = 0; if( "" == path ){ return false; } memset( dirPath,0,MAX_FILENAME_LENGTH ); strcpy( dirPath,path ); if( dirPath[strlen(dirPath)-1] != '/' ) dirPath[strlen(dirPath)] = '/'; if( strlen(dirPath) > MAX_FILENAME_LENGTH ) return false; DIR_STATE_STRUCT *dir; dir = fat_opendir((const char*)dirPath); if (dir == NULL) return false; struct stat stat_buf; DIR_ENTRY *currentEntry; char* filename; while(fat_readdir_ex(dir, &stat_buf) != NULL) { filename = currentEntry->d_name; if (strcmp(filename, ".") == 0 || strcmp(filename, "..") == 0) continue; if (!(stat_buf.st_mode & S_IFDIR)) { size += (stat_buf.st_size+511)/512; _usedSecNums +=(stat_buf.st_size+511)/512; } else if (includeSubdirs) { // calculate the size recursively unsigned int subDirSize = 0; char dirPathBuffer[MAX_FILENAME_LENGTH]; memset( dirPathBuffer,0,MAX_FILENAME_LENGTH ); strcpy( dirPathBuffer,dirPath ); memset( dirPath,0,MAX_FILENAME_LENGTH ); sprintf( dirPath,"%s%s",dirPathBuffer,filename ); int succ = getDirSize( dirPath, includeSubdirs, &subDirSize ); if( succ ) { size += (subDirSize+511)/512; _usedSecNums +=(subDirSize+511)/512; } memset( dirPath,0,MAX_FILENAME_LENGTH ); strcpy( dirPath,dirPathBuffer ); } } fat_closedir(dir); *dirSize = size;
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; } } }