📄 netdrv.c
字号:
if (pNetDev->protocol == PROTO_FTP && ftpCommand (ctrlSock, "QUIT",0,0,0,0,0,0) != FTP_COMPLETE) { NETDRV_DEBUG ("netFileExists: error calling ftpCommand(\"QUIT\"). errno:0x%08x\n", errno,1,2,3,4,5); status = ERROR; } close (ctrlSock); KHEAP_FREE (pDirName); KHEAP_FREE (pFileName); NETDRV_DEBUG ("netFileExists: returning (%d)\n", status,1,2,3,4,5); 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[MAX_FILENAME_LENGTH]; char buffer [MAX_FILENAME_LENGTH]; 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) { NETDRV_DEBUG ("netGet: error calling ftpXfer(). errno:0x%08x\n", errno,1,2,3,4,5); return (ERROR); } } else { if (pathCat (pNetFd->remDirName, pNetFd->remFileName, buffer) == ERROR) { NETDRV_DEBUG ("netGet: error calling pathCat(). errno:0x%08x\n", errno,1,2,3,4,5); return (ERROR); } sprintf (command, "/bin/cat < %s", buffer); dataSock = rcmd (pNetFd->pNetDev->host, RSHD, usr, usr, command, &ctrlSock); if (dataSock == ERROR) { NETDRV_DEBUG ("netGet: error calling rcmd(). errno:0x%08x\n", errno,1,2,3,4,5); 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) { NETDRV_DEBUG ("netGet: error calling ftpCommand(\"QUIT\"). errno:0x%08x\n",\ errno,1,2,3,4,5); 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 & pNetFd->pNetDev->fdMode; /* 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) { NETDRV_DEBUG ("netGet: error calling netWrite(). errno:0x%08x\n", \ errno,1,2,3,4,5); status = ERROR; } } if (nBytes < 0) /* recv error */ status = ERROR; close (dataSock); if (pNetFd->pNetDev->protocol == PROTO_FTP) { if (ftpReplyGet (ctrlSock, FALSE) != FTP_COMPLETE) { NETDRV_DEBUG ("netGet: error calling ftpReplyGet(). errno:0x%08x\n", \ errno,1,2,3,4,5); 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) { NETDRV_DEBUG ("netGet: %s:%s . errno:0x%08x\n", pNetFd->pNetDev->host, buf, errno,1,2,3); /* 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) { NETDRV_DEBUG ("netGet: error calling ftpCommand(\"QUIT\"). errno:0x%08x\n", \ errno,1,2,3,4,5); 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[MAX_FILENAME_LENGTH]; char buffer[MAX_FILENAME_LENGTH]; 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) { NETDRV_DEBUG ("netPut: error calling ftpXfer() errno:0x%08x\n", \ errno,1,2,3,4,5); return (ERROR); } } else { if (pathCat (pNetFd->remDirName, pNetFd->remFileName, buffer) == ERROR) { NETDRV_DEBUG ("netPut: error calling pathCat(). errno:0x%08x\n", \ errno,1,2,3,4,5); return (ERROR); } sprintf (command, "/bin/cat > %s", buffer); dataSock = rcmd (pNetFd->pNetDev->host, RSHD, usr, usr, command, &ctrlSock); if (dataSock == ERROR) { NETDRV_DEBUG ("netPut: error calling rcmd(). errno:0x%08x\n", \ errno,1,2,3,4,5); 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) { NETDRV_DEBUG ("netPut: error calling ftpCommand(\"QUIT\"). errno:0x%08x\n",\ errno,1,2,3,4,5); 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 & pNetFd->pNetDev->fdMode; /* 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) { NETDRV_DEBUG ("netPut: error calling write(). errno:0x%08x\n", \ errno,1,2,3,4,5); status = ERROR; } } if (nBytes < 0) /* netRead error */ { NETDRV_DEBUG ("netPut: error calling netRead(). errno:0x%08x\n", errno,1,2,3,4,5); status = ERROR; } if (close (dataSock) == ERROR) { NETDRV_DEBUG ("netPut: error calling close(). errno:0x%08x\n", errno,1,2,3,4,5); status = ERROR; } if (pNetFd->pNetDev->protocol == PROTO_FTP) { if (ftpReplyGet (ctrlSock, FALSE) != FTP_COMPLETE) { NETDRV_DEBUG ("netPut: error calling ftpReplyGet(). errno:0x%08x\n", \ errno,1,2,3,4,5); status = ERROR; } } else { /* check control socket for error */ if ((nBytes = fioRead (ctrlSock, buf, sizeof (buf) - 1)) > 0) { /* print error message */ buf [nBytes] = EOS; /* insure string termination */ NETDRV_DEBUG ("netPut: %s:%s . errno:0x%08x\n", pNetFd->pNetDev->host, buf, errno,1,2,3); /* set the task's status according to the UNIX error */ getNetStatus (buf); NETDRV_DEBUG ("netPut: error calling fioRead(). errno:0x%08x\n", \ errno,1,2,3,4,5); status = ERROR; } } if (pNetFd->pNetDev->protocol == PROTO_FTP && ftpCommand (ctrlSock, "QUIT",0,0,0,0,0,0) != FTP_COMPLETE) { NETDRV_DEBUG ("netPut: error calling ftpCommand(). errno:0x%08x\n", \ errno,1,2,3,4,5); 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; pErr = (char *) index (buf, ':'); if (pErr == NULL) errno = S_netDrv_UNIX_FILE_ERROR; else if (strcmp (pErr, ": Permission denied\n") == 0) errno = S_netDrv_PERMISSION_DENIED; else if (strcmp (pErr, ": No such file or directory\n") == 0) errno = S_netDrv_NO_SUCH_FILE_OR_DIR; else if (strcmp (pErr, ": Is a directory\n") == 0) errno = S_netDrv_IS_A_DIRECTORY; else errno = 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 = OK; FAST int byteCount = 0; /* number of bytes read so far */ /* check for valid maxbytes */ if (maxBytes <= 0) { errno = S_netDrv_INVALID_NUMBER_OF_BYTES; return (ERROR); } /* if file opened for O_WRONLY, don't read */ if ((pNetFd->options & pNetFd->pNetDev->fdMode) == O_WRONLY) { errno = S_netDrv_WRITE_ONLY_CANNOT_READ; return (ERROR); }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -