int32 OS_mv (const char *src, const char *dest) { int32 status; status = OS_cp (src, dest); if ( status == OS_FS_SUCCESS ) { status = OS_remove(src); } return ( status); }
int32 OS_mv (const char *src, const char *dest) { int i; int32 status; /* ** Validate the source and destination ** These checks may seem redundant because OS_cp and OS_remove also do ** the same checks, but this call needs to abort before doing a copy ** in some cases. */ /* ** Check to see if the path pointers are NULL */ if (src == NULL || dest == NULL) { return OS_FS_ERR_INVALID_POINTER; } /* ** Check to see if the paths are too long */ if (strlen(src) >= OS_MAX_PATH_LEN) { return OS_FS_ERR_PATH_TOO_LONG; } if (strlen(dest) >= OS_MAX_PATH_LEN) { return OS_FS_ERR_PATH_TOO_LONG; } /* ** check if the names of the files are too long */ if (OS_check_name_length(src) != OS_FS_SUCCESS) { return OS_FS_ERR_NAME_TOO_LONG; } if (OS_check_name_length(dest) != OS_FS_SUCCESS) { return OS_FS_ERR_NAME_TOO_LONG; } /* ** Make sure the source file is not open by the OSAL before doing the move */ for ( i =0; i < OS_MAX_NUM_OPEN_FILES; i++) { if ((OS_FDTable[i].IsValid == TRUE) && (strcmp(OS_FDTable[i].Path, src) == 0)) { return OS_FS_ERROR; } } status = OS_cp (src, dest); if ( status == OS_FS_SUCCESS ) { status = OS_remove(src); } return ( status); }/*end OS_mv */
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ int main(void) { /* ** Set up output results text file */ UTF_set_output_filename("ds_utest.out"); /* ** Set up HK packet handler */ UTF_set_packet_handler(DS_HK_TLM_MID, (utf_packet_handler)PrintHKPacket); /* ** Initialize time data structures */ UTF_init_sim_time(0.0); /* ** Initialize ES application data */ UTF_ES_InitAppRecords(); UTF_ES_AddAppRecord("DS",0); CFE_ES_RegisterApp(); /* ** Initialize CDS and table services data structures */ CFE_ES_CDS_EarlyInit(); CFE_TBL_EarlyInit(); /* * Setup the virtual/physical file system mapping... * * The following local machine directory structure is required: * * ... ds/fsw/unit_test <-- this is the current working directory * ... ds/fsw/unit_test/disk/TT <-- physical location for virtual disk "/tt" */ UTF_add_volume("/TT", "disk", FS_BASED, FALSE, FALSE, TRUE, "TT", "/tt", 0); /* ** Delete files created during previous tests */ OS_remove("/tt/app00002000.tlm"); OS_remove("/tt/app1980001000000.hk"); OS_remove("/tt/b_00000000.x"); OS_remove("/tt/b_99999999.x"); /* ** Required setup prior to calling many CFE API functions */ CFE_ES_RegisterApp(); CFE_EVS_Register(NULL, 0, 0); /* ** Run DS application unit tests */ UTF_put_text("\n*** DS -- Testing ds_cmds.c ***\n"); Test_cmds(); UTF_put_text("\n*** DS -- Testing ds_file.c ***\n"); Test_file(); UTF_put_text("\n*** DS -- Testing ds_table.c ***\n"); Test_table(); UTF_put_text("\n*** DS -- Testing ds_app.c ***\n"); Test_app(); UTF_put_text("\n*** DS -- Total test count = %d, total test errors = %d\n\n", UT_TotalTestCount, UT_TotalFailCount); /* ** Invoke the main loop "success" test now because the program ** will end when the last entry in the SB input file is read. */ UTF_CFE_ES_Set_Api_Return_Code(CFE_ES_RUNLOOP_PROC, TRUE); UTF_CFE_TBL_Set_Api_Return_Code (CFE_TBL_GETSTATUS_PROC, CFE_TBL_INFO_UPDATE_PENDING); DS_AppMain(); return 0; } /* End of main() */
/* -------------------------------------------------------------------------------------- Name: OS_ShellOutputToFile Purpose: Takes a shell command in and writes the output of that command to the specified file Returns: OS_FS_ERROR if the command was not executed properly OS_FS_ERR_INVALID_FD if the file descriptor passed in is invalid OS_SUCCESS if success ---------------------------------------------------------------------------------------*/ int32 OS_ShellOutputToFile(char* Cmd, int32 OS_fd) { int32 cmdFd; int32 tmpFd; rtems_status_code rtemsRc;; int32 ReturnCode = OS_SUCCESS; int32 fileStatus; int32 bytesRead; int32 bytesWritten; char readBuffer[256]; char outputFileName[OS_MAX_PATH_LEN + OS_SHELL_TMP_FILE_EXT_LEN]; char localCmd[OS_MAX_CMD_LEN]; /* Make sure the file descriptor is legit before using it */ if (OS_fd < 0 || OS_fd >= OS_MAX_NUM_OPEN_FILES || OS_FDTable[OS_fd].IsValid == FALSE) { ReturnCode = OS_FS_ERR_INVALID_FD; } else { /* ** Create a file to write the command to (or write over the old one) */ cmdFd = OS_creat(OS_SHELL_CMD_INPUT_FILE_NAME,OS_READ_WRITE); if (cmdFd < OS_FS_SUCCESS) { ReturnCode = OS_FS_ERROR; } else { /* ** Write the command to the buffer */ strncpy(localCmd,Cmd,OS_MAX_CMD_LEN); strncat(localCmd,"\n",1); /* ** This function passes in an open file descriptor to write the shell ** command output. The RTEMS shell script API expects a filename, not ** a file descriptor. So in addition to the temporary file for the shell input, ** we need to create a temporary file for the command output, then it has ** to be copied to the open file. */ strncpy(outputFileName,OS_SHELL_CMD_INPUT_FILE_NAME,OS_MAX_PATH_LEN); strncat(outputFileName,OS_SHELL_TMP_FILE_EXT,OS_SHELL_TMP_FILE_EXT_LEN); /* ** copy the shell command buffer to the file */ fileStatus = OS_write(cmdFd, localCmd, strlen(localCmd)); if ( fileStatus == strlen(localCmd) ) { /* ** Close the file */ OS_close(cmdFd); /* ** Spawn a task to execute the shell command */ rtemsRc = rtems_shell_script ( "RSHL", OS_SHELL_CMD_TASK_STACK_SIZE, OS_SHELL_CMD_TASK_PRIORITY, OS_SHELL_CMD_INPUT_FILE_NAME, outputFileName, FALSE, /* Do not append output to file */ TRUE, /* Wait for shell task to complete */ FALSE /* Echo output */ ); /* ** Now we have the temporary file with the output */ if((tmpFd = OS_open(outputFileName, OS_READ_ONLY,0)) == OS_FS_ERROR) { printf("OSAL:Could not open %s for reading\n",outputFileName); ReturnCode = OS_FS_ERROR; } else { while((bytesRead = OS_read(tmpFd, readBuffer, 256)) > 0) { bytesWritten = OS_write(OS_fd, readBuffer, bytesRead); } OS_close(tmpFd); /* ** Remove the temporary output file */ fileStatus = OS_remove(outputFileName); } } else { ReturnCode = OS_FS_ERROR; } } /* ** Remove the temporary shell input file */ fileStatus = OS_remove(OS_SHELL_CMD_INPUT_FILE_NAME); } return ReturnCode; }/* end OS_ShellOutputToFile */
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ int32 CFE_ES_ShellOutputCommand(char * CmdString, char *Filename) { int32 Result; int32 ReturnCode = CFE_SUCCESS; int32 fd; int32 FileSize; int32 CurrFilePtr; uint32 i; /* the extra 1 added for the \0 char */ char CheckCmd [CFE_ES_CHECKSIZE + 1]; char Cmd [CFE_ES_MAX_SHELL_CMD]; char OutputFilename [OS_MAX_PATH_LEN]; /* Use default filename if not provided */ if (Filename[0] == '\0') { strncpy(OutputFilename, CFE_ES_DEFAULT_SHELL_FILENAME, OS_MAX_PATH_LEN); } else { strncpy(OutputFilename, Filename, OS_MAX_PATH_LEN); } /* Make sure string is null terminated */ OutputFilename[OS_MAX_PATH_LEN - 1] = '\0'; /* Remove previous version of output file */ OS_remove(OutputFilename); fd = OS_creat(OutputFilename, OS_READ_WRITE); if (fd < OS_FS_SUCCESS) { Result = OS_FS_ERROR; } else { strncpy(CheckCmd,CmdString,CFE_ES_CHECKSIZE); CheckCmd[CFE_ES_CHECKSIZE] = '\0'; strncpy(Cmd,CmdString, CFE_ES_MAX_SHELL_CMD); /* We need to check if this command is directed at ES, or at the operating system */ if (strncmp(CheckCmd,"ES_",CFE_ES_CHECKSIZE) == 0) { /* This list can be expanded to include other ES functionality */ if ( strncmp(Cmd,CFE_ES_LIST_APPS_CMD,strlen(CFE_ES_LIST_APPS_CMD) )== 0) { Result = CFE_ES_ListApplications(fd); } else if ( strncmp(Cmd,CFE_ES_LIST_TASKS_CMD,strlen(CFE_ES_LIST_TASKS_CMD) )== 0) { Result = CFE_ES_ListTasks(fd); } else if ( strncmp(Cmd,CFE_ES_LIST_RESOURCES_CMD,strlen(CFE_ES_LIST_RESOURCES_CMD) )== 0) { Result = CFE_ES_ListResources(fd); } /* default if there is not an ES command that matches */ else { Result = CFE_ES_ERR_SHELL_CMD; CFE_ES_WriteToSysLog("There is no ES Shell command that matches %s \n",Cmd); } } /* if the command is not directed at ES, pass it through to the * underlying OS */ else { Result = OS_ShellOutputToFile(Cmd,fd); } /* seek to the end of the file to get it's size */ FileSize = OS_lseek(fd,0,OS_SEEK_END); if (FileSize == OS_FS_ERROR) { OS_close(fd); CFE_ES_WriteToSysLog("OS_lseek call failed from CFE_ES_ShellOutputCmd 1\n"); Result = OS_FS_ERROR; } /* We want to add 3 characters at the end of the telemetry,'\n','$','\0'. * To do this we need to make sure there are at least 3 empty characters at * the end of the last CFE_ES_MAX_SHELL_PKT so we don't over write any data. If * the current file has only 0,1, or 2 free spaces at the end, we want to * make the file longer to start a new tlm packet of size CFE_ES_MAX_SHELL_PKT. * This way we will get a 'blank' packet with the correct 3 characters at the end. */ else { /* if we are within 2 bytes of the end of the packet*/ if ( FileSize % CFE_ES_MAX_SHELL_PKT > (CFE_ES_MAX_SHELL_PKT - 3)) { /* add enough bytes to start a new packet */ for (i = 0; i < CFE_ES_MAX_SHELL_PKT - (FileSize % CFE_ES_MAX_SHELL_PKT) + 1 ; i++) { OS_write(fd," ",1); } } else { /* we are exactly at the end */ if( FileSize % CFE_ES_MAX_SHELL_PKT == 0) { OS_write(fd," ",1); } } /* seek to the end of the file again to get it's new size */ FileSize = OS_lseek(fd,0,OS_SEEK_END); if (FileSize == OS_FS_ERROR) { OS_close(fd); CFE_ES_WriteToSysLog("OS_lseek call failed from CFE_ES_ShellOutputCmd 2\n"); Result = OS_FS_ERROR; } else { /* set the file back to the beginning */ OS_lseek(fd,0,OS_SEEK_SET); /* start processing the chunks. We want to have one packet left so we are sure this for loop * won't run over */ for (CurrFilePtr=0; CurrFilePtr < (FileSize - CFE_ES_MAX_SHELL_PKT); CurrFilePtr += CFE_ES_MAX_SHELL_PKT) { OS_read(fd, CFE_ES_TaskData.ShellPacket.ShellOutput, CFE_ES_MAX_SHELL_PKT); /* Send the packet */ CFE_SB_TimeStampMsg((CFE_SB_Msg_t *) &CFE_ES_TaskData.ShellPacket); CFE_SB_SendMsg((CFE_SB_Msg_t *) &CFE_ES_TaskData.ShellPacket); /* delay to not flood the pipe on large messages */ OS_TaskDelay(200); } /* finish off the last portion of the file */ /* over write the last packet with spaces, then it will get filled * in with the correct info below. This assures that the last non full * part of the packet will be spaces */ for (i =0; i < CFE_ES_MAX_SHELL_PKT; i++) { CFE_ES_TaskData.ShellPacket.ShellOutput[i] = ' '; } OS_read(fd, CFE_ES_TaskData.ShellPacket.ShellOutput, ( FileSize - CurrFilePtr)); /* From our check above, we are assured that there are at least 3 free * characters to write our data into at the end of this last packet * * The \n assures we are on a new line, the $ gives us our prompt, and the * \0 assures we are null terminalted. */ CFE_ES_TaskData.ShellPacket.ShellOutput[ CFE_ES_MAX_SHELL_PKT - 3] = '\n'; CFE_ES_TaskData.ShellPacket.ShellOutput[ CFE_ES_MAX_SHELL_PKT - 2] = '$'; CFE_ES_TaskData.ShellPacket.ShellOutput[ CFE_ES_MAX_SHELL_PKT - 1] = '\0'; /* Send the last packet */ CFE_SB_TimeStampMsg((CFE_SB_Msg_t *) &CFE_ES_TaskData.ShellPacket); CFE_SB_SendMsg((CFE_SB_Msg_t *) &CFE_ES_TaskData.ShellPacket); /* Close the file descriptor */ OS_close(fd); } /* if FilseSize == OS_FS_ERROR */ } /* if FileSeize == OS_FS_ERROR */ }/* if fd < OS_FS_SUCCESS */ if (Result != OS_SUCCESS && Result != CFE_SUCCESS ) { ReturnCode = CFE_ES_ERR_SHELL_CMD; CFE_ES_WriteToSysLog("OS_ShellOutputToFile call failed from CFE_ES_ShellOutputCommand\n"); } else { ReturnCode = CFE_SUCCESS; } return ReturnCode; }