file.c 6.5 KB

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