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