dnix-traverse.cpp 8.5 KB

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