// file.flush() //=================================== static int file_flush( lua_State* L ) { if(FILE_NOT_OPENED==file_fd) return luaL_error(L, "open a file first"); if(SPIFFS_fflush(&fs,file_fd) == 0) lua_pushboolean(L, 1); else lua_pushnil(L); return 1; }
//---------------------------------- static void _saveData(uint8_t close) { if (!(status & FTP_RECEIVING)) { ftp_log("[FTP dta] Not receiving!\r\n"); file_status = -4; return; } if ((ftpfile == NULL) || (file_fd == FILE_NOT_OPENED)) { ftp_log("[FTP dta] Receive file not opened\r\n"); file_status = -2; return; } if (close == 0) { if (SPIFFS_write(&fs, (spiffs_file)file_fd, (char*)recvDataBuf, recvDataLen) < 0) { //failed SPIFFS_fflush(&fs,file_fd); SPIFFS_close(&fs,file_fd); file_fd = FILE_NOT_OPENED; ftp_log("\r\n[FTP dta] Write to local file failed\r\n"); file_status = -3; } else { ftp_log("\r[FTP dta] received: %d", recvDataLen); file_status += recvDataLen; } } else { // close file; SPIFFS_fflush(&fs,file_fd); SPIFFS_close(&fs,file_fd); file_fd = FILE_NOT_OPENED; free(ftpfile); ftpfile = NULL; if (close == 2) { ftp_log("\r\n[FTP dta] Data ERROR, file closed\r\n"); file_status = -1; } else { ftp_log("\r\n[FTP dta] Data file closed\r\n"); } } }
static int file_compile( lua_State* L ) { Proto* f; int file_fd = FILE_NOT_OPENED; size_t len; const char *fname = luaL_checklstring( L, 1, &len ); if ( len > SPIFFS_OBJ_NAME_LEN ) return luaL_error(L, "filename too long"); char output[SPIFFS_OBJ_NAME_LEN]; strcpy(output, fname); // check here that filename end with ".lua". if (len < 4 || (strcmp( output + len - 4, ".lua") != 0) ) return luaL_error(L, "not a .lua file"); output[strlen(output) - 2] = 'c'; output[strlen(output) - 1] = '\0'; if (luaL_loadfile(L, fname) != 0) { return luaL_error(L, lua_tostring(L, -1)); } f = toproto(L, -1); int stripping = 1; /* strip debug information? */ file_fd = SPIFFS_open(&fs,(char*)output,mode2flag("w+"),0); if (file_fd < FILE_NOT_OPENED) { return luaL_error(L, "cannot open/write to file"); } lua_lock(L); int result = luaU_dump(L, f, writer, &file_fd, stripping); lua_unlock(L); SPIFFS_fflush(&fs,file_fd); SPIFFS_close(&fs,file_fd); file_fd =FILE_NOT_OPENED; if (result == LUA_ERR_CC_INTOVERFLOW) { return luaL_error(L, "value too big or small for target integer type"); } if (result == LUA_ERR_CC_NOTINTEGER) { return luaL_error(L, "target lua_Number is integral but fractional value found"); } return 0; }
//================================== static int file_recv( lua_State* L ) { int32_t fsize = 0; uint8_t c, gnm; char fnm[SPIFFS_OBJ_NAME_LEN]; char buff[LUAL_BUFFERSIZE]; spiffs_DIR d; struct spiffs_dirent e; struct spiffs_dirent *pe = &e; uint32_t total, used; SPIFFS_info(&fs, &total, &used); if(total>2000000 || used>2000000 || used > total) { return luaL_error(L, "file system error");; } gnm = 0; if (lua_gettop(L) == 1 && lua_type( L, 1 ) == LUA_TSTRING) { size_t len; const char *fname = luaL_checklstring( L, 1, &len ); if (len > 0 && len < SPIFFS_OBJ_NAME_LEN) { // use given file name for (c=0; c<len; c++) { fnm[c] = fname[c]; } fnm[len] = '\0'; gnm = 1; } } if (FILE_NOT_OPENED != file_fd) { SPIFFS_close(&fs,file_fd); file_fd = FILE_NOT_OPENED; } l_message(NULL,"Start Ymodem file transfer..."); while (MicoUartRecv( MICO_UART_1, &c, 1, 10 ) == kNoErr) {} fsize = Ymodem_Receive(fnm, total-used-10000, gnm); luaWdgReload(); mico_thread_msleep(500); while (MicoUartRecv( MICO_UART_1, &c, 1, 10 ) == kNoErr) {} if (FILE_NOT_OPENED != file_fd) { SPIFFS_fflush(&fs,file_fd); SPIFFS_close(&fs,file_fd); file_fd = FILE_NOT_OPENED; } mico_thread_msleep(500); if (fsize > 0) { sprintf(buff,"\r\nReceived successfully, %d\r\n",fsize); l_message(NULL,buff); } else if (fsize == -1) { l_message(NULL,"\r\nFile write error!\r\n"); } else if (fsize == -2) { l_message(NULL,"\r\nFile open error!\r\n"); } else if (fsize == -3) { l_message(NULL,"\r\nAborted.\r\n"); } else if (fsize == -4) { l_message(NULL,"\r\nFile size too big, aborted.\r\n"); } else { l_message(NULL,"\r\nReceive failed!"); } if (fsize > 0) { SPIFFS_opendir(&fs, "/", &d); while ((pe = SPIFFS_readdir(&d, pe))) { sprintf(buff," %-32s size: %i", pe->name, pe->size); l_message(NULL,buff); } SPIFFS_closedir(&d); } return 0; }
int myspiffs_flush( int fd ){ return SPIFFS_fflush(&fs, (spiffs_file)fd); }
int fileFlush(file_t file) { return SPIFFS_fflush(&_filesystemStorageHandle, file); }
} TEST_END #endif TEST(eof_tell_72) { fs_reset(); s32_t res; spiffs_file fd = SPIFFS_open(FS, "file", SPIFFS_CREAT | SPIFFS_RDWR | SPIFFS_APPEND, 0); TEST_CHECK_GT(fd, 0); TEST_CHECK_EQ(SPIFFS_eof(FS, fd), 1); TEST_CHECK_EQ(SPIFFS_tell(FS, fd), 0); res = SPIFFS_write(FS, fd, "test", 4); TEST_CHECK_EQ(res, 4); TEST_CHECK_EQ(SPIFFS_eof(FS, fd), 1); TEST_CHECK_EQ(SPIFFS_tell(FS, fd), 4); res = SPIFFS_fflush(FS, fd); TEST_CHECK_EQ(res, SPIFFS_OK); TEST_CHECK_EQ(SPIFFS_eof(FS, fd), 1); TEST_CHECK_EQ(SPIFFS_tell(FS, fd), 4); res = SPIFFS_lseek(FS, fd, 2, SPIFFS_SEEK_SET); TEST_CHECK_EQ(res, 2); TEST_CHECK_EQ(SPIFFS_eof(FS, fd), 0); TEST_CHECK_EQ(SPIFFS_tell(FS, fd), 2); res = SPIFFS_write(FS, fd, "test", 4); TEST_CHECK_EQ(res, 4); TEST_CHECK_EQ(SPIFFS_eof(FS, fd), 1); TEST_CHECK_EQ(SPIFFS_tell(FS, fd), 8); res = SPIFFS_fflush(FS, fd); TEST_CHECK_EQ(res, SPIFFS_OK); TEST_CHECK_EQ(SPIFFS_eof(FS, fd), 1); TEST_CHECK_EQ(SPIFFS_tell(FS, fd), 8); res = SPIFFS_close(FS, fd); TEST_CHECK_EQ(res, SPIFFS_OK); TEST_CHECK_LT(SPIFFS_eof(FS, fd), SPIFFS_OK); TEST_CHECK_LT(SPIFFS_tell(FS, fd), SPIFFS_OK); fd = SPIFFS_open(FS, "file", SPIFFS_RDWR, 0); TEST_CHECK_GT(fd, 0); TEST_CHECK_EQ(SPIFFS_eof(FS, fd), 0); TEST_CHECK_EQ(SPIFFS_tell(FS, fd), 0); res = SPIFFS_lseek(FS, fd, 2, SPIFFS_SEEK_SET); TEST_CHECK_EQ(res, 2); TEST_CHECK_EQ(SPIFFS_eof(FS, fd), 0); TEST_CHECK_EQ(SPIFFS_tell(FS, fd), 2); res = SPIFFS_write(FS, fd, "test", 4); TEST_CHECK_EQ(res, 4); TEST_CHECK_EQ(SPIFFS_eof(FS, fd), 0); TEST_CHECK_EQ(SPIFFS_tell(FS, fd), 6); res = SPIFFS_fflush(FS, fd); TEST_CHECK_EQ(res, SPIFFS_OK); TEST_CHECK_EQ(SPIFFS_eof(FS, fd), 0); TEST_CHECK_EQ(SPIFFS_tell(FS, fd), 6); res = SPIFFS_lseek(FS, fd, 0, SPIFFS_SEEK_END); TEST_CHECK_EQ(res, 8); TEST_CHECK_EQ(SPIFFS_eof(FS, fd), 1); TEST_CHECK_EQ(SPIFFS_tell(FS, fd), 8); return TEST_RES_OK; } TEST_END