dnix-traverse.cpp 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  1. /*
  2. compile using : c++ dnix-traverse.cpp -o dnix-traverse
  3. Program to list and extract files from a DNIX FS. Should also be able to write to a DNIX file system. Possibly even add boot code and other things.
  4. */
  5. #define _POSIX_SOURCE 1
  6. #include <stdio.h>
  7. #include <unistd.h>
  8. #include <stdlib.h>
  9. #include <stdio.h>
  10. #include <time.h>
  11. #include <string.h>
  12. #include <limits.h> /* PATH_MAX */
  13. #include <errno.h>
  14. typedef unsigned int dnix_daddr_t;
  15. typedef int dnix_time_t;
  16. typedef int dnix_ino_t;
  17. #pragma pack(1)
  18. #define daddr_t dnix_daddr_t
  19. #define time_t dnix_time_t
  20. #define ino_t dnix_ino_t
  21. #include "../dnix-headers/diskpar.h"
  22. #include "../dnix-headers/inode.h"
  23. #include "../dnix-headers/sysfile.h"
  24. #include "../dnix-headers/dir.h"
  25. #undef ino_t
  26. #undef time_t
  27. #undef daddr_t
  28. #define time_t time_t
  29. #include <sys/stat.h> /* mkdir(2) */
  30. #include <utime.h>
  31. #define swap32(num) (((num>>24)&0xff) | ((num<<8)&0xff0000) | ((num>>8)&0xff00) | ((num<<24)&0xff000000))
  32. #define swap16(num) (((num >> 8) & 0xff) | ((num << 8) & 0xff00))
  33. class DnixFs {
  34. struct sysfptr dnixPartitionInfo;
  35. struct sysfile sysfile;
  36. FILE * image;
  37. public:
  38. DnixFs();
  39. void init( FILE * image );
  40. bool readInode (int inumber, struct dinode * inode);
  41. };
  42. bool DnixFs:: readInode (int inumber, struct dinode * inode) {
  43. struct dinode dinode;
  44. char buffer[26];
  45. struct tm * tm_info;
  46. time_t tim;
  47. int i;
  48. if (((inumber-1) * (sizeof dinode) + sysfile.s_inadr) > sysfile.s_insiz) {
  49. return false;
  50. }
  51. fseek (image, (inumber-1) * (sizeof dinode) + sysfile.s_inadr, SEEK_SET);
  52. fread ( (void * ) &dinode, sizeof dinode, 1, image);
  53. inode->di_mode = swap16(dinode.di_mode);
  54. inode->di_nlink = swap16(dinode.di_nlink);
  55. inode->di_uid = swap16(dinode.di_uid);
  56. inode->di_gid = swap16(dinode.di_gid);
  57. inode->di_size = swap32(dinode.di_size);
  58. inode->di_atime = swap32(dinode.di_atime);
  59. inode->di_mtime = swap32(dinode.di_mtime);
  60. inode->di_ctime = swap32(dinode.di_ctime);
  61. memcpy((void *) inode->di_addr, (void *) dinode.di_addr, 40);
  62. tim = inode->di_atime;
  63. tm_info = localtime((time_t *) ( &tim));
  64. strftime(buffer, 26, "%Y-%m-%d %H:%M:%S", tm_info);
  65. tim = inode->di_mtime;
  66. tm_info = localtime((time_t *) ( &tim));
  67. strftime(buffer, 26, "%Y-%m-%d %H:%M:%S", tm_info);
  68. tim = inode->di_ctime;
  69. tm_info = localtime((time_t *) ( &tim));
  70. strftime(buffer, 26, "%Y-%m-%d %H:%M:%S", tm_info);
  71. return true;
  72. }
  73. void DnixFs::init(FILE * img) {
  74. char buffer[26];
  75. struct tm * tm_info;
  76. daddr_t superblock_address;
  77. struct sysfile s;
  78. time_t tim;
  79. image = img;
  80. // Read the partition info
  81. fseek (image, SYSFPTR, SEEK_SET);
  82. fread ( (void * ) &dnixPartitionInfo, 512, 1, image);
  83. tim = swap32(dnixPartitionInfo.timestamp);
  84. tm_info = localtime((time_t *) ( &tim));
  85. strftime(buffer, 26, "%Y-%m-%d %H:%M:%S", tm_info);
  86. superblock_address = swap32(dnixPartitionInfo.vdsp);
  87. fseek (image, superblock_address, SEEK_SET);
  88. fread ( (void *) &s, sizeof sysfile, 1, image);
  89. memcpy ( (void *) &sysfile, (void *) &s, sizeof sysfile);
  90. sysfile.s_bmadr = swap32(s.s_bmadr);
  91. sysfile.s_inadr = swap32(s.s_inadr);
  92. sysfile.s_rtadr = swap32(s.s_rtadr);
  93. sysfile.s_swadr = swap32(s.s_swadr);
  94. sysfile.s_vlsiz = swap32(s.s_vlsiz);
  95. sysfile.s_bmsiz = swap32(s.s_bmsiz);
  96. sysfile.s_swsiz = swap32(s.s_swsiz);
  97. sysfile.s_bksiz = swap32(s.s_bksiz);
  98. sysfile.s_disiz = swap32(s.s_disiz);
  99. sysfile.s_insiz = swap32(s.s_insiz);
  100. sysfile.s_time = swap32(s.s_time);
  101. tim = sysfile.s_time;
  102. tm_info = localtime((time_t *) ( &tim));
  103. strftime(buffer, 26, "%Y-%m-%d %H:%M:%S", tm_info);
  104. }
  105. // Read a file - block number as specified - store in buf
  106. struct BlockCache {
  107. int disk_address;
  108. int block[512];
  109. };
  110. class DnixFile {
  111. struct dinode ino;
  112. FILE * image;
  113. struct BlockCache blockCache [3];
  114. void updateBlockCache(long disk_address, int level);
  115. public:
  116. void init( FILE * image, struct dinode * inode );
  117. void readFileBlock( int block_no, void * buf );
  118. };
  119. void DnixFile::updateBlockCache(long disk_address, int level) {
  120. if (disk_address != blockCache[level].disk_address) {
  121. fseek (image, disk_address, SEEK_SET);
  122. fread (blockCache[level].block, 2048, 1, image);
  123. }
  124. }
  125. void DnixFile::init(FILE * img, struct dinode * inode) {
  126. memcpy (&ino, inode, sizeof (struct dinode));
  127. image = img;
  128. }
  129. void DnixFile::readFileBlock ( int block_no, void * buf ) {
  130. long disk_address;
  131. if (block_no<11) {
  132. // direct blocks
  133. disk_address = (((0xff& ino.di_addr[block_no*3]) <<16 ) | ((0xff & ino.di_addr[block_no*3 + 1]) <<8 ) | (ino.di_addr[block_no*3 + 2] & 0xff)) <<8;
  134. } else if ((block_no > 10) && (block_no < 522)) {
  135. disk_address = (((0xff& ino.di_addr[30]) <<16 ) | ((0xff & ino.di_addr[31]) <<8 ) | (ino.di_addr[32] & 0xff))<<8;
  136. updateBlockCache(disk_address, 0);
  137. disk_address = swap32(blockCache[0].block[block_no-11]);
  138. } else if ((block_no >= 522) && (block_no < 262666)) {
  139. // Second level of indirect block
  140. disk_address = (((0xff& ino.di_addr[33]) <<16 ) | ((0xff & ino.di_addr[34]) <<8 ) | (ino.di_addr[35] & 0xff))<<8;
  141. updateBlockCache(disk_address, 1);
  142. disk_address = swap32(blockCache[1].block[(block_no-522)>>9]);
  143. updateBlockCache(disk_address, 0);
  144. disk_address = swap32(blockCache[0].block[(block_no-522) & 0x1ff]);
  145. } else {
  146. // Third level of indirect block
  147. disk_address = (((0xff& ino.di_addr[36]) <<16 ) | ((0xff & ino.di_addr[37]) <<8 ) | (ino.di_addr[38] & 0xff))<<8;
  148. updateBlockCache(disk_address, 2);
  149. disk_address = swap32(blockCache[2].block[(block_no-262666) >> 18]);
  150. updateBlockCache(disk_address, 1);
  151. disk_address = swap32(blockCache[1].block[((block_no-262666) >>9) & 0x1ff]);
  152. updateBlockCache(disk_address, 0);
  153. disk_address = swap32(blockCache[0].block[(block_no-262266) & 0x1ff]);
  154. }
  155. fseek (image, disk_address, SEEK_SET);
  156. fread (buf, 2048, 1, image);
  157. }
  158. DnixFs::DnixFs() {
  159. }
  160. int mkdir_p(const char *path)
  161. {
  162. /* Adapted from http://stackoverflow.com/a/2336245/119527 */
  163. const size_t len = strlen(path);
  164. char _path[PATH_MAX];
  165. char *p;
  166. errno = 0;
  167. /* Copy string so its mutable */
  168. if (len > sizeof(_path)-1) {
  169. errno = ENAMETOOLONG;
  170. return -1;
  171. }
  172. strcpy(_path, path);
  173. /* Iterate the string */
  174. for (p = _path + 1; *p; p++) {
  175. if (*p == '/') {
  176. /* Temporarily truncate */
  177. *p = '\0';
  178. if (mkdir(_path, S_IRWXU) != 0) {
  179. if (errno != EEXIST)
  180. return -1;
  181. }
  182. *p = '/';
  183. }
  184. }
  185. if (mkdir(_path, S_IRWXU) != 0) {
  186. if (errno != EEXIST)
  187. return -1;
  188. }
  189. return 0;
  190. }
  191. void readDir(int inumber, FILE * image_file, class DnixFs * dnixFs, char * path) {
  192. char p [1024];
  193. const char * slash = "/";
  194. struct direct * dir;
  195. class DnixFile * file = new class DnixFile;
  196. int size;
  197. FILE *output;
  198. int block_no=0;
  199. int dir_cnt;
  200. bool ret;
  201. struct utimbuf timebuf;
  202. struct dinode inode;
  203. ret = dnixFs->readInode(inumber, &inode);
  204. if (!ret) return;
  205. dir = (struct direct *) malloc (2048);
  206. file->init(image_file, &inode);
  207. size = inode.di_size;
  208. if (inode.di_mode & 0x4000) {
  209. mkdir_p(path);
  210. block_no = 0;
  211. do {
  212. file->readFileBlock(block_no, (void * ) dir);
  213. dir_cnt=0;
  214. do {
  215. if (((strncmp(dir[dir_cnt].d_name,".",1)!=0) && (strncmp(dir[dir_cnt].d_name,"..",2)!=0)) || (strlen(dir[dir_cnt].d_name) > 2)) {
  216. memset(p,0,sizeof p);
  217. strcpy(p, path);
  218. strcat(p, slash);
  219. strncat(p, dir[dir_cnt].d_name,14);
  220. readDir(swap16(dir[dir_cnt].d_ino), image_file, dnixFs, p);
  221. }
  222. dir_cnt++;
  223. } while ((dir_cnt < 128) && (swap16(dir[dir_cnt].d_ino) != 0));
  224. size -= 2048;
  225. block_no ++;
  226. } while (size > 0 && swap16(dir[255].d_ino) !=0 );
  227. } else {
  228. // Ordinary file
  229. output = fopen (path, "w");
  230. do {
  231. file->readFileBlock(block_no, (void * ) dir);
  232. if (size >= 2048) {
  233. fwrite(dir, 1, 2048, output);
  234. } else {
  235. fwrite(dir, 1, size, output);
  236. }
  237. size -= 2048;
  238. block_no ++;
  239. } while (size > 0);
  240. fclose(output);
  241. }
  242. chmod (path, inode.di_mode & 07777);
  243. chown (path, inode.di_gid, inode.di_uid);
  244. timebuf.actime = inode.di_atime;
  245. timebuf.modtime = inode.di_mtime;
  246. utime(path, &timebuf);
  247. delete file;
  248. }
  249. int main (int argc, char ** argv) {
  250. FILE * image_file;
  251. int opt;
  252. class DnixFs dnixFs;
  253. while ((opt = getopt(argc, argv, "d:")) != -1) {
  254. switch (opt) {
  255. case 'd':
  256. image_file = fopen (optarg, "r");
  257. if (image_file == NULL) {
  258. perror ("Failure opening file.");
  259. exit(EXIT_FAILURE);
  260. }
  261. break;
  262. default: /* '?' */
  263. fprintf(stderr, "Usage: %s [-d disk-image-file]\n", argv[0]);
  264. exit(EXIT_FAILURE);
  265. }
  266. }
  267. dnixFs.init(image_file);
  268. readDir(INOROOT, image_file, &dnixFs, (char *) ".");
  269. }