📄 nfslib.c
字号:
** nfsExportRead - get list of a host's exported file systems** This routine gets the exported file systems of a specified host and the groups* that are allowed to mount them.** After calling this routine, the export list must be free'd by calling* nfsExportFree.** RETURNS: OK or ERROR.** NOMANUAL*/STATUS nfsExportRead ( char *hostName, /* host machine for which to show exports */ exports *pExports ) { bzero ((char *) pExports, sizeof (*pExports)); return (nfsClientCall (hostName, MOUNTPROG, MOUNTVERS, MOUNTPROC_EXPORT, xdr_void, (char *) NULL, xdr_exports, (char *) pExports)); }/********************************************************************************* nfsExportFree - get list of a host's exported file systems** This routine frees the list of exported file systems returned by* nfsExportRead. It must be called after calling nfsExportRead, if* nfsExportRead returns OK.** RETURNS: OK** NOMANUAL*/STATUS nfsExportFree ( exports *pExports ) { clntudp_freeres (taskRpcStatics->nfsClientCache->client, xdr_exports, (caddr_t) pExports); return (OK); }/********************************************************************************* nfsHelp - display the NFS help menu** This routine displays a summary of NFS facilities typically called from* the shell:* .CS* nfsHelp Print this list* netHelp Print general network help list* nfsMount "host","filesystem"[,"devname"] Create device with* file system/directory from host* nfsUnmount "devname" Remove an NFS device* nfsAuthUnixShow Print current UNIX authentication* nfsAuthUnixPrompt Prompt for UNIX authentication* nfsIdSet id Set user ID for UNIX authentication* nfsDevShow Print list of NFS devices* nfsExportShow "host" Print a list of NFS file systems which* are exported on the specified host* mkdir "dirname" Create directory* rm "file" Remove file** EXAMPLE: -> hostAdd "wrs", "90.0.0.2"* -> nfsMount "wrs","/disk0/path/mydir","/mydir/"* -> cd "/mydir/"* -> nfsAuthUnixPrompt /@ fill in user ID, etc. @/* -> ls /@ list /disk0/path/mydir @/* -> copy < foo /@ copy foo to standard out @/* -> ld < foo.o /@ load object module foo.o @/* -> nfsUnmount "/mydir/" /@ remove NFS device /mydir/ @/* .CE** RETURNS: N/A*/void nfsHelp (void) { static char *help_msg [] ={"nfsHelp Print this list","netHelp Print general network help list","nfsMount \"host\",\"filesystem\"[,\"devname\"] Create device with"," file system/directory from host","nfsUnmount \"devname\" Remove an NFS device","nfsAuthUnixShow Print current UNIX authentication","nfsAuthUnixPrompt Prompt for UNIX authentication","nfsIdSet id Set user ID for UNIX authentication","nfsDevShow Print list of NFS devices","nfsExportShow \"host\" Print a list of NFS file systems which"," are exported on the specified host","mkdir \"dirname\" Create directory","rm \"file\" Remove file","","EXAMPLE: -> hostAdd \"wrs\", \"90.0.0.2\""," -> nfsMount \"wrs\",\"/disk0/path/mydir\",\"/mydir/\""," -> cd \"/mydir/\""," -> nfsAuthUnixPrompt /* fill in user ID, etc. *"," -> ls /* list /disk0/path/mydir *"," -> copy < foo /* copy foo to standard out *"," -> ld < foo.o /* load object module foo.o *"," -> nfsUnmount \"/mydir/\" /* remove NFS device /mydir/ *",}; FAST int ix; for (ix = 0; ix < NELEMENTS(help_msg); ix++) printf ("%s\n", help_msg [ix]); printf ("\n"); }/********************************************************************************* nfsExportShow - display the exported file systems of a remote host** This routine displays the file systems of a specified host and the groups* that are allowed to mount them.** EXAMPLE* .CS* -> nfsExportShow "wrs"* /d0 staff* /d1 staff eng* /d2 eng* /d3* value = 0 = 0x0* .CE** RETURNS: OK or ERROR.*/STATUS nfsExportShow ( char *hostName /* host machine to show exports for */ ) { exports nfsExports; if (hostName == NULL) { errnoSet (S_hostLib_INVALID_PARAMETER); return (ERROR); } if (nfsExportRead (hostName, &nfsExports) != OK) return (ERROR); if (nfsExports) nfsExportPrint (nfsExports); return (nfsExportFree (&nfsExports)); }/********************************************************************************* nfsLookUpByName - looks up a remote file** This routine returns information on a given file.* It takes the host name, mount handle, and file name, and returns the* file handle of the file and the file's attributes in pDirOpRes, and* the file handle of the directory that the file resides in in pDirHandle.** If the file name (directory path or filename itself) contains a symbolic* link, this routine changes the file name to incorporate the name of the link.** RETURNS: OK | ERROR | FOLLOW_LINK,* if FOLLOW_LINK, then fileName contains the name of the link** NOMANUAL*/int nfsLookUpByName ( char *hostName, char *fileName, /* name is changed if symbolic link */ nfs_fh *pMountHandle, /* these args are returned to calling routine */ FAST diropres *pDirOpRes, /* pointer to directory operation results */ nfs_fh *pDirHandle /* pointer to file's directory file handle */ ) { diropargs file; int nameCount = 0; char *nameArray [MAX_DIRNAMES]; /* ptrs to individual dir/file names * in path name */ char *nameBuf; /* buffer for individual dir/file names */ char *newLinkName; /* new file name if file is symb link */ char *linkName; /* name of link file */ char *currentDir; /* current dir for the look up's * that have been done */ if (nfsInit () != OK) return (ERROR); if ((nameBuf = (char *) alloca (nfsMaxPath)) == NULL) return (ERROR); if ((newLinkName = (char *) alloca (nfsMaxPath)) == NULL) return (ERROR); if ((linkName = (char *) alloca (nfsMaxPath)) == NULL) return (ERROR); if ((currentDir = (char *) alloca (nfsMaxPath)) == NULL) return (ERROR); bzero ((char *) &file, sizeof (file)); /* parse file name, enter each directory name in the path into the array */ pathParse (fileName, nameArray, nameBuf); bcopy ((char *) pMountHandle, (char *) &file.dir, sizeof (nfs_fh)); bcopy ((char *) pMountHandle, (char *) pDirHandle, sizeof (nfs_fh)); /* start traversing file name */ while ((nameCount < MAX_DIRNAMES) && (nameArray [nameCount] != NULL)) { file.name = nameArray [nameCount]; if (nfsClientCall (hostName, NFS_PROGRAM, NFS_VERSION, NFSPROC_LOOKUP, xdr_diropargs, (char *) &file, xdr_diropres, (char *) pDirOpRes) == ERROR) { return (ERROR); } if (pDirOpRes->status != NFS_OK) { nfsErrnoSet (pDirOpRes->status); return (ERROR); } if (pDirOpRes->diropres_u.diropres.attributes.type == NFDIR) { /* save directory information */ /* Store the directory handle of the second to last entry * in the path name array. This is the directory where the * file in question (or directory in question) lives. */ if ((nameCount < MAX_DIRNAMES - 1) && (nameArray [nameCount + 1] != NULL)) { bcopy ((char *) &pDirOpRes->diropres_u.diropres.file, (char *) pDirHandle, sizeof (nfs_fh)); } /* copy directory handle for the next nfsClientCall */ bcopy ((char *) &pDirOpRes->diropres_u.diropres.file, (char *) &file.dir, sizeof (nfs_fh)); } else if (pDirOpRes->diropres_u.diropres.attributes.type == NFLNK) { /* symbolic link, get real path name */ if (nfsLinkGet (hostName, &pDirOpRes->diropres_u.diropres.file, linkName) == ERROR) { return (ERROR); } /* Change fileName to include name of link. * Concatenate the directory name, link name, * and rest of the path name following the link. */ *currentDir = EOS; if ((pathBuild (nameArray, &(nameArray [nameCount]), currentDir) == ERROR) || (pathCat (currentDir, linkName, newLinkName) == ERROR) || (pathBuild (&(nameArray [++nameCount]), (char **) NULL, newLinkName) == ERROR)) { return (ERROR); } pathCondense (newLinkName); (void) strcpy (fileName, newLinkName); return (FOLLOW_LINK); } nameCount++; /* look up next directory */ } /* while */ return (OK); }/********************************************************************************* nfsFileRemove - remove a file** RETURNS: OK or ERROR** NOMANUAL*/STATUS nfsFileRemove ( char * hostName, nfs_fh * pMountHandle, /* file handle of mount device */ char * fullFileName ) { u_int nfsProc; diropres dirResults; diropargs where; nfsstat status; int retVal; char fileName [NAME_MAX + 1]; char * dirName; nfs_fh dirHandle; if (nfsInit () != OK) return (ERROR); if (strlen (fullFileName) >= nfsMaxPath) return (ERROR); if ((dirName = (char *) alloca (nfsMaxPath)) == NULL) return (ERROR); bzero ((char *) &dirResults, sizeof (dirResults)); bzero ((char *) &status, sizeof (status)); pathSplit (fullFileName, dirName, fileName); /* * get info on the file to be removed, such as the handle of its directory * and its file type (so we know whether to delete a file or directory) * * note: some file servers will indeed delete a file with the * NFSPROC_RMDIR command */ retVal = nfsLookUpByName (hostName, fullFileName, pMountHandle, &dirResults, &dirHandle); if (retVal == FOLLOW_LINK) { return (FOLLOW_LINK); } else if (retVal == ERROR) return (ERROR); bcopy ((char *) &dirHandle, (char *) &where.dir, sizeof (nfs_fh)); where.name = fileName; switch (dirResults.diropres_u.diropres.attributes.type) { case NFREG: /* remove regular file */ case NFLNK: /* remove symbolic link */ nfsProc = NFSPROC_REMOVE; break; case NFDIR: /* remove directory */ nfsProc = NFSPROC_RMDIR; break; default: errnoSet (S_nfsLib_NFS_INAPPLICABLE_FILE_TYPE); return (ERROR); } if (nfsClientCall (hostName, NFS_PROGRAM, NFS_VERSION, nfsProc, xdr_diropargs, (char *) &where, xdr_nfsstat, (char *) &status) == ERROR) { return (ERROR); } if (status != NFS_OK) { nfsErrnoSet (status); return (ERROR); } return (OK); }/********************************************************************************* nfsRename - rename a file** RETURNS: OK or ERROR** NOMANUAL*/STATUS nfsRename ( char * hostName, nfs_fh * pMountHandle, char * oldName, nfs_fh * pOldDirHandle, char * newName ) { diropres dirResults; renameargs renameArgs; nfsstat status; char newFileName [NAME_MAX + 1]; char oldFileName [NAME_MAX + 1]; char * newDirName; char * oldDirName; nfs_fh dummyDirHandle; if (nfsInit () != OK) return (ERROR); if ((strlen (oldName) >= nfsMaxPath) || (strlen (newName) >= nfsMaxPath)) { errnoSet (S_ioLib_NAME_TOO_LONG); return (ERROR); }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -