file.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. /* file.c - Additional file attributes
  2. Copyright (C) 1993 Werner Almesberger <werner.almesberger@lrc.di.epfl.ch>
  3. Copyright (C) 1998 Roman Hodek <Roman.Hodek@informatik.uni-erlangen.de>
  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 3 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>.
  14. On Debian systems, the complete text of the GNU General Public License
  15. can be found in /usr/share/common-licenses/GPL-3 file.
  16. */
  17. /* FAT32, VFAT, Atari format support, and various fixes additions May 1998
  18. * by Roman Hodek <Roman.Hodek@informatik.uni-erlangen.de> */
  19. #include <stdlib.h>
  20. #include <stdio.h>
  21. #include <string.h>
  22. #include <ctype.h>
  23. #include <unistd.h>
  24. #define _LINUX_STAT_H /* hack to avoid inclusion of <linux/stat.h> */
  25. #define _LINUX_STRING_H_ /* hack to avoid inclusion of <linux/string.h>*/
  26. #define _LINUX_FS_H /* hack to avoid inclusion of <linux/fs.h> */
  27. # include <asm/types.h>
  28. #include <linux/msdos_fs.h>
  29. #include "common.h"
  30. #include "file.h"
  31. FDSC *fp_root = NULL;
  32. static void put_char(char **p,unsigned char c)
  33. {
  34. if ((c >= ' ' && c < 0x7f) || c >= 0xa0) *(*p)++ = c;
  35. else {
  36. *(*p)++ = '\\';
  37. *(*p)++ = '0'+(c >> 6);
  38. *(*p)++ = '0'+((c >> 3) & 7);
  39. *(*p)++ = '0'+(c & 7);
  40. }
  41. }
  42. /**
  43. * Construct the "pretty-printed" representation of the name in a short directory entry.
  44. *
  45. * @param[in] fixed Pointer to name[0] of a DIR_ENT
  46. *
  47. * @return Pointer to static string containing pretty "8.3" equivalent of the
  48. * name in the directory entry.
  49. */
  50. char *file_name(unsigned char *fixed)
  51. {
  52. static char path[MSDOS_NAME*4+2];
  53. char *p;
  54. int i,j;
  55. p = path;
  56. for (i = j = 0; i < 8; i++)
  57. if (fixed[i] != ' ') {
  58. while (j++ < i) *p++ = ' ';
  59. put_char(&p,fixed[i]);
  60. }
  61. if (strncmp(fixed+8," ",3)) {
  62. *p++ = '.';
  63. for (i = j = 0; i < 3; i++)
  64. if (fixed[i+8] != ' ') {
  65. while (j++ < i) *p++ = ' ';
  66. put_char(&p,fixed[i+8]);
  67. }
  68. }
  69. *p = 0;
  70. return path;
  71. }
  72. int file_cvt(unsigned char *name,unsigned char *fixed)
  73. {
  74. unsigned char c;
  75. int size,ext,cnt;
  76. size = 8;
  77. ext = 0;
  78. while (*name) {
  79. c = *name;
  80. if (c < ' ' || c > 0x7e || strchr("*?<>|\"/",c)) {
  81. printf("Invalid character in name. Use \\ooo for special "
  82. "characters.\n");
  83. return 0;
  84. }
  85. if (c == '.') {
  86. if (ext) {
  87. printf("Duplicate dots in name.\n");
  88. return 0;
  89. }
  90. while (size--) *fixed++ = ' ';
  91. size = 3;
  92. ext = 1;
  93. name++;
  94. continue;
  95. }
  96. if (c == '\\') {
  97. c = 0;
  98. for (cnt = 3; cnt; cnt--) {
  99. if (*name < '0' || *name > '7') {
  100. printf("Invalid octal character.\n");
  101. return 0;
  102. }
  103. c = c*8+*name++-'0';
  104. }
  105. if (cnt < 4) {
  106. printf("Expected three octal digits.\n");
  107. return 0;
  108. }
  109. name += 3;
  110. }
  111. if (islower(c)) c = toupper(c);
  112. if (size) {
  113. *fixed++ = c;
  114. size--;
  115. }
  116. name++;
  117. }
  118. if (*name || size == 8) return 0;
  119. if (!ext) {
  120. while (size--) *fixed++ = ' ';
  121. size = 3;
  122. }
  123. while (size--) *fixed++ = ' ';
  124. return 1;
  125. }
  126. void file_add(char *path,FD_TYPE type)
  127. {
  128. FDSC **current,*walk;
  129. char name[MSDOS_NAME];
  130. char *here;
  131. current = &fp_root;
  132. if (*path != '/') die("%s: Absolute path required.",path);
  133. path++;
  134. while (1) {
  135. if ((here = strchr(path,'/'))) *here = 0;
  136. if (!file_cvt(path,name)) exit(2);
  137. for (walk = *current; walk; walk = walk->next)
  138. if (!here && (!strncmp(name,walk->name,MSDOS_NAME) || (type ==
  139. fdt_undelete && !strncmp(name+1,walk->name+1,MSDOS_NAME-1))))
  140. die("Ambiguous name: \"%s\"",path);
  141. else if (here && !strncmp(name,walk->name,MSDOS_NAME)) break;
  142. if (!walk) {
  143. walk = alloc(sizeof(FDSC));
  144. strncpy(walk->name,name,MSDOS_NAME);
  145. walk->type = here ? fdt_none : type;
  146. walk->first = NULL;
  147. walk->next = *current;
  148. *current = walk;
  149. }
  150. current = &walk->first;
  151. if (!here) break;
  152. *here = '/';
  153. path = here+1;
  154. }
  155. }
  156. FDSC **file_cd(FDSC **curr,char *fixed)
  157. {
  158. FDSC **walk;
  159. if (!curr || !*curr) return NULL;
  160. for (walk = curr; *walk; walk = &(*walk)->next)
  161. if (!strncmp((*walk)->name,fixed,MSDOS_NAME) && (*walk)->first)
  162. return &(*walk)->first;
  163. return NULL;
  164. }
  165. static FDSC **file_find(FDSC **dir,char *fixed)
  166. {
  167. if (!dir || !*dir) return NULL;
  168. if (*(unsigned char *) fixed == DELETED_FLAG) {
  169. while (*dir) {
  170. if (!strncmp((*dir)->name+1,fixed+1,MSDOS_NAME-1) && !(*dir)->first)
  171. return dir;
  172. dir = &(*dir)->next;
  173. }
  174. return NULL;
  175. }
  176. while (*dir) {
  177. if (!strncmp((*dir)->name,fixed,MSDOS_NAME) && !(*dir)->first)
  178. return dir;
  179. dir = &(*dir)->next;
  180. }
  181. return NULL;
  182. }
  183. /* Returns the attribute of the file FIXED in directory CURR or FDT_NONE if no
  184. such file exists or if CURR is NULL. */
  185. FD_TYPE file_type(FDSC **curr,char *fixed)
  186. {
  187. FDSC **this;
  188. if ((this = file_find(curr,fixed))) return (*this)->type;
  189. return fdt_none;
  190. }
  191. void file_modify(FDSC **curr,char *fixed)
  192. {
  193. FDSC **this,*next;
  194. if (!(this = file_find(curr,fixed)))
  195. die("Internal error: file_find failed");
  196. switch ((*this)->type) {
  197. case fdt_drop:
  198. printf("Dropping %s\n",file_name(fixed));
  199. *(unsigned char *) fixed = DELETED_FLAG;
  200. break;
  201. case fdt_undelete:
  202. *fixed = *(*this)->name;
  203. printf("Undeleting %s\n",file_name(fixed));
  204. break;
  205. default:
  206. die("Internal error: file_modify");
  207. }
  208. next = (*this)->next;
  209. free(*this);
  210. *this = next;
  211. }
  212. static void report_unused(FDSC *this)
  213. {
  214. FDSC *next;
  215. while (this) {
  216. next = this->next;
  217. if (this->first) report_unused(this->first);
  218. else if (this->type != fdt_none)
  219. printf("Warning: did not %s file %s\n",this->type == fdt_drop ?
  220. "drop" : "undelete",file_name(this->name));
  221. free(this);
  222. this = next;
  223. }
  224. }
  225. void file_unused(void)
  226. {
  227. report_unused(fp_root);
  228. }