📄 netdrv.c
字号:
* .CE** The file /usr/dog on the UNIX system "wrs" can now be accessed as* "wrs:/usr/dog" via RSH.** Before creating a device, the host must have already been created with* hostAdd().** RETURNS: OK or ERROR.** SEE ALSO: hostAdd()*/STATUS netDevCreate ( char *devName, /* name of device to create */ char *host, /* host this device will talk to */ int protocol /* remote file access protocol 0 = RSH, 1 = FTP */ ) { return (netDevCreate2 (devName, host, protocol, 0)); }/********************************************************************************* netDevCreate2 - create a remote file device with fixed buffer size** This routine creates a remote file device, just like netDevCreate(), but it allows* very large files to be accessed without loading the entire file to memory.* The fourth parameter <bufSize> specifies the amount of memory. If the <bufSize> is * zero, then the behavior is exactly the same as netDevCreate(). If <bufSize>* is not zero, the following restrictions apply:* * \ml* \m - O_RDONLY, O_WRONLY open modes are supported, but not O_RDWR open mode.* \m - seek is supported in O_RDONLY open mode, but not in O_WRONLY open mode.* \m - backward seek will be slow if it is beyond the buffer.* \me** RETURNS: OK or ERROR.** SEE ALSO: netDevCreate()*/STATUS netDevCreate2 ( char *devName, /* name of device to create */ char *host, /* host this device will talk to */ int protocol, /* remote file access protocol 0 = RSH, 1 = FTP */ UINT bufSize /* size of buffer in NET_FD */ ) { FAST NET_DEV *pNetDev; if (netDrvNum < 1) { errno = S_ioLib_NO_DRIVER; return (ERROR); } pNetDev = (NET_DEV *) KHEAP_ALLOC (sizeof (NET_DEV)); if (pNetDev == NULL) return (ERROR); (void) strcpy (pNetDev->host, host); pNetDev->protocol = protocol; pNetDev->bufSize = bufSize; /* if it is not zero, we use the buf */ if (bufSize == 0) pNetDev->fdMode = 3; /* O_RDONLY, O_WRONLY or O_RDWR */ else pNetDev->fdMode = 1; /* O_RDONLY, O_WRONLY */ if (iosDevAdd ((DEV_HDR *) pNetDev, devName, netDrvNum) != OK) { KHEAP_FREE ((char *) pNetDev); return ERROR; } return OK; }/* routines supplied to I/O system *//********************************************************************************* netCreate - create a remote file** Returns an open network file descriptor.** Called only by the I/O system.** RETURNS: Pointer to open network file descriptor or ERROR.*/LOCAL int netCreate ( NET_DEV *pNetDev, char *name, /* remote file name */ int mode /* ignored, always set to O_WRONLY */ ) { FAST NET_FD *pNetFd; pNetFd = (NET_FD *) netFdCreate (pNetDev, name, mode); if (pNetFd == (NET_FD *) ERROR) { return (ERROR); } if (netCreate2 (pNetFd, name, mode) == ERROR) { NETDRV_DEBUG ("netCreate: error calling netCreate2() errno:0x%08x\n", errno,1,2,3,4,5); return ERROR; } return ((int) pNetFd); }/********************************************************************************* netCreate2 - create a remote file with an existing net file descriptor** Returns an open network file descriptor.** Called only by netCreate and netOpen** RETURNS: Pointer to open network file descriptor or ERROR.*/LOCAL int netCreate2 ( NET_FD *pNetFd, char *name, /* remote file name */ int mode /* ignored, always set to O_WRONLY */ ) { if (pNetFd == (NET_FD *) ERROR) { NETDRV_DEBUG ("netCreate2: pNetFd == NULL errno:0x%08x\n", errno,1,2,3,4,5); return (ERROR); } if (pNetFd->pNetDev->bufSize > 0) { char command[MAX_FILENAME_LENGTH]; char buffer [MAX_FILENAME_LENGTH]; char usr [MAX_IDENTITY_LEN]; char passwd [MAX_IDENTITY_LEN]; /* open the remote file */ remCurIdGet (usr, passwd); if (pNetFd->pNetDev->protocol == PROTO_FTP) { if (ftpXfer (pNetFd->pNetDev->host, usr, passwd, "", "STOR %s", pNetFd->remDirName, pNetFd->remFileName, &pNetFd->ctrlSock, &pNetFd->dataSock) == ERROR) { NETDRV_DEBUG ("netCreate2: 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 ("netCreate2: error calling pathCat() errno:0x%08x\n", \ errno,1,2,3,4,5); return (ERROR); } sprintf (command, "/bin/cat > %s", buffer); pNetFd->dataSock = rcmd (pNetFd->pNetDev->host, RSHD, usr, usr, command, &pNetFd->ctrlSock); if (pNetFd->dataSock == ERROR) { NETDRV_DEBUG ("netCreate2: error calling rcmd() errno:0x%08x\n", \ errno,1,2,3,4,5); return (ERROR); } } } return ((int) pNetFd); }/********************************************************************************* netDelete - delete a file from a remote machine via the network.** The remote shell daemon on the machine 'host' is used to delete* the given file. The remote userId should have been set previously* by a call to iam().** RETURNS: OK or ERROR.*/LOCAL STATUS netDelete ( NET_DEV *pNetDev, char *name /* remote file name */ ) { int dataSock; int ctrlSock; char buf [DATASIZE]; /* make buf the same size as NET_FD's databuf */ FAST int nBytes; char command[MAX_FILENAME_LENGTH]; STATUS status = OK; char usr [MAX_IDENTITY_LEN]; char passwd [MAX_IDENTITY_LEN]; char *pDirName; char *pFileName; pDirName = (char *) KHEAP_ALLOC ((unsigned) (strlen (name) + 1)); pFileName = (char *) KHEAP_ALLOC ((unsigned) (strlen (name) + 1)); remCurIdGet (usr, passwd); /* get directory name and file name */ pathSplit (name, pDirName, pFileName); if (pNetDev->protocol == PROTO_FTP) { if (ftpXfer (pNetDev->host, usr, passwd, "", "DELE %s", pDirName, pFileName, &ctrlSock, (int *) NULL) == ERROR) { NETDRV_DEBUG ("netDelete: error calling ftpXfer() errno:0x%08x\n", \ errno,1,2,3,4,5); KHEAP_FREE (pDirName); KHEAP_FREE (pFileName); return (ERROR); } } else { sprintf (command, "/bin/rm %s", name); dataSock = rcmd (pNetDev->host, RSHD, usr, usr, command, &ctrlSock); if (dataSock == ERROR) { NETDRV_DEBUG ("netDelete: error calling rcmd() errno:0x%08x\n", \ errno,1,2,3,4,5); KHEAP_FREE (pDirName); KHEAP_FREE (pFileName); return (ERROR); } close (dataSock); /* check control socket for error */ if ((nBytes = fioRead (ctrlSock, buf, sizeof (buf) - 1)) > 0) { NETDRV_DEBUG ("netDelete: error fioread() errno:0x%08x\n", \ errno,1,2,3,4,5); /* print error message on standard error fd */ buf [nBytes] = EOS; /* insure string termination */ NETDRV_DEBUG ("%s:%s errno:0x%08x", pNetDev->host, buf, errno,3,4,5); /* set the task's status according to the UNIX error */ getNetStatus (buf); status = ERROR; } } if (pNetDev->protocol == PROTO_FTP && ftpCommand (ctrlSock, "QUIT",0,0,0,0,0,0) != FTP_COMPLETE) { NETDRV_DEBUG ( \ "netDelete: error calling ftpCommand(\"QUIT\") errno:0x%08x\n", \ errno,1,2,3,4,5); status = ERROR; } close (ctrlSock); KHEAP_FREE (pDirName); KHEAP_FREE (pFileName); return (status); }/********************************************************************************* netOpen - open a remote file** netOpen loads the remote file from the host into a network file descriptor.* If the file does not exist and the O_CREAT flag is not specifed, or the file* is not readable on the UNIX machine, then the UNIX error message is printed* on the standard error file descriptor and ERROR is returned.** Called only by the I/O system.** RETURNS: Pointer to open network file descriptor, or ERROR.*/LOCAL int netOpen ( FAST NET_DEV *pNetDev, /* pointer to network device */ char *name, /* remote file name to open */ int mode, /* O_RDONLY, O_WRONLY or O_RDWR, and O_CREAT */ int perm /* UNIX permissions, includes directory flag */ ) { FAST NET_FD *pNetFd; STATUS status; NETDRV_DEBUG ("**** netOpen: name:%s mode:0x%x perm:0x%x Entered ***\n", \ 1,2,3,4,5,6); pNetFd = (NET_FD *) netFdCreate (pNetDev, name, mode); if (pNetFd == (NET_FD *) ERROR) { NETDRV_DEBUG ("netOpen: error calling netFdCreate() errno:0x%08x\n", \ errno,1,2,3,4,5); return (ERROR); } /* Leave file at 0 length. */ if ((mode & O_TRUNC)) { return ((int) pNetFd); } if ((mode & O_CREAT)) { NETDRV_DEBUG ("**** netOpen: calling netFileExists ***\n", 1,2,3,4,5,6); } else { NETDRV_DEBUG ("**** O_CREATE NOT USED!!! ***\n", 1,2,3,4,5,6); } if (mode & O_CREAT) { if (netFileExists (pNetDev, name) == ERROR) { NETDRV_DEBUG ("netOpen: calling netCreate2 \n", 1,2,3,4,5,6); errno = 0; /* reset errno after netFileNetExists() */ if (netCreate2 (pNetFd, name, mode) == ERROR) { NETDRV_DEBUG ("**** netOpen: netCreate2 return error ***\n", 1,2,3,4,5,6); return (ERROR); } NETDRV_DEBUG ("netOpen: returning file descriptor pointer 0x%08x after creating file\n", pNetFd,1,2,3,4,5); return ((int) pNetFd); } else { NETDRV_DEBUG ("netOpen: netFileExists - returned OK - file exists\n", \ 1,2,3,4,5,6); } NETDRV_DEBUG ("netOpen: CREAT called, but file already exists\n", \ pNetFd,1,2,3,4,5); } semTake (pNetFd->netFdSem, WAIT_FOREVER); /* netFdCreate sets a O_RDONLY mode to O_RDWR and sets dirty bit, * set it back to original mode. */ pNetFd->options = mode & pNetFd->pNetDev->fdMode; if (pNetFd->pNetDev->bufSize == 0) { NETDRV_DEBUG ("netOpen: calling netGet() \n", 0,1,2,3,4,5); if (netGet (pNetFd) != OK) { /* not able to get file from host */ semGive (pNetFd->netFdSem); netFdRelease (pNetFd); NETDRV_DEBUG ("netOpen: netGet() return !OK - errno:0x%08x\n", \ errno,1,2,3,4,5); return (ERROR); } /* netGet might have moved the file pointer, so reset to beginning */ status = netSeek (pNetFd, 0); if (status == ERROR) { NETDRV_DEBUG ("netOpen: error calling netSeek() errno:0x%08x\n", \ errno,1,2,3,4,5); semGive (pNetFd->netFdSem);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -