dir.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  1. /*
  2. * Copyright (C) 2005 - Alejandro Liu Ly <alejandro_liu@hotmail.com>
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; either version 2 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  17. */
  18. /**
  19. * @project mfstool
  20. * @module dir
  21. * @section 3
  22. * @doc routines for reading directories
  23. */
  24. #include "minix_fs.h"
  25. #include "protos.h"
  26. #include <sys/stat.h>
  27. #include <string.h>
  28. #include <unistd.h>
  29. #include <time.h>
  30. /**
  31. * Print a directory entry
  32. * @param fp - file to write to (typically stdio)
  33. * @param dp - pointer to directory entry's name
  34. * @param namlen - length of file name
  35. */
  36. void outent(FILE *fp,const char *dp,int namlen) {
  37. while (*dp && namlen--) {
  38. putc(*dp++,fp);
  39. }
  40. putc('\n',fp);
  41. }
  42. /**
  43. * List contents of a directory
  44. * @param fs - filesystem structure
  45. * @param path - directory path to list
  46. */
  47. void dodir(struct minix_fs_dat *fs,const char *path) {
  48. int inode = find_inode(fs,path);
  49. int i,bsz,j;
  50. u8 blk[BLOCK_SIZE];
  51. int fdirsize;
  52. int dentsz = DIRSIZE(fs);
  53. if (inode == -1) fatalmsg("%s: not found",path);
  54. if (VERSION_2(fs)) {
  55. struct minix2_inode *ino = INODE2(fs,inode);
  56. if (!S_ISDIR(ino->i_mode)) fatalmsg("%s: is not a directory",path);
  57. fdirsize = ino->i_size;
  58. } else {
  59. struct minix_inode *ino = INODE(fs,inode);
  60. if (!S_ISDIR(ino->i_mode)) fatalmsg("%s: is not a directory",path);
  61. fdirsize = ino->i_size;
  62. }
  63. for (i = 0; i < fdirsize; i += BLOCK_SIZE) {
  64. bsz = read_inoblk(fs,inode,i / BLOCK_SIZE,blk);
  65. for (j = 0; j < bsz ; j+= dentsz) {
  66. u16 fino = *((u16 *)(blk+j));
  67. // printf("%d ",fino);
  68. if (fino == 0) continue;
  69. outent(stdout,blk+j+2,dentsz-2);
  70. }
  71. }
  72. }
  73. /**
  74. * Commant to list contents of a directory
  75. * @param fs - filesystem structure
  76. * @param argc - from command line
  77. * @param argv - from command line
  78. */
  79. void cmd_dir(struct minix_fs_dat *fs,int argc,char **argv) {
  80. if (argc == 1) {
  81. dodir(fs,".");
  82. } else if (argc == 2) {
  83. dodir(fs,argv[1]);
  84. } else {
  85. int i;
  86. for (i=1;i<argc;i++) {
  87. printf("%s:\n",argv[i]);
  88. dodir(fs,argv[i]);
  89. }
  90. }
  91. }
  92. /**
  93. * Create a directory
  94. * @param fs - filesystem structure
  95. * @param newdir - directory name
  96. */
  97. int domkdir(struct minix_fs_dat *fs,char *newdir) {
  98. int dinode;
  99. int ninode = make_node(fs,newdir,0755|S_IFDIR,dogetuid(),dogetgid(), 0,
  100. NOW,NOW,NOW,&dinode);
  101. dname_add(fs,ninode,".",ninode);
  102. dname_add(fs,ninode,"..",dinode);
  103. if (VERSION_2(fs)) {
  104. INODE2(fs,dinode)->i_nlinks++;
  105. INODE2(fs,ninode)->i_nlinks++;
  106. } else {
  107. INODE(fs,dinode)->i_nlinks++;
  108. INODE(fs,ninode)->i_nlinks++;
  109. }
  110. return ninode;
  111. }
  112. /**
  113. * Command to create directories
  114. * @param fs - filesystem structure
  115. * @param argc - from command line
  116. * @param argv - from command line
  117. */
  118. void cmd_mkdir(struct minix_fs_dat *fs,int argc,char **argv) {
  119. int i;
  120. for (i=1;i<argc;i++) {
  121. domkdir(fs,argv[i]);
  122. }
  123. }
  124. /**
  125. * Print time formatted
  126. * @param fp - output file pointer
  127. * @param str - type string
  128. * @param usecs - time stamp
  129. */
  130. void timefmt(FILE *fp,const char *str,u32 usecs) {
  131. struct tm tmb;
  132. time_t secs = usecs;
  133. gmtime_r(&secs,&tmb);
  134. fprintf(fp,"\t%s=%04d/%02d/%02d %02d:%02d:%02d (GMT)\n",
  135. str,
  136. tmb.tm_year + 1900, tmb.tm_mon+1, tmb.tm_mday,
  137. tmb.tm_hour, tmb.tm_min, tmb.tm_sec);
  138. }
  139. /**
  140. * Print formatted mode value
  141. * @param fp - output file
  142. * @param mode - mode bits
  143. */
  144. void outmode(FILE *fp,int mode) {
  145. switch (mode & S_IFMT) {
  146. case S_IFSOCK: fprintf(fp,"\ttype=socket\n"); break;
  147. case S_IFLNK: fprintf(fp,"\ttype=symbolic link\n"); break;
  148. case S_IFREG: fprintf(fp,"\ttype=regular file\n"); break;
  149. case S_IFBLK: fprintf(fp,"\ttype=block device\n"); break;
  150. case S_IFDIR: fprintf(fp,"\ttype=directory\n"); break;
  151. case S_IFCHR: fprintf(fp,"\ttype=character device\n"); break;
  152. case S_IFIFO: fprintf(fp,"\ttype=fifo\n"); break;
  153. default: fprintf(fp,"\ttype=unknown (%o)\n",mode & S_IFMT);
  154. }
  155. fprintf(fp,"\tmode=%04o\n",mode & 07777);
  156. }
  157. /**
  158. * Print stat entries for a inode
  159. * @param fs - filesystem structure
  160. * @param path - directory path to list
  161. */
  162. void dostat(struct minix_fs_dat *fs,const char *path) {
  163. int inode = find_inode(fs,path);
  164. if (inode == -1) {
  165. fprintf(stderr,"%s: not found\n",path);
  166. return;
  167. }
  168. printf("\tinode=%d\n",inode);
  169. if (VERSION_2(fs)) {
  170. struct minix2_inode *ino = INODE2(fs,inode);
  171. outmode(stdout,ino->i_mode);
  172. printf("\tnlinks=%d\n",ino->i_nlinks);
  173. if (S_ISCHR(ino->i_mode) || S_ISBLK(ino->i_mode))
  174. printf("\tmajor=%d,minor=%d\n",
  175. (ino->i_zone[0]>>8) & 0xff,
  176. ino->i_zone[0] & 0xff);
  177. else
  178. printf("\tsize=%d\n",ino->i_size);
  179. printf("\tuid=%d\n\tgid=%d\n",ino->i_uid,ino->i_gid);
  180. timefmt(stdout,"accessed",ino->i_atime);
  181. timefmt(stdout,"modified",ino->i_mtime);
  182. timefmt(stdout,"changed",ino->i_ctime);
  183. } else {
  184. struct minix_inode *ino = INODE(fs,inode);
  185. outmode(stdout,ino->i_mode);
  186. printf("\tnlinks=%d\n",ino->i_nlinks);
  187. if (S_ISCHR(ino->i_mode) || S_ISBLK(ino->i_mode))
  188. printf("\tmajor=%d\nminor=%d\n",
  189. (ino->i_zone[0]>>8) & 0xff,
  190. ino->i_zone[0] & 0xff);
  191. else
  192. printf("\tsize=%d\n",ino->i_size);
  193. printf("\tuid=%d\n\tgid=%d\n",ino->i_uid,ino->i_gid);
  194. timefmt(stdout,"accessed",ino->i_time);
  195. }
  196. }
  197. /**
  198. * Command to stat entries
  199. * @param fs - filesystem structure
  200. * @param argc - from command line
  201. * @param argv - from command line
  202. */
  203. void cmd_stat(struct minix_fs_dat *fs,int argc,char **argv) {
  204. if (argc == 1) {
  205. dostat(fs,".");
  206. } else if (argc == 2) {
  207. dostat(fs,argv[1]);
  208. } else {
  209. int i;
  210. for (i=1;i<argc;i++) {
  211. printf("%s:\n",argv[i]);
  212. dostat(fs,argv[i]);
  213. }
  214. }
  215. }
  216. /**
  217. * Remove an <b>empty</b> directory
  218. * @param fs - filesystem structure
  219. * @param dir - directory to remove
  220. */
  221. void dormdir(struct minix_fs_dat *fs,const char *dir) {
  222. int inode = find_inode(fs,dir);
  223. int i,bsz,j;
  224. u8 blk[BLOCK_SIZE];
  225. int fdirsize;
  226. int dentsz = DIRSIZE(fs);
  227. int pinode = -1;
  228. const char *p;
  229. if (inode == -1) fatalmsg("%s: not found",dir);
  230. if (inode == MINIX_ROOT_INO) fatalmsg("Can not remove root inode");
  231. /* Make sure directory is a directory */
  232. if (VERSION_2(fs)) {
  233. struct minix2_inode *ino = INODE2(fs,inode);
  234. if (!S_ISDIR(ino->i_mode)) fatalmsg("%s: is not a directory",dir);
  235. fdirsize = ino->i_size;
  236. } else {
  237. struct minix_inode *ino = INODE(fs,inode);
  238. if (!S_ISDIR(ino->i_mode)) fatalmsg("%s: is not a directory",dir);
  239. fdirsize = ino->i_size;
  240. }
  241. /* Do a directory scan... */
  242. for (i = 0; i < fdirsize; i += BLOCK_SIZE) {
  243. bsz = read_inoblk(fs,inode,i / BLOCK_SIZE,blk);
  244. for (j = 0; j < bsz ; j+= dentsz) {
  245. u16 fino = *((u16 *)(blk+j));
  246. if (blk[j+2] == '.' && blk[j+3] == 0) continue;
  247. if (blk[j+2] == '.' && blk[j+3] == '.'&& blk[j+4] == 0) {
  248. pinode = fino;
  249. continue;
  250. }
  251. if (fino != 0) fatalmsg("%s: not empty",dir);
  252. }
  253. }
  254. /* Free stuff */
  255. trunc_inode(fs,inode,0);
  256. clr_inode(fs, inode);
  257. if (VERSION_2(fs)) {
  258. INODE(fs,pinode)->i_nlinks--;
  259. } else {
  260. INODE(fs,pinode)->i_nlinks--;
  261. }
  262. p = strrchr(dir,'/');
  263. if (p)
  264. p++;
  265. else
  266. p = dir;
  267. dname_rem(fs,pinode,p);
  268. }
  269. /**
  270. * Remove directories
  271. * @param fs - filesystem structure
  272. * @param argc - from command line
  273. * @param argv - from command line
  274. */
  275. void cmd_rmdir(struct minix_fs_dat *fs,int argc,char **argv) {
  276. int i;
  277. for (i=1;i<argc;i++) {
  278. dormdir(fs,argv[i]);
  279. }
  280. }
  281. /**
  282. * Remove an file (not directory)
  283. * @param fs - filesystem structure
  284. * @param fpath - file path
  285. */
  286. void dounlink(struct minix_fs_dat *fs,char *fpath) {
  287. char *dir = fpath;
  288. char *fname = strrchr(fpath,'/');
  289. int dinode,inode;
  290. if (fname) {
  291. *(fname++) = 0;
  292. } else {
  293. dir = ".";
  294. fname = fpath;
  295. }
  296. dinode = find_inode(fs,dir);
  297. if (dinode == -1) fatalmsg("%s: not found\n",dir);
  298. inode = ilookup_name(fs,dinode,fname,NULL,NULL);
  299. if (inode == -1) fatalmsg("%s: not found\n",fname);
  300. /* Make sure file is not a directory */
  301. if (VERSION_2(fs)) {
  302. struct minix2_inode *ino = INODE2(fs,inode);
  303. if (S_ISDIR(ino->i_mode)) fatalmsg("%s: is a directory",dir);
  304. dname_rem(fs,dinode,fname);
  305. if (--(ino->i_nlinks)) return;
  306. } else {
  307. struct minix_inode *ino = INODE(fs,inode);
  308. if (!S_ISDIR(ino->i_mode)) fatalmsg("%s: is a directory",dir);
  309. dname_rem(fs,dinode,fname);
  310. if (--(ino->i_nlinks)) return;
  311. }
  312. /* Remove stuff... */
  313. trunc_inode(fs,inode,0);
  314. clr_inode(fs,inode);
  315. }
  316. /**
  317. * Remove files (but not directories)
  318. * @param fs - filesystem structure
  319. * @param argc - from command line
  320. * @param argv - from command line
  321. */
  322. void cmd_unlink(struct minix_fs_dat *fs,int argc,char **argv) {
  323. int i;
  324. for (i=1;i<argc;i++) {
  325. dounlink(fs,argv[i]);
  326. }
  327. }