bool SDCardFile::open(bool create) { if (!exists) { if (create) { SDCardEntry parentDir(dir); if (!parentDir.exists) return false; fat_dir_struct *dd = fat_open_dir(SDCard.fs, &parentDir.dir_entry); if (!fat_create_file(dd, name, &dir_entry) && strcmp(name, dir_entry.long_name)) { fat_close_dir(dd); return false; } fat_close_dir(dd); } else { return false; } } fd = fat_open_file(SDCard.fs, &dir_entry); if (fd != NULL) return true; else return false; }
void httpd_cleanup(void) { #ifdef VFS_SUPPORT if (STATE->handler == httpd_handle_vfs && STATE->u.vfs.fd) { printf("httpd: cleaning left-over vfs-handle at %p.\n", STATE->u.vfs.fd); vfs_close(STATE->u.vfs.fd); STATE->u.vfs.fd = NULL; } #endif /* VFS_SUPPORT */ #ifdef HTTP_SD_DIR_SUPPORT if (STATE->handler == httpd_handle_sd_dir && STATE->u.dir.handle) { fat_close_dir(STATE->u.dir.handle); STATE->u.dir.handle = NULL; } #endif /* HTTP_SD_DIR_SUPPORT */ #ifdef HTTPD_SOAP_SUPPORT if (STATE->handler == httpd_handle_soap) soap_deallocate_context(&STATE->u.soap); #endif /* HTTPD_SOAP_SUPPORT */ STATE->handler = NULL; }
/* recursively delete entries */ bool SDCardEntry::deleteFirstEntry() { if (!isDirectory()) return false; fat_dir_struct *dd = fat_open_dir(SDCard.fs, &dir_entry); if (dd == NULL) return false; SDCardEntry entry; uint8_t ret; again: ret = fat_read_dir(dd, &entry.dir_entry); if (!strcmp(entry.dir_entry.long_name, ".") || !strcmp(entry.dir_entry.long_name, "..")) { goto again; } fat_close_dir(dd); if (!ret) { return false; } entry.setFromParentEntry(this); return entry.deleteEntry(true); }
bool SDCardEntry::findFile(const char *name, struct fat_dir_entry_struct *entry) { if (!isDirectory()) return false; fat_dir_struct *dd = fat_open_dir(SDCard.fs, &dir_entry); if (dd == NULL) return false; while (fat_read_dir(dd, entry)) { if (strcmp(entry->long_name, name) == 0) { fat_close_dir(dd); return true; } } fat_close_dir(dd); return false; }
bool SDCardEntry::createSubDirectory(const char *path, struct fat_dir_entry_struct *new_entry) { if (path[0] == '/') path++; fat_dir_struct *dd = fat_open_dir(SDCard.fs, &dir_entry); if (dd == NULL) return false; char subDir[64]; while (1) { const char *pos = strchr(path, '/'); if (pos == NULL) { m_strncpy(subDir, path, sizeof(subDir) - 1); } else { int len = pos - path; memcpy(subDir, path, len); subDir[len] = '\0'; path = pos + 1; } struct fat_dir_entry_struct new_dir_entry; int result = fat_create_dir(dd, subDir, &new_dir_entry); if (result == 0 && strcmp(subDir, new_dir_entry.long_name)) { fat_close_dir(dd); return false; } else { memcpy(new_entry, &new_dir_entry, sizeof(new_dir_entry)); fat_close_dir(dd); dd = fat_open_dir(SDCard.fs, &new_dir_entry); if (dd == NULL) { return false; } else { if (pos == NULL) { sd_raw_sync(); fat_close_dir(dd); return true; } } } } }
void FatFsClass::finalize(void) { /* close directory */ fat_close_dir(_dd); /* close file system */ fat_close(_fs); /* close partition */ partition_close(_partition); }
bool FatFsClass::changeDirectory(const char * file_name) { struct fat_dir_entry_struct directory; if(fat_get_dir_entry_of_path(_fs, file_name, &directory)){ fat_close_dir(_dd); _dd = fat_open_dir(_fs, &directory); if(_dd){ return true; } } return false; }
void RepRapSDCard::reset() { if (dd != NULL) { fat_close_dir(dd); dd = NULL; } if (fs != NULL) { fat_close(fs); fs = NULL; } if (partition != NULL) { partition_close(partition); partition = NULL; } }
void reset() { if (playing) finishRead(); if (dd != 0) { fat_close_dir(dd); dd = 0; } if (fs != 0) { fat_close(fs); fs = 0; } if (partition != 0) { partition_close(partition); partition = 0; } }
int SDCardEntry::listDirectory(SDCardEntry entries[], int maxCount) { if (!isDirectory()) return -1; int entryCount = 0; fat_dir_struct *dd = fat_open_dir(SDCard.fs, &dir_entry); if (dd == NULL) return -1; while (fat_read_dir(dd, &entries[entryCount].dir_entry) && (entryCount < maxCount)) { if (!strcmp(entries[entryCount].dir_entry.long_name, ".")) continue; entries[entryCount].setFromParentEntry(this); entryCount++; } fat_close_dir(dd); return entryCount; }
bool SDCardEntry::isEmpty() { if (!isDirectory()) { return false; } fat_dir_struct *dd = fat_open_dir(SDCard.fs, &dir_entry); if (dd == NULL) return -1; uint8_t cnt = 0; struct fat_dir_entry_struct entry; uint8_t ret; while ((ret = fat_read_dir(dd, &entry))) { if (!strcmp(entry.long_name, ".") || !strcmp(entry.long_name, "..")) { continue; } else { cnt++; break; } } fat_close_dir(dd); return (cnt == 0); }
static void httpd_handle_input(void) { char *ptr = (char *) uip_appdata; #ifdef HTTPD_AUTH_SUPPORT char *start_ptr; if (STATE->header_reparse) { printf("reparse next part of the header\n"); goto start_auth; } #endif /* HTTPD_AUTH_SUPPORT */ if (uip_len < 10) { printf("httpd: received request to short (%d bytes).\n", uip_len); STATE->handler = httpd_handle_400; return; } #ifdef HTTPD_SOAP_SUPPORT if (strncasecmp_P(uip_appdata, PSTR("POST /soap"), 10) == 0) { soap_initialize_context(&STATE->u.soap); STATE->handler = httpd_handle_soap; return; } #endif /* HTTPD_SOAP_SUPPORT */ if (strncasecmp_P(uip_appdata, PSTR("GET /"), 5)) { printf("httpd: received request is not GET.\n"); STATE->handler = httpd_handle_400; return; } char *filename = (char *)uip_appdata + 5; /* beyond slash */ ptr = strchr(filename, ' '); if (ptr == NULL) { printf("httpd: space after filename not found.\n"); STATE->handler = httpd_handle_400; return; } *ptr = 0; /* Terminate filename. */ /* * Successfully parsed the GET request, * possibly check authentication. */ #ifdef HTTPD_AUTH_SUPPORT ptr++; /* Increment pointer to the end of * the GET */ start_auth: start_ptr = ptr; ptr = strstr_P(ptr, PSTR("Authorization: ")); if (ptr == NULL) { if (strstr_P(start_ptr, PSTR("\r\n\r\n"))) { printf("Authorization-header not found.\n"); printf("%s\n", start_ptr); goto auth_failed; } else { ptr = start_ptr; /* Skip all Lines before the last one */ while (1) { ptr = strstr_P(start_ptr, PSTR("\r\n")); if (ptr) start_ptr = ptr + 2; else break; } if (!strncmp(start_ptr, PSTR("Authorization: "), strlen(start_ptr))) { printf("Authorization header is split over two packages, damn"); printf("%s\n", start_ptr); goto auth_failed; } else { STATE->header_reparse = 1; goto after_auth; } } } ptr += 15; /* Skip `Authorization: ' header. */ if (strncmp_P(ptr, PSTR("Basic "), 6)) { printf("auth: method is not basic.\n"); goto auth_failed; } ptr += 6; /* Skip `Basic ' string. */ char *nl = strchr(ptr, '\n'); if (nl == NULL) { printf("auth: newline not found.\n"); goto auth_failed; } *nl = 0; /* Zero-terminate BASE64-string. */ base64_str_decode(ptr); printf("auth: decoded auth string: '%s'.\n", ptr); char *password; if ((password = strchr(ptr, ':')) == NULL) { printf("auth: didn't find colon!\n"); auth_failed: httpd_cleanup(); STATE->handler = httpd_handle_401; STATE->header_reparse = 0; return; } *password = 0; password++; /* Do the PAM authentification */ pam_auth(ptr, password, &STATE->auth_state); if (STATE->header_reparse) { STATE->header_reparse = 0; return; /* We musn't open the file once again */ } after_auth: /* Dummy statement, to keep GCC happy, if HTTP_SD_DIR_SUPPORT is enabled * and thus the uint8_t definition the next statement in code. */ (void) 0; #endif /* HTTPD_AUTH_SUPPORT */ /* * Authentication is okay, now fulfill request for file * refered to in filename. */ #ifndef HTTP_SD_DIR_SUPPORT if (*filename == 0) /* No filename, override -> index */ strcpy_P(filename, PSTR(HTTPD_INDEX)); #endif #ifdef HTTP_FAVICON_SUPPORT if (strcmp_P(filename, PSTR("favicon.ico")) == 0) strcpy_P(filename, PSTR("If.ico")); #endif #ifdef ECMD_PARSER_SUPPORT uint8_t offset = strlen_P(PSTR(ECMD_INDEX "?")); if (strncmp_P(filename, PSTR(ECMD_INDEX "?"), offset) == 0) { httpd_handle_ecmd_setup(filename + offset); return; } #endif /* ECMD_PARSER_SUPPORT */ #ifdef VFS_SUPPORT /* Keep content-type identifing char. */ /* filename instead starts after last directory slash. */ char *slash = strrchr(filename, '/'); if (slash != NULL) { STATE->u.vfs.content_type = *(char *) (slash + 1); } else { STATE->u.vfs.content_type = *filename; } STATE->u.vfs.fd = vfs_open(filename); if (STATE->u.vfs.fd) { STATE->handler = httpd_handle_vfs; return; } /* Now try appending the index.html document name */ ptr = filename + strlen(filename); #ifdef HTTP_SD_DIR_SUPPORT uint8_t lastchar = ptr[-1]; #endif if (ptr[-1] != '/') *(ptr++) = '/'; strcpy_P(ptr, PSTR(HTTPD_INDEX)); STATE->u.vfs.fd = vfs_open(filename); if (STATE->u.vfs.fd) { STATE->handler = httpd_handle_vfs; return; } if (ptr == filename) /* Make sure not to strip initial slash. */ ptr[0] = 0; else ptr[-1] = 0; /* Strip index filename again, * including the last slash. */ #endif /* VFS_SUPPORT */ #ifdef HTTP_SD_DIR_SUPPORT if ((STATE->u.dir.handle = vfs_sd_chdir(filename - 1))) { strncpy(STATE->u.dir.dirname, filename - 1, SD_DIR_MAX_DIRNAME_LEN); STATE->u.dir.dirname[SD_DIR_MAX_DIRNAME_LEN - 1] = 0; if (lastchar != '/') { STATE->handler = httpd_handle_sd_dir_redirect; fat_close_dir(STATE->u.dir.handle); } else STATE->handler = httpd_handle_sd_dir; return; } #endif /* HTTP_SD_DIR_SUPPORT */ /* Fallback, send 404. */ STATE->handler = httpd_handle_404; }
int main() { /* we will just use ordinary idle mode */ set_sleep_mode(SLEEP_MODE_IDLE); /* setup uart */ uart_init(); while(1) { /* setup sd card slot */ if(!sd_raw_init()) { #if DEBUG uart_puts_p(PSTR("MMC/SD initialization failed\n")); #endif continue; } /* open first partition */ struct partition_struct* partition = partition_open(sd_raw_read, sd_raw_read_interval, #if SD_RAW_WRITE_SUPPORT sd_raw_write, sd_raw_write_interval, #else 0, 0, #endif 0 ); if(!partition) { /* If the partition did not open, assume the storage device * is a "superfloppy", i.e. has no MBR. */ partition = partition_open(sd_raw_read, sd_raw_read_interval, #if SD_RAW_WRITE_SUPPORT sd_raw_write, sd_raw_write_interval, #else 0, 0, #endif -1 ); if(!partition) { #if DEBUG uart_puts_p(PSTR("opening partition failed\n")); #endif continue; } } /* open file system */ struct fat_fs_struct* fs = fat_open(partition); if(!fs) { #if DEBUG uart_puts_p(PSTR("opening filesystem failed\n")); #endif continue; } /* open root directory */ struct fat_dir_entry_struct directory; fat_get_dir_entry_of_path(fs, "/", &directory); struct fat_dir_struct* dd = fat_open_dir(fs, &directory); if(!dd) { #if DEBUG uart_puts_p(PSTR("opening root directory failed\n")); #endif continue; } /* print some card information as a boot message */ print_disk_info(fs); /* provide a simple shell */ char buffer[24]; while(1) { /* print prompt */ uart_putc('>'); uart_putc(' '); /* read command */ char* command = buffer; if(read_line(command, sizeof(buffer)) < 1) continue; /* execute command */ if(strcmp_P(command, PSTR("init")) == 0) { break; } else if(strncmp_P(command, PSTR("cd "), 3) == 0) { command += 3; if(command[0] == '\0') continue; /* change directory */ struct fat_dir_entry_struct subdir_entry; if(find_file_in_dir(fs, dd, command, &subdir_entry)) { struct fat_dir_struct* dd_new = fat_open_dir(fs, &subdir_entry); if(dd_new) { fat_close_dir(dd); dd = dd_new; continue; } } uart_puts_p(PSTR("directory not found: ")); uart_puts(command); uart_putc('\n'); } else if(strcmp_P(command, PSTR("ls")) == 0) { /* print directory listing */ struct fat_dir_entry_struct dir_entry; while(fat_read_dir(dd, &dir_entry)) { uint8_t spaces = sizeof(dir_entry.long_name) - strlen(dir_entry.long_name) + 4; uart_puts(dir_entry.long_name); uart_putc(dir_entry.attributes & FAT_ATTRIB_DIR ? '/' : ' '); while(spaces--) uart_putc(' '); uart_putdw_dec(dir_entry.file_size); uart_putc('\n'); } } else if(strncmp_P(command, PSTR("cat "), 4) == 0) { command += 4; if(command[0] == '\0') continue; /* search file in current directory and open it */ struct fat_file_struct* fd = open_file_in_dir(fs, dd, command); if(!fd) { uart_puts_p(PSTR("error opening ")); uart_puts(command); uart_putc('\n'); continue; } /* print file contents */ uint8_t buffer[8]; uint32_t offset = 0; while(fat_read_file(fd, buffer, sizeof(buffer)) > 0) { uart_putdw_hex(offset); uart_putc(':'); for(uint8_t i = 0; i < 8; ++i) { uart_putc(' '); uart_putc_hex(buffer[i]); } uart_putc('\n'); offset += 8; } fat_close_file(fd); } else if(strcmp_P(command, PSTR("disk")) == 0) { if(!print_disk_info(fs)) uart_puts_p(PSTR("error reading disk info\n")); } #if FAT_WRITE_SUPPORT else if(strncmp_P(command, PSTR("rm "), 3) == 0) { command += 3; if(command[0] == '\0') continue; struct fat_dir_entry_struct file_entry; if(find_file_in_dir(fs, dd, command, &file_entry)) { if(fat_delete_file(fs, &file_entry)) continue; } uart_puts_p(PSTR("error deleting file: ")); uart_puts(command); uart_putc('\n'); } else if(strncmp_P(command, PSTR("touch "), 6) == 0) { command += 6; if(command[0] == '\0') continue; struct fat_dir_entry_struct file_entry; if(!fat_create_file(dd, command, &file_entry)) { uart_puts_p(PSTR("error creating file: ")); uart_puts(command); uart_putc('\n'); } } else if(strncmp_P(command, PSTR("write "), 6) == 0) { command += 6; if(command[0] == '\0') continue; char* offset_value = command; while(*offset_value != ' ' && *offset_value != '\0') ++offset_value; if(*offset_value == ' ') *offset_value++ = '\0'; else continue; /* search file in current directory and open it */ struct fat_file_struct* fd = open_file_in_dir(fs, dd, command); if(!fd) { uart_puts_p(PSTR("error opening ")); uart_puts(command); uart_putc('\n'); continue; } int32_t offset = strtolong(offset_value); if(!fat_seek_file(fd, &offset, FAT_SEEK_SET)) { uart_puts_p(PSTR("error seeking on ")); uart_puts(command); uart_putc('\n'); fat_close_file(fd); continue; } /* read text from the shell and write it to the file */ uint8_t data_len; while(1) { /* give a different prompt */ uart_putc('<'); uart_putc(' '); /* read one line of text */ data_len = read_line(buffer, sizeof(buffer)); if(!data_len) break; /* write text to file */ if(fat_write_file(fd, (uint8_t*) buffer, data_len) != data_len) { uart_puts_p(PSTR("error writing to file\n")); break; } } fat_close_file(fd); } else if(strncmp_P(command, PSTR("mkdir "), 6) == 0) { command += 6; if(command[0] == '\0') continue; struct fat_dir_entry_struct dir_entry; if(!fat_create_dir(dd, command, &dir_entry)) { uart_puts_p(PSTR("error creating directory: ")); uart_puts(command); uart_putc('\n'); } } #endif #if SD_RAW_WRITE_BUFFERING else if(strcmp_P(command, PSTR("sync")) == 0) { if(!sd_raw_sync()) uart_puts_p(PSTR("error syncing disk\n")); } #endif else if(strcmp_P(command, PSTR("sync")) == 0) { // vytvor adresar mereni_teploty // nekonecna smycka - pomoci RTCka kazdou minutu odmer teplotu } else { uart_puts_p(PSTR("unknown command: ")); uart_puts(command); uart_putc('\n'); } } /* close directory */ fat_close_dir(dd); /* close file system */ fat_close(fs); /* close partition */ partition_close(partition); } return 0; }
int syscall_file_closedir(unsigned long *vdirdesc) { return fat_close_dir(vdirdesc); }