iname.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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 iname
  21. * @section 3
  22. * @doc directory name manipulation functions
  23. */
  24. #include "minix_fs.h"
  25. #include "protos.h"
  26. #include "bitops.h"
  27. #include <string.h>
  28. /**
  29. * Compare filenames in a directory structure
  30. * @param lname - name to compare
  31. * @param dname - directory name to compare
  32. * @param nlen - length of directory structure
  33. */
  34. int cmp_name(const char *lname,const char *dname,int nlen) {
  35. int i;
  36. for (i=0;i< nlen;i++) {
  37. if (lname[i] == '/' || lname[i] == 0) {
  38. if (dname[i] == 0) return 0;
  39. return -1;
  40. }
  41. if (lname[i] != dname[i]) return 1;
  42. }
  43. return 0;
  44. }
  45. /**
  46. * Find name in a directory
  47. * @param fs - filesystem structure
  48. * @param inode - inode for directory to search
  49. * @param lname - filename (nul or "/" terminated)
  50. * @param blkp - file block where name was found
  51. * @param offp - offset pointer where name was found
  52. * @return inode for found name, -1 on error.
  53. * @effect *blkp and *offp are updated if name is found.
  54. */
  55. int ilookup_name(struct minix_fs_dat *fs,int inode,const char *lname,
  56. u32 *blkp,int *offp) {
  57. int i,bsz,j;
  58. u8 blk[BLOCK_SIZE];
  59. int fdirsize = VERSION_2(fs) ?
  60. (INODE2(fs,inode))->i_size : (INODE(fs,inode))->i_size;
  61. int dentsz = DIRSIZE(fs);
  62. for (i = 0; i < fdirsize; i += BLOCK_SIZE) {
  63. bsz = read_inoblk(fs,inode,i / BLOCK_SIZE,blk);
  64. for (j = 0; j < bsz ; j+= dentsz) {
  65. u16 fino = *((u16 *)(blk+j));
  66. if (!fino) continue;
  67. if (!cmp_name(lname,blk+j+2,dentsz-2)) {
  68. if (blkp) *blkp = i / BLOCK_SIZE;
  69. if (offp) *offp = j;
  70. return fino;
  71. }
  72. }
  73. }
  74. return -1;
  75. }
  76. /**
  77. * Add file name to a directory.
  78. * @param fs - filesystem pointer
  79. * @param dir - path to directory
  80. * @param name - name to add
  81. * @param inode - inode number
  82. */
  83. void dname_add(struct minix_fs_dat *fs,int dinode,const char *name,int inode) {
  84. u8 blk[BLOCK_SIZE];
  85. int dentsz = DIRSIZE(fs);
  86. int dfsize = VERSION_2(fs) ?
  87. INODE2(fs,dinode)->i_size : INODE(fs,dinode)->i_size;
  88. int i,j=0,nblk=0,bsz;
  89. for (i=0;i< dfsize ; i+= BLOCK_SIZE) {
  90. bsz = read_inoblk(fs,dinode,nblk = i/BLOCK_SIZE,blk);
  91. for (j = 0; j < bsz ; j += dentsz) {
  92. u16 fino = *((u16 *)(blk+j));
  93. if (!fino) goto SKIP_ALL;
  94. }
  95. }
  96. SKIP_ALL:
  97. if (i >= dfsize) {
  98. /* Need to extend directory file */
  99. if (VERSION_2(fs)) {
  100. INODE2(fs,dinode)->i_size += dentsz;
  101. } else {
  102. INODE(fs,dinode)->i_size += dentsz;
  103. }
  104. if (j == BLOCK_SIZE) {
  105. nblk++;
  106. j = 0;
  107. memset(blk,0,BLOCK_SIZE);
  108. }
  109. } else {
  110. memset(blk+j,0,dentsz);
  111. }
  112. /* Create directory entry */
  113. *((u16 *)(blk+j)) = inode;
  114. strncpy(blk+j+2,name,dentsz-2);
  115. /* Update directory */
  116. write_inoblk(fs,dinode,nblk,blk);
  117. }
  118. /**
  119. * Remove file name from a directory.
  120. * @param fs - filesystem pointer
  121. * @param dir - path to directory
  122. * @param name - name to add
  123. * @return 0 in success, -1 in error.
  124. */
  125. void dname_rem(struct minix_fs_dat *fs,int dinode,const char *name) {
  126. u8 blk[BLOCK_SIZE];
  127. int dentsz = DIRSIZE(fs);
  128. u32 nblk;
  129. int off;
  130. int i;
  131. i = ilookup_name(fs,dinode,name,&nblk,&off);
  132. if (i == -1) return;
  133. i = (VERSION_2(fs) ? INODE2(fs,dinode)->i_size : INODE(fs,dinode)->i_size)
  134. - dentsz;
  135. if (i == (nblk * BLOCK_SIZE + off)) {
  136. /* Need to shorten directory file */
  137. if (VERSION_2(fs)) {
  138. INODE2(fs,dinode)->i_size = i;
  139. } else {
  140. INODE(fs,dinode)->i_size = i;
  141. }
  142. if (!(i % BLOCK_SIZE)) {
  143. /* We should free-up the last block... */
  144. free_inoblk(fs,dinode,(i/BLOCK_SIZE)+1);
  145. }
  146. } else {
  147. read_inoblk(fs,dinode,nblk,blk);
  148. memset(blk+off,0,dentsz);
  149. write_inoblk(fs,dinode,nblk,blk);
  150. }
  151. }
  152. /**
  153. * Create a new inode/directory
  154. * @param fs - file system structure
  155. * @param fpath - filename path
  156. * @param mode - mode
  157. * @param uid - user id
  158. * @param gid - group id
  159. * @param size - file size
  160. * @param atime - access time
  161. * @param mtime - modification time
  162. * @param ctime - change time
  163. * @param dinode_p - pointer to the directory that contains this node
  164. */
  165. int make_node(struct minix_fs_dat *fs,char *fpath, int mode,
  166. int uid, int gid, u32 size,
  167. u32 atime, u32 mtime, u32 ctime, int *dinode_p) {
  168. char *dir = fpath;
  169. char *fname = strrchr(fpath,'/');
  170. int dinode,ninode;
  171. if (find_inode(fs,fpath) != -1) fatalmsg("%s: already exists",fpath);
  172. if (fname) {
  173. *(fname++) = 0;
  174. } else {
  175. dir = ".";
  176. fname = fpath;
  177. }
  178. dinode = find_inode(fs,dir);
  179. if (dinode == -1) fatalmsg("%s: not found\n",dir);
  180. ninode = get_free_inode(fs); mark_inode(fs,ninode);
  181. /* Initialize inode */
  182. set_inode(fs, ninode, mode, 1, uid, gid,size,atime,mtime,ctime,1);
  183. dname_add(fs,dinode,fname,ninode);
  184. if (dinode_p) *dinode_p = dinode;
  185. return ninode;
  186. }