📄 netdrv.c
字号:
char *pDirName = (char *) malloc ((unsigned) (strlen (name) + 1)); char *pFileName = (char *) malloc ((unsigned) (strlen (name) + 1)); /* don't allow null filenames */ if (name[0] == EOS) { errnoSet (S_ioLib_NO_FILENAME); return (ERROR); } /* get directory and filename */ pathSplit (name, pDirName, pFileName); remCurIdGet (usr, passwd); if (pNetDev->protocol == PROTO_FTP) { if (ftpXfer (pNetDev->host, usr, passwd, "", "NLST %s", pDirName, pFileName, &ctrlSock, &dataSock) == ERROR) { free (pDirName); free (pFileName); return (ERROR); } } else { sprintf (command, netLsStr, name); dataSock = rcmd (pNetDev->host, RSHD, usr, usr, command, &ctrlSock); if (dataSock == ERROR) { free (pDirName); free (pFileName); return (ERROR); } } if (pNetDev->protocol == PROTO_FTP) { /* check dataSock for ls error message */ if (read (dataSock, buf, sizeof (buf)) <= 0 || strncmp (buf + strlen (pFileName) + 1, lsErrMsg, lsErrMsgLen) == 0) { status = ERROR; } close (dataSock); if (ftpReplyGet (ctrlSock, FALSE) != FTP_COMPLETE) status = ERROR; } else { /* check control socket for error */ if (read (ctrlSock, buf, sizeof (buf)) > 0) { /* set the task's status according to the UNIX error */ getNetStatus (buf); status = ERROR; } close (dataSock); } if (pNetDev->protocol == PROTO_FTP && ftpCommand (ctrlSock, "QUIT",0,0,0,0,0,0) != FTP_COMPLETE) { status = ERROR; } close (ctrlSock); free (pDirName); free (pFileName); return (status); }/********************************************************************************* netGet - downLoad a file from a remote machine via the network.** The remote shell daemon on the machine 'host' is used to download* the given file to the specified previously opened network file descriptor.* The remote userId should have been set previously by a call to iam().* If the file does not exist, the error message from the UNIX 'host'* is printed to the VxWorks standard error file descriptor and ERROR * is returned.** RETURNS: OK or ERROR.*/LOCAL STATUS netGet ( FAST NET_FD *pNetFd ) { int dataSock; int ctrlSock; char buf [DATASIZE]; /* make buf the same size as NET_FD's databuf */ FAST int nBytes = -1; char command[100]; char buffer [100]; int saveOptions; STATUS status = OK; char usr [MAX_IDENTITY_LEN]; char passwd [MAX_IDENTITY_LEN]; char *errMsg = "cat: read error: Is a directory"; int errMsgLen = strlen (errMsg); remCurIdGet (usr, passwd); if (pNetFd->pNetDev->protocol == PROTO_FTP) { if (ftpXfer (pNetFd->pNetDev->host, usr, passwd, "", "RETR %s", pNetFd->remDirName, pNetFd->remFileName, &ctrlSock, &dataSock) == ERROR) { return (ERROR); } } else { if (pathCat (pNetFd->remDirName, pNetFd->remFileName, buffer) == ERROR) return (ERROR); sprintf (command, "/bin/cat < %s", buffer); dataSock = rcmd (pNetFd->pNetDev->host, RSHD, usr, usr, command, &ctrlSock); if (dataSock == ERROR) return (ERROR); } /* Set file pointer to beginning of file */ if (netSeek (pNetFd, 0) == ERROR) { if (pNetFd->pNetDev->protocol == PROTO_FTP && ftpCommand (ctrlSock, "QUIT",0,0,0,0,0,0) != FTP_COMPLETE) { status = ERROR; } close (dataSock); close (ctrlSock); return (ERROR); } /* set mode to write so that file can be written to, * save original options so they can be restored later */ saveOptions = pNetFd->options; pNetFd->options = O_WRONLY & FD_MODE; /* read bytes from socket and write them * out to file descriptor one block at a time */ while ((status == OK) && ((nBytes = read (dataSock, buf, sizeof (buf))) > 0)) { if (netWrite (pNetFd, buf, nBytes) != nBytes) status = ERROR; } if (nBytes < 0) /* recv error */ status = ERROR; close (dataSock); if (pNetFd->pNetDev->protocol == PROTO_FTP) { if (ftpReplyGet (ctrlSock, FALSE) != FTP_COMPLETE) status = ERROR; } else { /* check control socket for error */ if ((nBytes = fioRead (ctrlSock, buf, sizeof (buf) - 1)) > 0) { /* print error message on standard error fd */ buf [nBytes] = EOS; /* insure string termination */ /* check error message indicating cat of NFS mounted directory */ if (strncmp (buf, errMsg, errMsgLen) != 0) { printErr ("%s:%s", pNetFd->pNetDev->host, buf); /* set the task's status according to the UNIX error */ getNetStatus (buf); status = ERROR; } } } if (pNetFd->pNetDev->protocol == PROTO_FTP && ftpCommand (ctrlSock, "QUIT",0,0,0,0,0,0) != FTP_COMPLETE) { status = ERROR; } close (ctrlSock); pNetFd->options = saveOptions; /* restore original options */ return (status); }/********************************************************************************* netPut - upload a file to a remote machine via the network.** The remote shell daemon on the machine 'host' is used to upload* from the open network file descriptor to a remote file.* The remote userId should have been set previously be a call* to iam(). If an error occurs, the UNIX error is output to the* VxWorks standard error fd.** RETURNS: OK or ERROR.*/LOCAL STATUS netPut ( FAST NET_FD *pNetFd ) { int dataSock; int ctrlSock; char buf [DATASIZE]; /* make buf the same size as NET_FD's databuf */ FAST int nBytes = -1; char command[100]; char buffer[100]; int saveOptions; STATUS status = OK; char usr [MAX_IDENTITY_LEN]; char passwd [MAX_IDENTITY_LEN]; remCurIdGet (usr, passwd); if (pNetFd->pNetDev->protocol == PROTO_FTP) { if (ftpXfer (pNetFd->pNetDev->host, usr, passwd, "", "STOR %s", pNetFd->remDirName, pNetFd->remFileName, &ctrlSock, &dataSock) == ERROR) { return (ERROR); } } else { if (pathCat (pNetFd->remDirName, pNetFd->remFileName, buffer) == ERROR) return (ERROR); sprintf (command, "/bin/cat > %s", buffer); dataSock = rcmd (pNetFd->pNetDev->host, RSHD, usr, usr, command, &ctrlSock); if (dataSock == ERROR) return (ERROR); } /* Set file pointer to beginning of file */ if (netSeek (pNetFd, 0) == ERROR) { if (pNetFd->pNetDev->protocol == PROTO_FTP && ftpCommand (ctrlSock, "QUIT",0,0,0,0,0,0) != FTP_COMPLETE) { status = ERROR; } close (dataSock); close (ctrlSock); return (ERROR); } /* set mode to write so that file can be written to, * save original options so they can be restored later */ saveOptions = pNetFd->options; pNetFd->options = O_RDONLY & FD_MODE; /* Read the data from one DATABLOCK into buffer. * Continue until file pointer reaches the end of file. */ while (status == OK && (nBytes = netRead (pNetFd, buf, sizeof (buf))) > 0) { if (write (dataSock, buf, nBytes) != nBytes) status = ERROR; } if (nBytes < 0) /* netRead error */ status = ERROR; if (close (dataSock) == ERROR) status = ERROR; if (pNetFd->pNetDev->protocol == PROTO_FTP) { if (ftpReplyGet (ctrlSock, FALSE) != FTP_COMPLETE) status = ERROR; } else { /* check control socket for error */ if ((nBytes = fioRead (ctrlSock, buf, sizeof (buf) - 1)) > 0) { /* print error message on standard error fd */ buf [nBytes] = EOS; /* insure string termination */ printErr ("%s:%s", pNetFd->pNetDev->host, buf); /* set the task's status according to the UNIX error */ getNetStatus (buf); status = ERROR; } } if (pNetFd->pNetDev->protocol == PROTO_FTP && ftpCommand (ctrlSock, "QUIT",0,0,0,0,0,0) != FTP_COMPLETE) { status = ERROR; } close (ctrlSock); pNetFd->options = saveOptions; /* restore original options */ return (status); }/****************************************************************************** getNetStatus - set task status according to network error** Compares string in buf with some known UNIX errors that can occur when* copying files over the network. Sets task status accordingly.*/LOCAL void getNetStatus ( char *buf /* buffer containing string with UNIX error */ ) { FAST char *pErr = (char *) index (buf, ':'); if (pErr == NULL) errnoSet (S_netDrv_UNIX_FILE_ERROR); else if (strcmp (pErr, ": Permission denied\n") == 0) errnoSet (S_netDrv_PERMISSION_DENIED); else if (strcmp (pErr, ": No such file or directory\n") == 0) errnoSet (S_netDrv_NO_SUCH_FILE_OR_DIR); else if (strcmp (pErr, ": Is a directory\n") == 0) errnoSet (S_netDrv_IS_A_DIRECTORY); else errnoSet (S_netDrv_UNIX_FILE_ERROR); }/********************************************************************************* netRead - read bytes from remote file** netRead reads up to the specified number of bytes from the open network* file descriptor and puts them into a buffer. Bytes are read starting* from the point marked by the file pointer. The file pointer is then* updated to point immediately after the last character that was read.** Called only by the I/O system.** SIDE EFFECTS: moves file pointer** RETURNS: Number of bytes read, or ERROR.*/LOCAL int netRead ( FAST NET_FD *pNetFd, /* pointer to open network file descriptor */ char *buf, /* pointer to buffer to receive bytes */ FAST int maxBytes /* max number of bytes to read into buffer */ ) { STATUS status; FAST int byteCount = 0; /* number of bytes read so far */ FAST DATABLOCK *dptr; /* points to current datablock */ FAST int cindex; /* character index datablock's databuf */ FAST char *cptr; /* points to character being read in the file */ FAST char *bptr; /* points to current position in buffer */ /* check for valid maxbytes */ if (maxBytes <= 0) { errnoSet (S_netDrv_INVALID_NUMBER_OF_BYTES); return (ERROR); } /* if file opened for O_WRONLY, don't read */ if ((pNetFd->options & FD_MODE) == O_WRONLY)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -