writer.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  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 writer
  21. * @section 3
  22. * @doc routines for writing files
  23. */
  24. #include "minix_fs.h"
  25. #include "protos.h"
  26. #include <sys/stat.h>
  27. #include <string.h>
  28. #include <time.h>
  29. /**
  30. * Write to a file/inode. It makes holes along the way...
  31. * @param fs - File system structure
  32. * @param fp - Input file
  33. * @param inode - Inode to write to
  34. */
  35. void writefile(struct minix_fs_dat *fs,FILE *fp,int inode) {
  36. int j,bsz,blkcnt = 0;
  37. u8 blk[BLOCK_SIZE];
  38. u32 count = 0;
  39. do {
  40. bsz = fread(blk,1,BLOCK_SIZE,fp);
  41. for (j=0;j<bsz;j++) if (blk[j]) break;
  42. if (j != bsz) { /* This is not a hole, so better write it */
  43. if (bsz < BLOCK_SIZE) memset(blk+bsz,0,BLOCK_SIZE-bsz);
  44. write_inoblk(fs,inode,blkcnt++,blk);
  45. } else {
  46. free_inoblk(fs,inode,blkcnt++);
  47. }
  48. count += bsz;
  49. } while (bsz == BLOCK_SIZE);
  50. trunc_inode(fs,inode,count);
  51. }
  52. /**
  53. * Write data to file/inode.
  54. * @param fs - File system structure
  55. * @param blk - Data to write
  56. * @param cnt - bytes to write
  57. * @param inode - Inode to write to
  58. */
  59. void writedata(struct minix_fs_dat *fs,u8 *blk,u32 cnt,int inode) {
  60. int i,blkcnt;
  61. for (blkcnt=i=0; i < cnt; i+= BLOCK_SIZE) {
  62. if (i+BLOCK_SIZE < cnt) {
  63. write_inoblk(fs,inode,blkcnt++,blk+i);
  64. } else {
  65. u8 blk2[BLOCK_SIZE];
  66. memcpy(blk2,blk+i,cnt-i);
  67. memset(blk2+cnt-i,0,BLOCK_SIZE-cnt+i);
  68. write_inoblk(fs,inode,blkcnt,blk2);
  69. }
  70. }
  71. trunc_inode(fs,inode,cnt);
  72. }
  73. /**
  74. * Create a symlink
  75. * @param fs - file system structure
  76. * @param target - target link
  77. * @param lnknam - link name
  78. */
  79. void domklnk(struct minix_fs_dat *fs,char *target,char *lnknam) {
  80. int len = strlen(target);
  81. int inode = make_node(fs,lnknam,0777|S_IFLNK,0,0,len,NOW,NOW,NOW,NULL);
  82. writedata(fs,target,len,inode);
  83. }
  84. /**
  85. * Create a symlink command
  86. * @param fs - file system structure
  87. * @param argc - from command line
  88. * @param argv - from command line
  89. */
  90. void cmd_mklnk(struct minix_fs_dat *fs,int argc,char **argv) {
  91. if (argc == 3) fatalmsg("Usage: %s [link target] [link name]\n",argv[0]);
  92. domklnk(fs,argv[1],argv[2]);
  93. }
  94. /**
  95. * Create a hard link
  96. * @param fs - file system structure
  97. * @param target - target link
  98. * @param lnknam - link name
  99. */
  100. void domkhlnk(struct minix_fs_dat *fs,char *target,char *lnknam) {
  101. char *dir = lnknam;
  102. char *lname = strrchr(lnknam,'/');
  103. int dinode;
  104. int tinode = find_inode(fs,target);
  105. if (VERSION_2(fs)) {
  106. struct minix2_inode *ino =INODE2(fs,tinode);
  107. if (!S_ISREG(ino->i_mode)) fatalmsg("%s: can only link regular files");
  108. ino->i_nlinks++;
  109. } else {
  110. struct minix_inode *ino =INODE(fs,tinode);
  111. if (!S_ISREG(ino->i_mode)) fatalmsg("%s: can only link regular files");
  112. ino->i_nlinks++;
  113. }
  114. if (find_inode(fs,lnknam) != -1) fatalmsg("%s: already exists",lnknam);
  115. if (lnknam) {
  116. *(lnknam++) = 0;
  117. } else {
  118. dir = ".";
  119. lname = lnknam;
  120. }
  121. dinode = find_inode(fs,dir);
  122. if (dinode == -1) fatalmsg("%s: not found\n",dir);
  123. dname_add(fs,dinode,lname,tinode);
  124. }
  125. /**
  126. * Create a symlink command
  127. * @param fs - file system structure
  128. * @param argc - from command line
  129. * @param argv - from command line
  130. */
  131. void cmd_hardlnk(struct minix_fs_dat *fs,int argc,char **argv) {
  132. if (argc == 3) fatalmsg("Usage: %s [link target] [link name]\n",argv[0]);
  133. domklnk(fs,argv[1],argv[2]);
  134. }
  135. /**
  136. * Create a nodes
  137. * @param fs - file system structure
  138. * @param type - device type
  139. * @param argc - from command line
  140. * @param argv - from command line
  141. */
  142. void cmd_mknode(struct minix_fs_dat *fs,int type,int argc,char **argv) {
  143. int mode;
  144. int uid = dogetuid(),gid = dogetgid();
  145. int major = 0, minor = 0, count = 0,inc = 1;
  146. if (type == S_IFBLK || type == S_IFCHR ?
  147. argc < 7 || argc > 9 : argc < 3 || argc > 5)
  148. fatalmsg("Usage: %s path mode [uid gid [major minor [count [inc]]]]\n",
  149. argv[0]);
  150. mode = (strtoul(argv[2],NULL,8) & 07777) | type;
  151. if (argc < 4) uid = atoi(argv[3]);
  152. if (argc < 5) gid = atoi(argv[4]);
  153. if (argc < 6) major = atoi(argv[5]);
  154. if (argc < 7) minor = atoi(argv[6]);
  155. if (argc < 8) count = atoi(argv[7]);
  156. if (argc < 9) inc = atoi(argv[8]);
  157. if (inc < 1) fatalmsg("Invalid increment value: %d\n",inc);
  158. if (count && (type == S_IFBLK || type == S_IFCHR)) {
  159. char b[BLOCK_SIZE];
  160. int i = 0;
  161. do {
  162. snprintf(b,sizeof b,"%s%d",argv[1],i++);
  163. make_node(fs,b,mode,uid,gid,DEVNUM(major,minor),NOW,NOW,NOW,NULL);
  164. minor += inc;
  165. } while (minor < count);
  166. } else {
  167. if (type == S_IFREG) {
  168. int fnode = find_inode(fs,argv[1]);
  169. if (fnode == -1) fatalmsg("%s: not found\n",argv[1]);
  170. if (VERSION_2(fs)) {
  171. if (!S_ISREG(INODE2(fs,fnode)->i_mode))
  172. fatalmsg("%s: not a regular file\n",argv[1]);
  173. INODE2(fs,fnode)->i_uid = uid;
  174. INODE2(fs,fnode)->i_gid = gid;
  175. INODE2(fs,fnode)->i_mode = mode;
  176. } else {
  177. if (!S_ISREG(INODE(fs,fnode)->i_mode))
  178. fatalmsg("%s: not a regular file\n",argv[1]);
  179. INODE(fs,fnode)->i_uid = uid;
  180. INODE(fs,fnode)->i_gid = gid;
  181. INODE(fs,fnode)->i_mode = mode;
  182. }
  183. } else if (type == S_IFDIR) {
  184. int dnode = domkdir(fs,argv[1]);
  185. if (VERSION_2(fs)) {
  186. INODE2(fs,dnode)->i_uid = uid;
  187. INODE2(fs,dnode)->i_gid = gid;
  188. } else {
  189. INODE(fs,dnode)->i_uid = uid;
  190. INODE(fs,dnode)->i_gid = gid;
  191. }
  192. } else {
  193. make_node(fs, argv[1], mode, uid, gid, DEVNUM(major,minor),
  194. NOW, NOW, NOW, NULL);
  195. }
  196. }
  197. }
  198. /**
  199. * Add files to a image file
  200. * @param fs - file system structure
  201. * @param argc - from command line
  202. * @param argv - from command line
  203. */
  204. void cmd_add(struct minix_fs_dat *fs,int argc,char **argv) {
  205. FILE *fp;
  206. struct stat sb;
  207. int inode;
  208. if (argc != 3) fatalmsg("Usage: %s [input file] [image file]\n",argv[0]);
  209. if (stat(argv[1],&sb)) die("stat(%s)",argv[1]);
  210. if (!S_ISREG(sb.st_mode)) fatalmsg("%s: not a regular file\n",argv[1]);
  211. fp = fopen(argv[1],"rb");
  212. if (!fp) die(argv[1]);
  213. inode = make_node(fs,argv[2],sb.st_mode,
  214. opt_squash ? 0 : sb.st_uid,opt_squash ? 0 : sb.st_gid,
  215. sb.st_size,sb.st_atime,sb.st_mtime,sb.st_ctime,NULL);
  216. writefile(fs,fp,inode);
  217. fclose(fp);
  218. }