static int32_t backup_log(log_t *log) { int32_t ret = 0; char bakName[LOG_NAME_LEN]; char name[LOG_NAME_LEN]; time_t t = 0; struct tm *ts = NULL; if (0 == (log->mode & LOG_TO_FILE)) { return 0; } MkDir(log->dir); OS_RWLOCK_WRLOCK(&log->rwlock); t = time(NULL); ts = localtime(&t); if (NULL == ts) { OS_SNPRINTF(bakName, LOG_NAME_LEN, "%s/%s.log.bak", log->dir, log->name); } else { OS_SNPRINTF(bakName, LOG_NAME_LEN, "%s/%s_%04d%02d%02d_%02d%02d%02d.log", log->dir, log->name, ts->tm_year+1900, ts->tm_mon+1, ts->tm_mday, ts->tm_hour, ts->tm_min, ts->tm_sec); } OS_SNPRINTF(name, LOG_NAME_LEN, "%s/%s.log", log->dir, log->name); if (NULL != log->disk_hnd) { os_file_close(log->disk_hnd); log->disk_hnd = NULL; } unlink(bakName); rename(name, bakName); if (NULL == open_log(log)) { ret = -1; } log->total_lines = 0; OS_RWLOCK_WRUNLOCK(&log->rwlock); return ret; }
void log_close(void *log) { log_t *tmp_log = (log_t *)log; if (NULL == tmp_log) { return; } if (NULL != tmp_log->disk_hnd) { char date_time[DATA_TIME_STR_LEN]; os_get_date_time_string(date_time, DATA_TIME_STR_LEN); os_file_printf(tmp_log->disk_hnd, "%s %s\n", date_time, "NOTE: LOG FILE CLOSE!!!"); os_file_close(tmp_log->disk_hnd); tmp_log->disk_hnd = NULL; } OS_RWLOCK_DESTROY(&tmp_log->rwlock); OS_FREE(tmp_log); }
//***************************************************************************** // Function : GetFile // Description : Get a file from host. // Input : aIdx // index of file to download // Return : 0/-1; // 0 => Success // // -1 => Error // Note : N/A // Globals Changed : N/A //***************************************************************************** static int GetFile(int aIdx) { T_APP_INFO app_list; int retry, j; int len, rx_len; BYTE auxname[30+1]; BYTE linea[30+1]; DWORD getfile_mark, rx_mark, which; int handle; TBuffers.gRxBuf = MallocMW(COMM_BUFF_SIZE); // buffer de recepcion de datos TBuffers.gTxBuf = MallocMW(COMM_BUFF_SIZE); // buffer de recepcion de datos memset(TBuffers.gRxBuf,0x00,COMM_BUFF_SIZE); memset(TBuffers.gTxBuf,0x00,COMM_BUFF_SIZE); for (retry=0; retry<NUM_OF_RETRY; retry++) { FlushAll (); which = MW_FTP_DATA_CHNL; IOCtlMW(gFtpHandle, IO_FTP_DISCONNECT, &which); FlushAll (); handle = os_file_open(KTempFile, K_O_RDWR|K_O_TRUNC|K_O_CREAT); if (handle < 0) { //ShowMsgWait("Temp File Open Fail.", TRUE); FreeMW(TBuffers.gRxBuf); FreeMW(TBuffers.gTxBuf); return -1; } //SprintfMW ((char *)gTxBuf, "\x12\x1b\x20""Dnld %s..\x5", gFileList[aIdx]); //PrintfMW(gTxBuf); // clear_area_screen(4,2,7,20,FALSE,FALSE); sprintf ((char *)TBuffers.gTxBuf, "Descarga %.8s", gFileList[aIdx]); memset(auxname,0x00,sizeof(auxname)); memcpy(auxname,gFileList[aIdx],sizeof(auxname)); memset(linea, 0x00, sizeof(linea)); // printf("\f\x1C VERSIONES \x1D"); for( j = 0; j < 50; j++ ) { if(os_app_search_id(j) == TRUE) { os_app_info(j , &app_list); memset(linea,0x00,sizeof(linea)); sprintf(linea, "%s-%04X.tms", &app_list.s_app_name[2], app_list.w_checksum); //printf("\f %s \n %s\n %d",linea,gFileList[aIdx], gNumOfFiles);APM_WaitKey(3000,0); if(memcmp(linea,auxname,30)==0){ os_file_close(handle); FreeMW(TBuffers.gRxBuf); FreeMW(TBuffers.gTxBuf); return 1; } } } //ShowMsgWait(TBuffers.gTxBuf, FALSE); // centerS(4,2,21,TBuffers.gTxBuf); SprintfMW ((char *)TBuffers.gTxBuf, "GET I /%s%s", STR_TEST_DIR_NAME, gFileList[aIdx]); if (IOCtlMW(gFtpHandle, IO_FTP_CMD_SEND, TBuffers.gTxBuf) == FALSE) { //ShowMsgWait("GET:Command Fail.", TRUE); break; } if (WaitStatus(MW_FTP_STAT_DATA_CONNED, TIMEOUT_STATUS) < 0) { graphicWindow("ERROR FTP"); // centerS(3,1,21,"Falla al"); // centerS(4,1,21,"Obtener los"); // centerS(5,1,21,"datos de FTP"); //ShowMsgWait("GET:Fail.", TRUE); break; } gRxSpeed = 0; rx_len = 0; getfile_mark = FreeRunMark(); while (1) { DWORD timeout=0; int ret; ret = GetCmdResp(); if (ret >= 400) { PrintfMW("\x1B\xE0\x1CGET:%d\x05\x1D", ret); break; } rx_mark = FreeRunMark(); do { len = ReadMW(gFtpHandle, TBuffers.gRxBuf, COMM_BUFF_SIZE); if (len > 0) { os_file_write(handle, TBuffers.gRxBuf, len); // save to file break; } if (WaitStatus(MW_FTP_STAT_DATA_CONNED, 1) < 0) { timeout = TIMEOUT_READ_BLOCK; break; } //SleepMW(); } while ((timeout = FreeRunElapsed(rx_mark)) < TIMEOUT_READ_BLOCK && GetCharMW() != MWKEY_CANCL); if (GetCharMW() == MWKEY_CANCL) break; if (timeout >= TIMEOUT_READ_BLOCK) // End ? break; rx_len += len; printf("\fRx: %3d.%d K.bytes",rx_len/1000,(rx_len%1000)/100); //PrintfMW("\x1B\x80""RXed %d bytes\x05\n\x05\n\x05", rx_len); } os_file_close(handle); if (rx_len == 0) { graphicWindow("ERROR FTP"); // clear_area_screen(2,2,7,20,FALSE,FALSE); // centerS(4,1,21,"RX Timeout."); ; os_file_close(handle); continue; } os_file_delete(gFileList[aIdx]); os_file_rename(gFileList[aIdx], KTempFile); which = MW_FTP_DATA_CHNL; IOCtlMW(gFtpHandle, IO_FTP_DISCONNECT, &which); // Close Data channel gRxSpeed = (rx_len/(FreeRunElapsed(getfile_mark)/1000)); //PrintfMW("\x1B\xA0Rx Spd:%d Bps\n", gRxSpeed); FreeMW(TBuffers.gRxBuf); FreeMW(TBuffers.gTxBuf); return 0; } os_file_close(handle); FreeMW(TBuffers.gRxBuf); FreeMW(TBuffers.gTxBuf); return -1; }