📄 nfslib.c
字号:
if ((newDirName = (char *) alloca (nfsMaxPath)) == NULL) return (ERROR); if ((oldDirName = (char *) alloca (nfsMaxPath)) == NULL) return (ERROR); bzero ((char *) &dirResults, sizeof (dirResults)); bzero ((char *) &status, sizeof (status)); pathSplit (oldName, oldDirName, oldFileName); pathSplit (newName, newDirName, newFileName); bcopy ((char *) pOldDirHandle, (char *) &renameArgs.from.dir, sizeof (nfs_fh)); renameArgs.from.name = oldFileName; if (newDirName [0] == EOS) { /* NULL directory name */ bcopy ((char *) pMountHandle, (char *) &renameArgs.to.dir, sizeof (nfs_fh)); } else { if (nfsLookUpByName (hostName, newDirName, pMountHandle, &dirResults, &dummyDirHandle) != OK) return (ERROR); bcopy ((char *) &dirResults.diropres_u.diropres.file, (char *) &renameArgs.to.dir, sizeof (nfs_fh)); } renameArgs.to.name = newFileName; if (nfsClientCall (hostName, NFS_PROGRAM, NFS_VERSION, NFSPROC_RENAME, xdr_renameargs, (char *) &renameArgs, xdr_nfsstat, (char *) &status) == ERROR) { return (ERROR); } if (status != NFS_OK) { nfsErrnoSet (status); return (ERROR); } return (OK); }/********************************************************************************* nfsThingCreate - creates file or directory** RETURNS: OK | ERROR | FOLLOW_LINK,* if FOLLOW_LINK, then fullFileName contains the name of the link** NOMANUAL*/int nfsThingCreate ( char * hostName, char * fullFileName, /* name of thing being created */ nfs_fh * pMountHandle, /* mount device's file handle */ diropres * pDirOpRes, /* ptr to returned direc. operation results */ nfs_fh * pDirHandle, /* ptr to returned file's directory file hndl */ u_int mode /* mode that file or dir is created with * UNIX chmod style */ ) { FAST int status; char * newName; /* new name if name contained a link */ char * dirName; /* dir where file will be created */ char fileName[NAME_MAX + 1]; /* name of file without path */ char * tmpName; /* temporary file name */ FAST sattr *pSattr; /* ptr to attributes */ diropres lookUpResult; createargs createArgs; nfs_fh dummyDirHandle; u_int nfsProc; /* which nfs procedure to call, * depending on whether a file or * directory is made */ if (nfsInit () != OK) return (ERROR); if ((newName = (char *) alloca (nfsMaxPath)) == NULL) return (ERROR); if ((dirName = (char *) alloca (nfsMaxPath)) == NULL) return (ERROR); /* Extra 4 characters allocated for '/../' */ if ((tmpName = (char *) alloca (nfsMaxPath + 1 + 3)) == NULL) return (ERROR); bzero ((char *) &createArgs, sizeof (createArgs)); bzero ((char *) &lookUpResult, sizeof (lookUpResult)); bzero ((char *) pDirOpRes, sizeof (diropres)); pathSplit (fullFileName, dirName, fileName); status = nfsLookUpByName (hostName, dirName, pMountHandle, &lookUpResult, &dummyDirHandle); if (status == FOLLOW_LINK) { if ((strlen (dirName) + strlen (fileName) + 1) >= nfsMaxPath) { errnoSet (S_ioLib_NAME_TOO_LONG); status = ERROR; } else { pathCat (dirName, fileName, newName); (void) strcpy (fullFileName, newName); } } if (status != OK) return (status); bcopy ((char *) &lookUpResult.diropres_u.diropres.file, (char *) &createArgs.where.dir, sizeof (nfs_fh)); bcopy ((char *) &lookUpResult.diropres_u.diropres.file, (char *) pDirHandle, sizeof (nfs_fh)); createArgs.where.name = fileName; pSattr = &createArgs.attributes; if (mode == 0) /* create a file with default permissions */ pSattr->mode = NFS_FSTAT_REG | DEFAULT_FILE_PERM; else pSattr->mode = mode; /* don't set these attributes */ pSattr->uid = pSattr->gid = pSattr->size = -1; /* user ID */ pSattr->atime.seconds = pSattr->atime.useconds = -1; pSattr->mtime.seconds = pSattr->mtime.useconds = -1; if (mode & NFS_FSTAT_DIR) nfsProc = NFSPROC_MKDIR; else nfsProc = NFSPROC_CREATE; if (nfsClientCall (hostName, NFS_PROGRAM, NFS_VERSION, nfsProc, xdr_createargs, (char *) &createArgs, xdr_diropres, (char *) pDirOpRes) == ERROR) { return (ERROR); } /* * If the file created is really a symlink, then change the name and * return FOLLOW_LINK */ if (pDirOpRes->diropres_u.diropres.attributes.type == NFLNK) { strcpy (tmpName, fullFileName); /* Make a copy of fullFileName to * use with nfsLinkGet */ nfsLinkGet (hostName, &pDirOpRes->diropres_u.diropres.file, tmpName); /* * Need to replace last element of fullFileName with return value from * nfsLinkGet. Add /../newFileName to fullFileName, call * pathCondense(). */ strcpy (fullFileName, tmpName); return (FOLLOW_LINK); } if (pDirOpRes->status != NFS_OK) { nfsErrnoSet (pDirOpRes->status); return (ERROR); } return (OK); }/********************************************************************************* nfsFileWrite - write to a file** Either all characters will be written, or there is an ERROR.** RETURNS: number of characters written | ERROR** NOMANUAL*/int nfsFileWrite ( char * hostName, nfs_fh * pHandle, /* file handle of file being written to */ unsigned offset, /* current byte offset in file */ unsigned count, /* number of bytes to write */ char * data, /* data to be written (up to NFS_MAXDATA) */ fattr * pNewAttr ) { FAST unsigned nWrite = 0; /* bytes written in one nfs write */ FAST unsigned nTotalWrite = 0; /* bytes written in all writes together */ writeargs writeArgs; /* arguments to pass to nfs server */ attrstat writeResult; /* info returned from nfs server */ if (nfsInit () != OK) return (ERROR); bzero ((char *) &writeArgs, sizeof (writeArgs)); bcopy ((char *) pHandle, (char *) &writeArgs.file, sizeof (nfs_fh)); /* set offset into file where write starts */ writeArgs.offset = offset; while (nTotalWrite < count) { /* can only write up to NFS_MAXDATA bytes in one nfs write */ if (count - nTotalWrite > nfsMaxMsgLen) nWrite = nfsMaxMsgLen; else nWrite = count - nTotalWrite; writeArgs.totalcount = nWrite; writeArgs.data.data_len = nWrite; writeArgs.data.data_val = data + nTotalWrite; writeArgs.offset = offset + nTotalWrite; bzero ((char *) &writeResult, sizeof (attrstat)); if (nfsClientCall (hostName, NFS_PROGRAM, NFS_VERSION, NFSPROC_WRITE, xdr_writeargs, (char *) &writeArgs, xdr_attrstat, (char *) &writeResult) == ERROR) { return (ERROR); /* XXX if some writes have been done and there was an error, * nTotalWrite should be returned */ } if (writeResult.status != NFS_OK) { nfsErrnoSet (writeResult.status); return (ERROR); } nTotalWrite += nWrite; } bcopy ((char *) &writeResult.attrstat_u.attributes, (char *) pNewAttr, sizeof (fattr)); /* if nfs write returned ok, then all bytes were written */ return (count); }/********************************************************************************* nfsFileRead - read from a file** Reads as many bytes as are requested if that many bytes are left before* the end of file.** RETURNS: number of characters read or ERROR** NOMANUAL*/int nfsFileRead ( char * hostName, nfs_fh * pHandle, /* file handle of file being read */ unsigned offset, /* start at offset bytes from beg. of file */ unsigned count, /* number bytes to read up to */ char * buf, /* buffer that data is read into */ fattr * pNewAttr /* arguments returned from server */ ) { FAST unsigned nRead = 0; /* number of bytes read in one read */ FAST unsigned nTotalRead = 0; /* bytes read in all reads together */ readargs readArgs; /* arguments to pass to nfs server */ readres readReply; if (nfsInit () != OK) return (ERROR); bzero ((char *) &readArgs, sizeof (readArgs)); bcopy ((char *) pHandle, (char *) &readArgs.file, sizeof (nfs_fh)); while (nTotalRead < count) { if (count - nTotalRead > nfsMaxMsgLen) readArgs.count = nfsMaxMsgLen; else readArgs.count = count - nTotalRead; readArgs.offset = offset + nTotalRead; bzero ((char *) &readReply, sizeof (readReply)); /* make xdr buffer point to caller's buffer */ readReply.readres_u.reply.data.data_val = buf + nTotalRead; if (nfsClientCall (hostName, NFS_PROGRAM, NFS_VERSION, NFSPROC_READ, xdr_readargs, (char *) &readArgs, xdr_readres, (char *) &readReply) == ERROR) { return (ERROR); } if (readReply.status != NFS_OK) { nfsErrnoSet (readReply.status); return (ERROR); } /* check for end of file (data_len == 0) */ if ((nRead = readReply.readres_u.reply.data.data_len) == 0) break; /* update attributes */ bcopy ((char *) &(readReply.readres_u.reply.attributes), (char *) pNewAttr, sizeof (fattr)); nTotalRead += nRead; } /* while not all bytes requested have been read */ return (nTotalRead); }/********************************************************************************* nfsLinkGet - gets name of real file from a symbolic link** RETURNS: OK or ERROR*/LOCAL STATUS nfsLinkGet ( char * hostName, nfs_fh * pHandle, /* file handle of file being read */ nfspath realPath /* the actual pathname gets returned here */ ) { readlinkres pathStatus; if (nfsInit () != OK) return (ERROR); bzero ((char *) &pathStatus, sizeof (readlinkres)); /* make xdr buffer point to caller's buffer */ pathStatus.readlinkres_u.data = realPath; if (nfsClientCall (hostName, NFS_PROGRAM, NFS_VERSION, NFSPROC_READLINK, xdr_nfs_fh, (char *) pHandle, xdr_readlinkres, (char *) &pathStatus) == ERROR) { return (ERROR); } if (pathStatus.status != NFS_OK) { nfsErrnoSet (pathStatus.status); return (ERROR); } return (OK); }#if 0/********************************************************************************* nfsDirRead - read entries from a directory** RETURNS: OK or ERROR*/LOCAL STATUS nfsDirRead ( char * hostName, nfs_fh * pDirHandle, nfscookie cookie, /* hand this cookie to the server. The cookie * marks a place in the directory where entries * are to be read from */ unsigned count, /* number of bytes that nfs may return */ readdirres *pReadDirRes /* return the results of directory read here */ ) { readdirargs readDirArgs; if (nfsInit () != OK) return (ERROR); bzero ((char *) &readDirArgs, sizeof (readdirargs)); bcopy ((char *) pDirHandle, (char *) &readDirArgs.dir, sizeof (nfs_fh)); bcopy (cookie, readDirArgs.cookie, sizeof (nfscookie)); readDirArgs.count = count; bzero ((char *) pReadDirRes, sizeof (*pReadDirRes)); if (nfsClientCall (hostName, NFS_PROGRAM, NFS_VERSION, NFSPROC_READDIR, xdr_readdirargs, (char *) &readDirArgs, xdr_readdirres, (char *) pReadDirRes) == ERROR) { return (ERROR); } if (pReadDirRes->status != NFS_OK) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -