file.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  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)
  35. *(*p)++ = c;
  36. else {
  37. *(*p)++ = '\\';
  38. *(*p)++ = '0' + (c >> 6);
  39. *(*p)++ = '0' + ((c >> 3) & 7);
  40. *(*p)++ = '0' + (c & 7);
  41. }
  42. }
  43. /**
  44. * Construct the "pretty-printed" representation of the name in a short directory entry.
  45. *
  46. * @param[in] fixed Pointer to name[0] of a DIR_ENT
  47. *
  48. * @return Pointer to static string containing pretty "8.3" equivalent of the
  49. * name in the directory entry.
  50. */
  51. char *file_name(unsigned char *fixed)
  52. {
  53. static char path[MSDOS_NAME * 4 + 2];
  54. char *p;
  55. int i, j;
  56. p = path;
  57. for (i = j = 0; i < 8; i++)
  58. if (fixed[i] != ' ') {
  59. while (j++ < i)
  60. *p++ = ' ';
  61. put_char(&p, fixed[i]);
  62. }
  63. if (strncmp(fixed + 8, " ", 3)) {
  64. *p++ = '.';
  65. for (i = j = 0; i < 3; i++)
  66. if (fixed[i + 8] != ' ') {
  67. while (j++ < i)
  68. *p++ = ' ';
  69. put_char(&p, fixed[i + 8]);
  70. }
  71. }
  72. *p = 0;
  73. return path;
  74. }
  75. int file_cvt(unsigned char *name, unsigned char *fixed)
  76. {
  77. unsigned char c;
  78. int size, ext, cnt;
  79. size = 8;
  80. ext = 0;
  81. while (*name) {
  82. c = *name;
  83. if (c < ' ' || c > 0x7e || strchr("*?<>|\"/", c)) {
  84. printf("Invalid character in name. Use \\ooo for special "
  85. "characters.\n");
  86. return 0;
  87. }
  88. if (c == '.') {
  89. if (ext) {
  90. printf("Duplicate dots in name.\n");
  91. return 0;
  92. }
  93. while (size--)
  94. *fixed++ = ' ';
  95. size = 3;
  96. ext = 1;
  97. name++;
  98. continue;
  99. }
  100. if (c == '\\') {
  101. c = 0;
  102. for (cnt = 3; cnt; cnt--) {
  103. if (*name < '0' || *name > '7') {
  104. printf("Invalid octal character.\n");
  105. return 0;
  106. }
  107. c = c * 8 + *name++ - '0';
  108. }
  109. if (cnt < 4) {
  110. printf("Expected three octal digits.\n");
  111. return 0;
  112. }
  113. name += 3;
  114. }
  115. if (islower(c))
  116. c = toupper(c);
  117. if (size) {
  118. *fixed++ = c;
  119. size--;
  120. }
  121. name++;
  122. }
  123. if (*name || size == 8)
  124. return 0;
  125. if (!ext) {
  126. while (size--)
  127. *fixed++ = ' ';
  128. size = 3;
  129. }
  130. while (size--)
  131. *fixed++ = ' ';
  132. return 1;
  133. }
  134. void file_add(char *path, FD_TYPE type)
  135. {
  136. FDSC **current, *walk;
  137. char name[MSDOS_NAME];
  138. char *here;
  139. current = &fp_root;
  140. if (*path != '/')
  141. die("%s: Absolute path required.", path);
  142. path++;
  143. while (1) {
  144. if ((here = strchr(path, '/')))
  145. *here = 0;
  146. if (!file_cvt(path, name))
  147. exit(2);
  148. for (walk = *current; walk; walk = walk->next)
  149. if (!here && (!strncmp(name, walk->name, MSDOS_NAME) || (type ==
  150. fdt_undelete
  151. &&
  152. !strncmp
  153. (name + 1,
  154. walk->
  155. name + 1,
  156. MSDOS_NAME
  157. - 1))))
  158. die("Ambiguous name: \"%s\"", path);
  159. else if (here && !strncmp(name, walk->name, MSDOS_NAME))
  160. break;
  161. if (!walk) {
  162. walk = alloc(sizeof(FDSC));
  163. strncpy(walk->name, name, MSDOS_NAME);
  164. walk->type = here ? fdt_none : type;
  165. walk->first = NULL;
  166. walk->next = *current;
  167. *current = walk;
  168. }
  169. current = &walk->first;
  170. if (!here)
  171. break;
  172. *here = '/';
  173. path = here + 1;
  174. }
  175. }
  176. FDSC **file_cd(FDSC ** curr, char *fixed)
  177. {
  178. FDSC **walk;
  179. if (!curr || !*curr)
  180. return NULL;
  181. for (walk = curr; *walk; walk = &(*walk)->next)
  182. if (!strncmp((*walk)->name, fixed, MSDOS_NAME) && (*walk)->first)
  183. return &(*walk)->first;
  184. return NULL;
  185. }
  186. static FDSC **file_find(FDSC ** dir, char *fixed)
  187. {
  188. if (!dir || !*dir)
  189. return NULL;
  190. if (*(unsigned char *)fixed == DELETED_FLAG) {
  191. while (*dir) {
  192. if (!strncmp((*dir)->name + 1, fixed + 1, MSDOS_NAME - 1)
  193. && !(*dir)->first)
  194. return dir;
  195. dir = &(*dir)->next;
  196. }
  197. return NULL;
  198. }
  199. while (*dir) {
  200. if (!strncmp((*dir)->name, fixed, MSDOS_NAME) && !(*dir)->first)
  201. return dir;
  202. dir = &(*dir)->next;
  203. }
  204. return NULL;
  205. }
  206. /* Returns the attribute of the file FIXED in directory CURR or FDT_NONE if no
  207. such file exists or if CURR is NULL. */
  208. FD_TYPE file_type(FDSC ** curr, char *fixed)
  209. {
  210. FDSC **this;
  211. if ((this = file_find(curr, fixed)))
  212. return (*this)->type;
  213. return fdt_none;
  214. }
  215. void file_modify(FDSC ** curr, char *fixed)
  216. {
  217. FDSC **this, *next;
  218. if (!(this = file_find(curr, fixed)))
  219. die("Internal error: file_find failed");
  220. switch ((*this)->type) {
  221. case fdt_drop:
  222. printf("Dropping %s\n", file_name(fixed));
  223. *(unsigned char *)fixed = DELETED_FLAG;
  224. break;
  225. case fdt_undelete:
  226. *fixed = *(*this)->name;
  227. printf("Undeleting %s\n", file_name(fixed));
  228. break;
  229. default:
  230. die("Internal error: file_modify");
  231. }
  232. next = (*this)->next;
  233. free(*this);
  234. *this = next;
  235. }
  236. static void report_unused(FDSC * this)
  237. {
  238. FDSC *next;
  239. while (this) {
  240. next = this->next;
  241. if (this->first)
  242. report_unused(this->first);
  243. else if (this->type != fdt_none)
  244. printf("Warning: did not %s file %s\n", this->type == fdt_drop ?
  245. "drop" : "undelete", file_name(this->name));
  246. free(this);
  247. this = next;
  248. }
  249. }
  250. void file_unused(void)
  251. {
  252. report_unused(fp_root);
  253. }