Advertisement
Guest User

Untitled

a guest
Sep 28th, 2019
276
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.47 KB | None | 0 0
  1. import com.github.lukethompsxn.edufuse.filesystem.FileSystemStub;
  2. import com.github.lukethompsxn.edufuse.struct.*;
  3. import com.github.lukethompsxn.edufuse.util.ErrorCodes;
  4. import com.github.lukethompsxn.edufuse.util.FuseFillDir;
  5. import jnr.ffi.Pointer;
  6. import jnr.ffi.Runtime;
  7. import jnr.ffi.types.dev_t;
  8. import jnr.ffi.types.mode_t;
  9. import jnr.ffi.types.off_t;
  10. import jnr.ffi.types.size_t;
  11. import util.MemoryINode;
  12. import util.MemoryINodeTable;
  13. import util.MemoryVisualiser;
  14.  
  15. import java.io.File;
  16. import java.io.IOException;
  17.  
  18. import java.util.Objects;
  19.  
  20. import com.sun.security.auth.module.UnixSystem;
  21.  
  22. /**
  23. * @author Luke Thompson and (Hong Shi)
  24. * @since 04.09.19
  25. */
  26. public class MemoryFS extends FileSystemStub {
  27. private static final String HELLO_PATH = "/hello";
  28. private static final String HELLO_STR = "Hello World!\n";
  29.  
  30. private MemoryINodeTable iNodeTable = new MemoryINodeTable();
  31. private MemoryVisualiser visualiser;
  32. private UnixSystem unix = new UnixSystem();
  33.  
  34. @Override
  35. public Pointer init(Pointer conn) {
  36.  
  37. // setup an example file for testing purposes
  38. MemoryINode iNode = new MemoryINode();
  39. FileStat stat = new FileStat(Runtime.getSystemRuntime());
  40. // you will have to add more stat information here eventually
  41. stat.st_mode.set(FileStat.S_IFREG | 0444 | 0200);
  42. stat.st_size.set(HELLO_STR.getBytes().length);
  43. iNode.setStat(stat);
  44. iNode.setContent(HELLO_STR.getBytes());
  45. iNodeTable.updateINode(HELLO_PATH, iNode);
  46. iNode.getStat().st_nlink.set(1);
  47. long time = System.currentTimeMillis();
  48. iNode.getStat().st_mtim.tv_nsec.set(time*1000000L);
  49. iNode.getStat().st_mtim.tv_sec.set(time/1000L);
  50. iNode.getStat().st_atim.tv_nsec.set(time*1000000L);
  51. iNode.getStat().st_atim.tv_sec.set(time/1000L);
  52. iNode.getStat().st_ctim.tv_nsec.set(time*1000000L);
  53. iNode.getStat().st_ctim.tv_sec.set(time/1000L);
  54.  
  55.  
  56. if (isVisualised()) {
  57. visualiser = new MemoryVisualiser();
  58. visualiser.sendINodeTable(iNodeTable);
  59. }
  60.  
  61. return conn;
  62. }
  63.  
  64. @Override
  65. public int getattr(String path, FileStat stat) {
  66. int res = 0;
  67. if (Objects.equals(path, "/")) { // minimal set up for the mount point root
  68. stat.st_mode.set(FileStat.S_IFDIR | 0755);
  69. stat.st_nlink.set(2);
  70. } else if (iNodeTable.containsINode(path)) {
  71. FileStat savedStat = iNodeTable.getINode(path).getStat();
  72. // fill in the stat object with values from the savedStat object of your inode
  73. stat.st_mode.set(savedStat.st_mode.intValue());
  74. stat.st_size.set(savedStat.st_size.intValue());
  75. stat.st_blksize.set(savedStat.st_blksize.intValue());
  76. stat.st_blocks.set(savedStat.st_blocks.byteValue());
  77. stat.st_rdev.set(savedStat.st_rdev.get());
  78. stat.st_gid.set(unix.getGid());
  79. stat.st_uid.set(unix.getUid());
  80. stat.st_nlink.set(savedStat.st_nlink.byteValue());
  81. stat.st_atim.tv_sec.set(savedStat.st_atim.tv_sec.byteValue());
  82. stat.st_atim.tv_nsec.set(savedStat.st_atim.tv_nsec.byteValue());
  83. stat.st_mtim.tv_sec.set(savedStat.st_atim.tv_sec.byteValue());
  84. stat.st_mtim.tv_nsec.set(savedStat.st_atim.tv_nsec.byteValue());
  85. stat.st_ctim.tv_sec.set(savedStat.st_atim.tv_sec.byteValue());
  86. stat.st_ctim.tv_nsec.set(savedStat.st_atim.tv_nsec.byteValue());
  87.  
  88. } else {
  89. res = -ErrorCodes.ENOENT();
  90. }
  91. return res;
  92. }
  93.  
  94. @Override
  95. public int readdir(String path, Pointer buf, FuseFillDir filler, @off_t long offset, FuseFileInfo fi) {
  96. // For each file in the directory call filler.apply.
  97. // The filler.apply method adds information on the files
  98. // in the directory, it has the following parameters:
  99. // buf - a pointer to a buffer for the directory entries
  100. // name - the file name (with no "/" at the beginning)
  101. // stbuf - the FileStat information for the file
  102. // off - just use 0
  103. filler.apply(buf, ".", null, 0);
  104. filler.apply(buf, "..", null, 0);
  105. // Loops through all the entries in the iNodeTable which correspond to the files in the current directory
  106. for (String s: iNodeTable.entries()) {
  107. filler.apply(buf,s.substring(1),iNodeTable.getINode(s).getStat(), 0);
  108. }
  109. return 0;
  110. }
  111.  
  112. @Override
  113. public int open(String path, FuseFileInfo fi) {
  114. return 0;
  115. }
  116.  
  117. @Override
  118. public int flush(String path, FuseFileInfo fi) {
  119. return 0;
  120. }
  121.  
  122. @Override
  123. public int read(String path, Pointer buf, @size_t long size, @off_t long offset, FuseFileInfo fi) {
  124. if (!iNodeTable.containsINode(path)) {
  125. return -ErrorCodes.ENOENT();
  126. }
  127. int amount = 0;
  128. // you need to extract data from the content field of the inode and place it in the buffer
  129. // something like:
  130. // buf.put(0, content, offset, amount);
  131. MemoryINode iNode = iNodeTable.getINode(path);
  132. byte[] bytes = iNode.getContent();
  133. amount = bytes.length;
  134. buf.put(offset, bytes,0,amount);
  135.  
  136.  
  137. if (isVisualised()) {
  138. visualiser.sendINodeTable(iNodeTable);
  139. }
  140. long time = System.currentTimeMillis();
  141. iNode.getStat().st_atim.tv_nsec.set(time*1000000L);
  142. iNode.getStat().st_atim.tv_sec.set(time/1000L);
  143. return amount;
  144. }
  145.  
  146. @Override
  147. public int write(String path, Pointer buf, @size_t long size, @off_t long offset, FuseFileInfo fi) {
  148. if (!iNodeTable.containsINode(path)) {
  149. return -ErrorCodes.ENOENT(); // ENONET();
  150. }
  151. // similar to read but you get data from the buffer like:
  152. // buf.get(0, content, offset, size);
  153. MemoryINode iNode = iNodeTable.getINode(path);
  154. byte[] bytes = iNode.getContent();
  155. int length = bytes.length;
  156. byte[] newContent = new byte[length + (int) size];
  157. System.arraycopy(bytes, 0, newContent, 0, bytes.length);
  158. buf.get(0,newContent, length, (int) size);
  159. iNode.setContent(newContent);
  160. iNode.getStat().st_size.set(length + size);
  161. long time = System.currentTimeMillis();
  162. // change time
  163. iNode.getStat().st_mtim.tv_nsec.set(time*1000000L);
  164. iNode.getStat().st_mtim.tv_sec.set(time/1000L);
  165. iNode.getStat().st_atim.tv_nsec.set(time*1000000L);
  166. iNode.getStat().st_atim.tv_sec.set(time/1000L);
  167.  
  168. if (isVisualised()) {
  169. visualiser.sendINodeTable(iNodeTable);
  170. }
  171. return (int) size;
  172. }
  173.  
  174. @Override
  175. public int mknod(String path, @mode_t long mode, @dev_t long rdev) {
  176. if (iNodeTable.containsINode(path)) {
  177. return -ErrorCodes.EEXIST();
  178. }
  179.  
  180. MemoryINode mockINode = new MemoryINode();
  181. // set up the stat information for this inode
  182. FileStat stat = new FileStat(Runtime.getSystemRuntime());
  183. stat.st_mode.set(FileStat.S_IFREG | 0444 | 0200);
  184. stat.st_size.set(mockINode.getContent().length);
  185. stat.st_nlink.set(1);
  186. long time = System.currentTimeMillis();
  187. stat.st_mtim.tv_nsec.set(time*1000000L);
  188. stat.st_mtim.tv_sec.set(time/1000L);
  189. stat.st_atim.tv_nsec.set(time*1000000L);
  190. stat.st_atim.tv_sec.set(time/1000L);
  191. stat.st_ctim.tv_nsec.set(time*1000000L);
  192. stat.st_ctim.tv_sec.set(time/1000L);
  193. mockINode.setStat(stat);
  194. iNodeTable.updateINode(path, mockINode);
  195.  
  196. if (isVisualised()) {
  197. visualiser.sendINodeTable(iNodeTable);
  198. }
  199.  
  200. return 0;
  201. }
  202.  
  203. @Override
  204. public int statfs(String path, Statvfs stbuf) {
  205. return super.statfs(path, stbuf);
  206. }
  207.  
  208. @Override
  209. public int utimens(String path, Timespec[] timespec) {
  210. // The Timespec array has the following information.
  211. // You need to set the corresponding fields of the inode's stat object.
  212. // You can access the data in the Timespec objects with "get()" and "longValue()".
  213. // You have to find out which time fields these correspond to.
  214. // timespec[0].tv_nsec.set(System.currentTimeMillis()*1000000L);
  215. // timespec[0].tv_sec.set(System.currentTimeMillis()/1000L);
  216. // timespec[1].tv_nsec.set(System.currentTimeMillis()*1000000L);
  217. // timespec[1].tv_sec.set(System.currentTimeMillis()/1000L);
  218. return 0;
  219. }
  220.  
  221. @Override
  222. public int link(java.lang.String oldpath, java.lang.String newpath) {
  223.  
  224. iNodeTable.updateINode(newpath, iNodeTable.getINode(oldpath));
  225. MemoryINode iNode = iNodeTable.getINode(oldpath);
  226. int links = iNode.getStat().st_nlink.intValue();
  227. links++;
  228. iNode.getStat().st_nlink.set(links);
  229. return 0;
  230. }
  231.  
  232. @Override
  233. public int unlink(String path) {
  234. if (!iNodeTable.containsINode(path)) {
  235. return -ErrorCodes.ENONET();
  236. }
  237. MemoryINode iNode = iNodeTable.getINode(path);
  238. int links = iNode.getStat().st_nlink.intValue();
  239. links --;
  240. if(links > 0){
  241. iNode.getStat().st_nlink.set(links);
  242. }
  243. iNodeTable.removeINode(path);
  244. if(isVisualised()){
  245. visualiser.sendINodeTable(iNodeTable);
  246. }
  247. // delete the file if there are no more hard links
  248. return 0;
  249. }
  250.  
  251. @Override
  252. public int mkdir(String path, long mode) {
  253. return 0;
  254. }
  255.  
  256. @Override
  257. public int rmdir(String path) {
  258. return 0;
  259. }
  260.  
  261. @Override
  262. public int rename(String oldpath, String newpath) {
  263. return 0;
  264. }
  265.  
  266. @Override
  267. public int truncate(String path, @size_t long size) {
  268. return 0;
  269. }
  270.  
  271. @Override
  272. public int release(String path, FuseFileInfo fi) {
  273. return 0;
  274. }
  275.  
  276. @Override
  277. public int fsync(String path, int isdatasync, FuseFileInfo fi) {
  278. return 0;
  279. }
  280.  
  281. @Override
  282. public int setxattr(String path, String name, Pointer value, @size_t long size, int flags) {
  283. return 0;
  284. }
  285.  
  286. @Override
  287. public int getxattr(String path, String name, Pointer value, @size_t long size) {
  288. return 0;
  289. }
  290.  
  291. @Override
  292. public int listxattr(String path, Pointer list, @size_t long size) {
  293. return 0;
  294. }
  295.  
  296. @Override
  297. public int removexattr(String path, String name) {
  298. return 0;
  299. }
  300.  
  301. @Override
  302. public int opendir(String path, FuseFileInfo fi) {
  303. return 0;
  304. }
  305.  
  306. @Override
  307. public int releasedir(String path, FuseFileInfo fi) {
  308. return 0;
  309. }
  310.  
  311. @Override
  312. public void destroy(Pointer initResult) {
  313. if (isVisualised()) {
  314. try {
  315. visualiser.stopConnection();
  316. } catch (IOException e) {
  317. e.printStackTrace();
  318. }
  319. }
  320. }
  321.  
  322. @Override
  323. public int access(String path, int mask) {
  324. return 0;
  325. }
  326.  
  327. @Override
  328. public int lock(String path, FuseFileInfo fi, int cmd, Flock flock) {
  329. return 0;
  330. }
  331.  
  332. public static void main(String[] args) {
  333. MemoryFS fs = new MemoryFS();
  334. try {
  335. fs.mount(args, true);
  336. } finally {
  337. fs.unmount();
  338. }
  339. }
  340. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement