fp@1335: /***************************************************************************** fp@1335: * fp@1335: * $Id$ fp@1335: * fp@1335: ****************************************************************************/ fp@1335: fp@1335: #include // basename() fp@1335: fp@1335: #include fp@1335: #include fp@1335: #include fp@1335: using namespace std; fp@1335: fp@1335: #include "CommandFoeWrite.h" fp@1335: fp@1335: /*****************************************************************************/ fp@1335: fp@1335: CommandFoeWrite::CommandFoeWrite(): fp@1335: FoeCommand("foe_write", "Store a file on a slave via FoE.") fp@1335: { fp@1335: } fp@1335: fp@1335: /*****************************************************************************/ fp@1335: fp@1335: string CommandFoeWrite::helpString() const fp@1335: { fp@1335: stringstream str; fp@1335: fp@1335: str << getName() << " [OPTIONS] " << endl fp@1335: << endl fp@1335: << getBriefDescription() << endl fp@1335: << endl fp@1335: << "This command requires a single slave to be selected." << endl fp@1335: << endl fp@1335: << "Arguments:" << endl fp@1335: << " FILENAME can either be a path to a file, or '-'. In" << endl fp@1335: << " the latter case, data are read from stdin and" << endl fp@1335: << " the --output-file option has to be specified." << endl fp@1335: << endl fp@1335: << "Command-specific options:" << endl fp@1335: << " --output-file -o Target filename on the slave." << endl fp@1335: << " If the FILENAME argument is" << endl fp@1335: << " '-', this is mandatory." << endl fp@1335: << " Otherwise, the basename() of" << endl fp@1335: << " FILENAME is used by default." << endl fp@1335: << " --alias -a " << endl fp@1335: << " --position -p Slave selection. See the help" << endl fp@1335: << " of the 'slaves' command." << endl fp@1335: << endl fp@1335: << numericInfo(); fp@1335: fp@1335: return str.str(); fp@1335: } fp@1335: fp@1335: /****************************************************************************/ fp@1335: fp@1335: void CommandFoeWrite::execute(MasterDevice &m, const StringVector &args) fp@1335: { fp@1335: stringstream err; fp@1335: ec_ioctl_slave_foe_t data; fp@1335: ifstream file; fp@1335: SlaveList slaves; fp@1335: string storeFileName; fp@1335: fp@1335: if (args.size() != 1) { fp@1335: err << "'" << getName() << "' takes exactly one argument!"; fp@1335: throwInvalidUsageException(err); fp@1335: } fp@1335: fp@1335: if (args[0] == "-") { fp@1335: loadFoeData(&data, cin); fp@1335: if (getOutputFile().empty()) { fp@1335: err << "Please specify a filename for the slave side" fp@1335: << " with --output-file!"; fp@1335: throwCommandException(err); fp@1335: } else { fp@1335: storeFileName = getOutputFile(); fp@1335: } fp@1335: } else { fp@1335: file.open(args[0].c_str(), ifstream::in | ifstream::binary); fp@1335: if (file.fail()) { fp@1335: err << "Failed to open '" << args[0] << "'!"; fp@1335: throwCommandException(err); fp@1335: } fp@1335: loadFoeData(&data, file); fp@1335: file.close(); fp@1335: if (getOutputFile().empty()) { fp@1335: char *cpy = strdup(args[0].c_str()); // basename can modify fp@1335: // the string contents fp@1335: storeFileName = basename(cpy); fp@1335: free(cpy); fp@1335: } else { fp@1335: storeFileName = getOutputFile(); fp@1335: } fp@1335: } fp@1335: fp@1335: try { fp@1335: m.open(MasterDevice::ReadWrite); fp@1335: } catch (MasterDeviceException &e) { fp@1335: if (data.buffer_size) fp@1335: delete [] data.buffer; fp@1335: throw e; fp@1335: } fp@1335: fp@1335: slaves = selectedSlaves(m); fp@1335: if (slaves.size() != 1) { fp@1335: if (data.buffer_size) fp@1335: delete [] data.buffer; fp@1335: throwSingleSlaveRequired(slaves.size()); fp@1335: } fp@1335: data.slave_position = slaves.front().position; fp@1335: fp@1335: // write data via foe to the slave fp@1335: data.offset = 0; fp@1335: strncpy(data.file_name, storeFileName.c_str(), sizeof(data.file_name)); fp@1335: fp@1335: try { fp@1335: m.writeFoe(&data); fp@1335: } catch (MasterDeviceException &e) { fp@1335: if (data.buffer_size) fp@1335: delete [] data.buffer; fp@1335: if (data.abort_code) { fp@1335: err << "Failed to write via FoE: " fp@1335: << errorString(data.abort_code); fp@1335: throwCommandException(err); fp@1335: } else { fp@1335: throw e; fp@1335: } fp@1335: } fp@1335: fp@1335: if (getVerbosity() == Verbose) { fp@1335: cerr << "FoE writing finished." << endl; fp@1335: } fp@1335: fp@1335: if (data.buffer_size) fp@1335: delete [] data.buffer; fp@1335: } fp@1335: fp@1335: /*****************************************************************************/ fp@1335: fp@1335: void CommandFoeWrite::loadFoeData( fp@1335: ec_ioctl_slave_foe_t *data, fp@1335: const istream &in fp@1335: ) fp@1335: { fp@1335: stringstream err; fp@1335: ostringstream tmp; fp@1335: fp@1335: tmp << in.rdbuf(); fp@1335: string const &contents = tmp.str(); fp@1335: fp@1335: if (getVerbosity() == Verbose) { fp@1335: cerr << "Read " << contents.size() << " bytes of FoE data." << endl; fp@1335: } fp@1335: fp@1335: data->buffer_size = contents.size(); fp@1335: fp@1335: if (data->buffer_size) { fp@1335: // allocate buffer and read file into buffer fp@1335: data->buffer = new uint8_t[data->buffer_size]; fp@1335: contents.copy((char *) data->buffer, contents.size()); fp@1335: } fp@1335: } fp@1335: fp@1335: /*****************************************************************************/