package NFS;
import java.io.*;
class NFSDir implements NFSConsts {
Handle handles;
PathMapper pm;
TimeMapper tm;
FileSystemInfo fsinfo;
NFSDir(Handle h, PathMapper p, TimeMapper t, FileSystemInfo f) {
handles = h;
pm = p;
tm = t;
fsinfo = f;
}
XDRPacket GetAttr(long xid, XDRPacket packet) throws NFSException {
try {
// get info out of the packet
fhandle f = new fhandle(packet);
// carry out the operation
fattr fa = new fattr(handles, tm);
String file = GetNameFromHandle(f.Handle(), xid);
// make sure the file exists
File fd = new File(file);
if (fd.exists() == false)
throw new NFSException(xid, NFSERR_NOENT);
// System.out.print("getattr on " + file + "\n");
fa.Load(file);
// make the reply packet
XDRPacket reply = new XDRPacket(128);
reply.AddReplyHeader(xid);
reply.AddLong(NFS_OK);
fa.Emit(reply);
return reply;
} catch (FileNotFoundException e) {
throw new NFSException(xid, NFSERR_NOENT);
}
}
XDRPacket SetAttr(long xid, XDRPacket packet) throws NFSException {
try {
// get info out of the packet
fhandle f = new fhandle(packet);
String fileName = GetNameFromHandle(f.Handle(), xid);
// the attributes
int mode = (int) packet.GetLong();
int uid = (int) packet.GetLong();
int gid = (int) packet.GetLong();
int size = (int) packet.GetLong();
timeval atime = new timeval(packet);
timeval mtime = new timeval(packet);
// do the work - the only attribute that can be set is the size can
// be set to 0 to truncate the file
if (size == 0) {
// truncate by deleting and recreating the file
File fd = new File(fileName);
fd.delete();
RandomAccessFile ra = new RandomAccessFile(fileName, "rw");
ra.close();
}
// make the reply
XDRPacket reply = new XDRPacket(128);
reply.AddReplyHeader(xid);
reply.AddLong(NFS_OK);
fattr fa = new fattr(handles, tm);
fa.Load(fileName);
fa.Emit(reply);
return reply;
} catch (FileNotFoundException e) {
throw new NFSException(xid, NFSERR_NOENT);
} catch (IOException e) {
throw new NFSException(xid, NFSERR_PERM);
}
}
XDRPacket Lookup(long xid, XDRPacket packet) throws NFSException {
try {
fhandle dir = new fhandle(packet);
String entry = packet.GetString();
// figure out the file name being requested and make a handle for it
// if it exists.
String dirName = GetNameFromHandle(dir.Handle(), xid);
String fileName = pm.MakePath(dirName, entry);
File fd = new File(fileName); // open it to make sure it exists
if (fd.exists() != true)
throw new NFSException(xid, NFSERR_NOENT);
fattr childFA = new fattr(handles, tm);
childFA.Load(fileName);
// make a fhandle for this new path
long childHandle = handles.Allocate(fileName);
fhandle childFH = new fhandle();
childFH.Set(dir.Root(), childHandle, dir.ReadOnly());
// make the reply
XDRPacket reply = new XDRPacket(128);
reply.AddReplyHeader(xid);
reply.AddLong(NFS_OK);
childFH.Emit(reply);
childFA.Emit(reply);
return reply;
} catch(FileNotFoundException e) {
throw new NFSException(xid, NFSERR_NOENT);
}
}
// keep these between calls so subsequent calls getting the rest of the
// contents of a directory are fast.
String cachedDirName;
String [] cachedFiles;
XDRPacket Readdir(long xid, XDRPacket packet) throws NFSException {
fhandle fh = new fhandle(packet);
long cookie = packet.GetLong();
long count = packet.GetLong();
// if this is a new call to readdir (cookie=0) or it is a new
// directory to read, replace the cache.
String dirName = GetNameFromHandle(fh.Handle(), xid);
System.out.print("Reading dir " + dirName + " cookie=" + cookie
+ " count=" + count + "\n");
if (cookie == 0 || (dirName.equals(cachedDirName) == false)) {
File dirfd = new File(dirName);
if (dirfd == null)
throw new NFSException(xid, NFSERR_NOENT);
String [] dirfiles = dirfd.list();
if (dirfiles == null)
throw new NFSException(xid, NFSERR_NOENT);
System.out.println("dir has " + dirfiles.length + " entries");
if (dirfiles.length <= 0)
throw new NFSException(xid, NFSERR_NOENT);
// sort the files by name
Sort s = new Sort();
// turns out my bubblesort implementation is faster than my
// qsort for the numbers of files that listdir gets
s.BubbleSort(dirfiles);
// make a new list that contains the old list plus . and ..
String [] files = new String[dirfiles.length + 2];
files[0] = new String(".");
files[1] = new String("..");
for (int i = 0; i < dirfiles.length; i++)
files[i + 2] = dirfiles[i];
cachedFiles = files;
cachedDirName = dirName;
}
// prepare the reply packet.
XDRPacket reply = new XDRPacket((int) count);
reply.AddReplyHeader(xid);
reply.AddLong(NFS_OK);
// Add files to the list until there are no more files or all of
// the count bytes have been used.
int current = reply.Length();
boolean more = false; // are there more files to get
// if there are any files to add
if (cachedFiles != null && cachedFiles.length > 0) {
for (int i = (int) cookie; i < cachedFiles.length; i++) {
// see if there is enough room for another file - 3 longs of id,
// the name (rounded up to 4 bytes) and a trailing long
// indicating whether there are more files to get
int needed = 3 * 4 + (cachedFiles[i].length() + 3) + 8;
if (needed + current >= count) {
more = true;
break;
}
// get the handle for this file
String fileName = pm.MakePath(cachedDirName, cachedFiles[i]);
long handle = handles.Allocate(fileName);
// add an entry to the packet for this file
reply.AddLong(NFS_TRUE);
reply.AddLong(handle);
reply.AddString(cachedFiles[i]);
reply.AddLong(i + 1); // this is the cookie
current = reply.Length();
}
}
reply.AddLong(NFS_FALSE); // no more entries in this packet
// tell the client if this packet has returned the last of the files
if (more)
reply.AddLong(NFS_FALSE);
else
reply.AddLong(NFS_TRUE);
return reply;
}
XDRPacket Create(long xid, XDRPacket packet) throws NFSException {
try {
fhandle dirFH = new fhandle(packet);
String entry = packet.GetString();
String dirName = GetNameFromHandle(dirFH.Handle(), xid);
String path = pm.MakePath(dirName, entry);
// make the file
File fd = new File(path);
if (fd.exists())
throw new NFSException(xid, NFSERR_EXIST);
RandomAccessFile ra = new RandomAccessFile(path, "rw");
ra.close();
// make a new handle for this file
fhandle fh = new fhandle();
long handle = handles.Allocate(path);
fh.Set(dirFH.Root(), handle, dirFH.ReadOnly());
// get the attributes of this new file
fattr fa = new fattr(handles, tm);
fa.Load(path);
// create the reply packet
XDRPacket reply = new XDRPacket(128);
reply.AddReplyHeader(xid);
reply.AddLong(NFS_OK);
fh.Emit(reply);
fa.Emit(reply);
return reply;
} catch (FileNotFoundException e) {
throw new NFSException(xid, NFSERR_IO);
} catch (IOException e) {
throw new NFSException(xid, NFSERR_IO);
} catch (SecurityException e) {
throw new NFSException(xid, NFSERR_PERM);
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
NFS.zip_NFS_java nfs_nfs java_nfs-kernel-server
共89个文件
class:45个
java:40个
copyright:1个
1.该资源内容由用户上传,如若侵权请联系客服进行举报
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
版权申诉
0 下载量 21 浏览量
2022-09-24
13:56:27
上传
评论
收藏 95KB ZIP 举报
温馨提示
NFS服务.该服务与FTP类似,全称为Network File System(网络文件系统)
资源详情
资源评论
资源推荐
收起资源包目录
NFS.zip (89个子文件)
www.pudn.com.txt 218B
NFS
NFSItem.java 299B
XIDCache$XIDCacheItem.class 1KB
MTListItem.class 685B
PacketCollector.class 2KB
Handle.class 3KB
NFSException.java 385B
portmap.java 478B
ntnfs.java 1KB
MTList.class 1KB
FileSystemInfo.class 796B
rpcManager.java 6KB
Exports.class 5KB
portmap.class 841B
rpcHandler.class 827B
UnixPathMapper.java 741B
portmapMapping.class 1KB
XDRPacketCache.java 2KB
RPCConsts.class 503B
NFSException.class 593B
nfs.class 4KB
MountdConsts.java 282B
NFSDir.java 12KB
fattr.class 4KB
PacketCollector.java 2KB
fattr.java 5KB
UnixPathMapper.class 1KB
TimeMapper.java 209B
NFSHandler.class 4KB
Exports.java 6KB
PathMapper.class 265B
XDRPacket.java 3KB
ExportsItem.class 985B
MountdConsts.class 294B
NFSIO.java 4KB
MountdHandler.java 4KB
Java3TimeMapper.class 477B
PortmapRegister.class 1KB
NFSDir.class 8KB
TimeMapper.class 451B
NTTimeMapper.class 566B
NFSConsts.class 2KB
PortMapConst.java 1KB
portMapHandler.java 8KB
ntnfs.class 826B
XIDCache.class 4KB
NFSConsts.java 2KB
UDPPacketPort.class 2KB
UnixPermissions.class 770B
nfs.java 5KB
NFSOperation.java 553B
unixnfs.java 1KB
NTTimeMapper.java 601B
XDRPacketCache.class 2KB
XDRPacket.class 4KB
NFSItem.class 566B
BackupFile.java 3KB
PortMapConst.class 673B
unixnfs.class 849B
RPCConsts.java 587B
NTPathMapper.class 3KB
fhandle.java 2KB
FileSystemInfo.java 609B
MountdHandler.class 4KB
UnixPermissions.java 1KB
rpcHandler.java 594B
timeval.java 994B
Copyright 698B
Java3TimeMapper.java 1KB
PathMapper.java 246B
Handle.java 2KB
NFSMTHandler.java 772B
Makefile 2KB
NFSHandler.java 5KB
exports 177B
NFSMTHandler.class 1KB
XIDCache.java 4KB
NFSIO.class 4KB
NTPathMapper.java 4KB
rpcManager.class 5KB
fhandle.class 2KB
NFSOperation.class 933B
portMapHandler.class 5KB
Sort.java 4KB
UDPPacketPort.java 2KB
BackupFile.class 3KB
MTList.java 981B
Sort.class 3KB
timeval.class 2KB
共 89 条
- 1
小贝德罗
- 粉丝: 69
- 资源: 1万+
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
评论0