ItsTotallyRSX

Test FileIO

Jun 13th, 2018
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.57 KB | None | 0 0
  1. /*
  2. Purpose: Xenus NTOS emulator entrypoint.
  3. Author: Reece W.
  4. License: All Rights Reserved J. Reece Wilson
  5. */
  6.  
  7. #include <xenus_lazy.h>
  8. #include <liblinux.hpp>
  9.  
  10. #include <ITypes\ITaskStruct.hpp>
  11. #include <ITypes\IFile.hpp>
  12. #include <ITypes\IPath.hpp>
  13.  
  14. bool file_to_root(char * path, char * dest)
  15. {
  16. size_t i;
  17. size_t length;
  18.  
  19. length = strlen(path);
  20. memcpy(dest, path, length + 1);
  21.  
  22. for (i = length; i > 0; i--)
  23. {
  24. if (path[i] == '/')
  25. {
  26. *(char **)(&(dest[i + 1])) = (char *)0x00;
  27. break;
  28. }
  29. }
  30.  
  31. return i != 0;
  32. }
  33.  
  34. int unlink_file(const char * req_path)
  35. {
  36. char file_dir[256];
  37. file_k path_handle;
  38. file_k target_handle;
  39. inode_k delegated_inode;
  40.  
  41. if (!file_to_root((char *)req_path, file_dir))
  42. return 0; /* failed to strip path to dir */
  43.  
  44. if (!(target_handle = filp_open(req_path, O_RDONLY, 0600)))
  45. return -1; /* file not found */
  46.  
  47. if (!(path_handle = filp_open(file_dir, O_DIRECTORY, 0600)))
  48. return -2; /* dir not found. odd linking? device? who knows? */
  49.  
  50. //printf("%s is located in %s\n", req_path, file_dir);
  51.  
  52. IFile file = IFile(target_handle);
  53. IPath file_path = IPath(file.get_f_path());
  54. IFile path = IFile(path_handle);
  55.  
  56. vfs_unlink(path.get_f_inode(),
  57. file_path.get_dentry(),
  58. &delegated_inode);
  59.  
  60. filp_close(target_handle, 0);
  61. filp_close(path_handle, 0);
  62.  
  63. return 1;
  64. }
  65.  
  66. void test_write(const char * fn)
  67. {
  68. loff_t pos;
  69. file_k file;
  70. const char * string;
  71.  
  72. pos = 0;
  73. string = "Hello from Xenus!";
  74.  
  75. if (!(file = filp_open(fn, O_CREAT | O_RDWR, 0777)))
  76. {
  77. printf("Failed to open file %s \n", fn);
  78. return;
  79. }
  80.  
  81. kernel_write(file,
  82. (const void *)string,
  83. strlen(string),
  84. &pos);
  85.  
  86. filp_close(file, 0);
  87. }
  88.  
  89.  
  90. static mod_global_data_t ntos_module = { 0 };
  91.  
  92. bool subsystem_ntos_init(mod_dependency_list_p deps)
  93. {
  94. return true;
  95. }
  96.  
  97. int subsystem_ntos_start()
  98. {
  99. test_write("/Xenus/TestFile"); //create
  100. unlink_file("/Xenus/TestFile"); //then delete
  101. return 1;
  102. }
  103.  
  104. void subsystem_ntos_shutdown()
  105. {
  106.  
  107. }
  108.  
  109. void entrypoint(xenus_entrypoint_ctx_p context)
  110. {
  111. /* Do note, the IAT is still NULL'd at this moment in time! */
  112. context->size = sizeof(xenus_entrypoint_ctx_t);
  113. context->description = "An emulation attempt of the Microsoft Windows� Kernel (ntos.exe).";
  114. context->copyright = "All rights reserved, Reece Wilson (2018)";
  115. context->init = subsystem_ntos_init;
  116. context->start = subsystem_ntos_start;
  117. context->shutdown = subsystem_ntos_shutdown;
  118. context->dependencies.count = 0;
  119. context->static_data = &ntos_module;
  120. }
Add Comment
Please, Sign In to add comment