1 /* Copyright 1995 David C. Niemi
2 * Copyright 1996-2003,2005,2007-2009 Alain Knaff.
3 * This file is part of mtools.
5 * Mtools 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.
10 * Mtools is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with Mtools. If not, see <http://www.gnu.org/licenses/>.
20 * Miscellaneous VFAT-related functions
23 #include "sysincludes.h"
29 #include "dirCacheP.h"
30 #include "file_name.h"
34 const char *short_illegals=";+=[]',\"*\\<>/?:|";
35 const char *long_illegals = "\"*\\<>/?:|\005";
37 /* Automatically derive a new name */
38 static void autorename(char *name,
39 char tilda, char dot, const char *illegals,
43 unsigned int seqnum=0, maxseq=0;
48 printf("In autorename for name=%s.\n", name);
53 if (strchr(illegals, *p)) {
59 name[dotpos] && dotpos < limit && name[dotpos] != dot ;
61 if(name[dotpos] == tilda) {
65 } else if (name[dotpos] >= '0' && name[dotpos] <= '9') {
66 seqnum = seqnum * 10 + name[dotpos] - '0';
69 tildapos = -1; /* sequence number interrupted */
72 /* no sequence number yet */
73 if(dotpos > limit - 2) {
86 tildapos = dotpos - 2;
87 /* this matches Win95's behavior, and also guarantees
88 * us that the sequence numbers never get shorter */
90 if (seqnum == maxseq) {
99 if((bump && seqnum == 1) || seqnum > 1 || mtools_numeric_tail)
100 sprintf(name+tildapos,"%c%d",tilda, seqnum);
103 /* replace the character if it wasn't a space */
105 printf("Out autorename for name=%s.\n", name);
110 void autorename_short(dos_name_t *name, int bump)
112 autorename(name->base, '~', ' ', short_illegals, 8, bump);
115 void autorename_long(char *name, int bump)
117 autorename(name, '-', '\0', long_illegals, 255, bump);
121 static __inline__ int unicode_read(struct unicode_char *in,
122 wchar_t *out, int num)
124 wchar_t *end_out = out+num;
126 while(out < end_out) {
128 *out = in->lchar | ((in->uchar) << 8);
142 void clear_vfat(struct vfat_state *v)
152 * Calculate the checksum that results from the short name in *dir.
154 * The sum is formed by circularly right-shifting the previous sum
155 * and adding in each character, from left to right, padding both
156 * the name and extension to maximum length with spaces and skipping
157 * the "." (hence always summing exactly 11 characters).
159 * This exact algorithm is required in order to remain compatible
160 * with Microsoft Windows-95 and Microsoft Windows NT 3.5.
161 * Thanks to Jeffrey Richter of Microsoft Systems Journal for
162 * pointing me to the correct algorithm.
164 * David C. Niemi (niemi@tuxers.net) 95.01.19
166 static __inline__ unsigned char sum_shortname(const dos_name_t *dn)
169 const char *name=dn->base;
170 const char *end = name+11;
172 for (sum=0; name<end; ++name)
173 sum = ((sum & 1) ? 0x80 : 0) + (sum >> 1)
180 * Inspect a directory and any associated VSEs.
181 * Return 1 if the VSEs comprise a valid long file name,
184 static __inline__ void check_vfat(struct vfat_state *v, struct directory *dir)
188 if (! v->subentries) {
190 fprintf(stderr, "check_vfat: no VSEs.\n");
195 memcpy(dn.base, (char *)dir->name, 8);
196 memcpy(dn.ext, (char *)dir->ext, 3);
198 if (v->sum != sum_shortname(&dn))
201 if( (v->status & ((1<<v->subentries) - 1)) != (1<<v->subentries) - 1)
202 return; /* missing entries */
204 /* zero out byte following last entry, for good measure */
205 v->name[VSE_NAMELEN * v->subentries] = 0;
210 int clear_vses(Stream_t *Dir, int entrySlot, size_t last)
217 entry.entry = entrySlot;
219 /*maximize(last, entry.entry + MAX_VFAT_SUBENTRIES);*/
220 cache = allocDirCache(Dir, last);
222 fprintf(stderr, "Out of memory error in clear_vses\n");
225 addFreeEntry(cache, entry.entry, last);
226 for (; entry.entry < (signed int) last; ++entry.entry) {
228 fprintf(stderr,"Clearing entry %d.\n", entry.entry);
230 dir_read(&entry, &error);
233 if(!entry.dir.name[0] || entry.dir.name[0] == DELMARK)
235 entry.dir.name[0] = DELMARK;
236 if (entry.dir.attr == 0xf)
237 entry.dir.attr = '\0';
238 low_level_dir_write(&entry);
243 int write_vfat(Stream_t *Dir, dos_name_t *shortname, char *longname, int start,
244 direntry_t *mainEntry)
246 struct vfat_subentry *vse;
247 int vse_id, num_vses;
251 wchar_t unixyName[13];
252 doscp_t *cp = GET_DOSCONVERT(Dir);
254 wchar_t wlongname[MAX_VNAMELEN+1];
259 printf("Entering write_vfat with longname=\"%s\", start=%d.\n",
263 vse = (struct vfat_subentry *) &entry.dir;
264 /* Fill in invariant part of vse */
265 vse->attribute = 0x0f;
266 vse->hash1 = vse->sector_l = vse->sector_u = 0;
267 vse->sum = sum_shortname(shortname);
269 printf("Wrote checksum=%d for shortname %s.%s\n",
270 vse->sum,shortname->base,shortname->ext);
273 wlen = native_to_wchar(longname, wlongname, MAX_VNAMELEN+1,
275 num_vses = (wlen + VSE_NAMELEN - 1)/VSE_NAMELEN;
276 for (vse_id = num_vses; vse_id; --vse_id) {
279 c = wlongname + (vse_id - 1) * VSE_NAMELEN;
281 c += unicode_write(c, vse->text1, VSE1SIZE, &end);
282 c += unicode_write(c, vse->text2, VSE2SIZE, &end);
283 c += unicode_write(c, vse->text3, VSE3SIZE, &end);
285 vse->id = (vse_id == num_vses) ? (vse_id | VSE_LAST) : vse_id;
287 printf("Writing longname=(%s), VSE %d (%13s) at %d, end = %d.\n",
288 longname, vse_id, longname + (vse_id-1) * VSE_NAMELEN,
289 start + num_vses - vse_id, start + num_vses);
292 entry.entry = start + num_vses - vse_id;
293 low_level_dir_write(&entry);
299 cache = allocDirCache(Dir, start + num_vses + 1);
301 fprintf(stderr, "Out of memory error\n");
304 unix_name(cp, shortname->base, shortname->ext, 0, unixyName);
305 addUsedEntry(cache, start, start + num_vses + 1, wlongname, unixyName,
307 low_level_dir_write(mainEntry);
308 return start + num_vses;
311 void dir_write(direntry_t *entry)
313 dirCacheEntry_t *dce;
316 if(entry->entry == -3) {
317 fprintf(stderr, "Attempt to write root directory pointer\n");
321 cache = allocDirCache(entry->Dir, entry->entry + 1);
323 fprintf(stderr, "Out of memory error in dir_write\n");
326 dce = cache->entries[entry->entry];
328 if(entry->dir.name[0] == DELMARK) {
329 addFreeEntry(cache, dce->beginSlot, dce->endSlot);
331 dce->dir = entry->dir;
334 low_level_dir_write(entry);
339 * The following function translates a series of vfat_subentries into
340 * data suitable for a dircache entry
342 static __inline__ void parse_vses(direntry_t *entry,
343 struct vfat_state *v)
345 struct vfat_subentry *vse;
346 unsigned char id, last_flag;
349 vse = (struct vfat_subentry *) &entry->dir;
351 id = vse->id & VSE_MASK;
352 last_flag = (vse->id & VSE_LAST);
353 if (id > MAX_VFAT_SUBENTRIES) {
354 fprintf(stderr, "parse_vses: invalid VSE ID %d at %d.\n",
359 /* 950819: This code enforced finding the VSEs in order. Well, Win95
360 * likes to write them in *reverse* order for some bizarre reason! So
361 * we pretty much have to tolerate them coming in any possible order.
362 * So skip this check, we'll do without it (What does this do, Alain?).
364 * 950820: Totally rearranged code to tolerate any order but to warn if
365 * they are not in reverse order like Win95 uses.
367 * 950909: Tolerate any order. We recognize new chains by mismatching
368 * checksums. In the event that the checksums match, new entries silently
369 * overwrite old entries of the same id. This should accept all valid
370 * entries, but may fail to reject invalid entries in some rare cases.
373 /* bad checksum, begin new chain */
374 if(v->sum != vse->sum) {
380 if(v->status & (1 << (id-1)))
382 "parse_vses: duplicate VSE %d\n", vse->id);
385 v->status |= 1 << (id-1);
390 if (id > v->subentries)
391 /* simple test to detect entries preceding
392 * the "last" entry (really the first) */
394 "parse_vses: new VSE %d sans LAST flag\n",
398 c = &(v->name[VSE_NAMELEN * (id-1)]);
399 c += unicode_read(vse->text1, c, VSE1SIZE);
400 c += unicode_read(vse->text2, c, VSE2SIZE);
401 c += unicode_read(vse->text3, c, VSE3SIZE);
403 printf("Read VSE %d at %d, subentries=%d, = (%13ls).\n",
404 id,entry->entry,v->subentries,&(v->name[VSE_NAMELEN * (id-1)]));
407 *c = '\0'; /* Null terminate long name */
411 * Read one complete entry from directory (main name plus any VSEs
414 static dirCacheEntry_t *vfat_lookup_loop_common(doscp_t *cp,
415 direntry_t *direntry,
417 int lookForFreeSpace,
421 int initpos = direntry->entry + 1;
422 struct vfat_state vfat;
432 if(!dir_read(direntry, &error)){
437 addFreeEndEntry(cache, initpos, direntry->entry,
439 return addEndEntry(cache, direntry->entry);
442 if (endmarkSeen || direntry->dir.name[0] == ENDMARK){
443 /* the end of the directory */
444 if(lookForFreeSpace) {
448 return addEndEntry(cache, direntry->entry);
450 if(direntry->dir.name[0] != DELMARK &&
451 direntry->dir.attr == 0x0f)
452 parse_vses(direntry, &vfat);
458 /* If we get here, it's a short name FAT entry, maybe erased.
459 * thus we should make sure that the vfat structure will be
460 * cleared before the next loop run */
463 if (direntry->dir.name[0] == DELMARK) {
464 return addFreeEntry(cache, initpos,
465 direntry->entry + 1);
468 check_vfat(&vfat, &direntry->dir);
472 /* mark space between last entry and this one as free */
473 addFreeEntry(cache, initpos,
474 direntry->entry - vfat.subentries);
476 if (direntry->dir.attr & 0x8){
477 /* Read entry as a label */
478 wchar_t *ptr = newfile;
479 ptr += dos_to_wchar(cp, direntry->dir.name, ptr, 8);
480 ptr += dos_to_wchar(cp, direntry->dir.ext, ptr, 3);
490 longname = vfat.name;
494 return addUsedEntry(cache, direntry->entry - vfat.subentries,
495 direntry->entry + 1, longname,
496 newfile, &direntry->dir);
499 static __inline__ dirCacheEntry_t *vfat_lookup_loop_for_read(doscp_t *cp,
500 direntry_t *direntry,
504 int initpos = direntry->entry + 1;
505 dirCacheEntry_t *dce;
508 dce = cache->entries[initpos];
510 direntry->entry = dce->endSlot - 1;
513 return vfat_lookup_loop_common(cp,
514 direntry, cache, 0, io_error);
519 typedef enum result_t {
532 static result_t checkNameForMatch(struct direntry_t *direntry,
533 dirCacheEntry_t *dce,
534 const wchar_t *filename,
546 fprintf(stderr, "Unexpected entry type %d\n",
551 direntry->dir = dce->dir;
553 /* make sure the entry is of an accepted type */
554 if((direntry->dir.attr & 0x8) && !(flags & ACCEPT_LABEL))
558 /*---------- multiple files ----------*/
559 if(!((flags & MATCH_ANY) ||
561 match(dce->longName, filename, direntry->name, 0, length)) ||
562 match(dce->shortName, filename, direntry->name, 1, length))) {
567 /* entry of non-requested type, has to come after name
568 * checking because of clash handling */
569 if(IS_DIR(direntry) && !(flags & ACCEPT_DIR)) {
570 if(!(flags & (ACCEPT_LABEL|MATCH_ANY|NO_MSG))) {
572 wchar_to_native(dce->shortName,tmp,13);
573 fprintf(stderr, "Skipping \"%s\", is a directory\n",
579 if(!(direntry->dir.attr & (ATTR_LABEL | ATTR_DIR)) &&
580 !(flags & ACCEPT_PLAIN)) {
581 if(!(flags & (ACCEPT_LABEL|MATCH_ANY|NO_MSG))) {
583 wchar_to_native(dce->shortName,tmp,13);
585 "Skipping \"%s\", is not a directory\n",
596 * vfat_lookup looks for filenames in directory dir.
597 * if a name if found, it is returned in outname
598 * if applicable, the file is opened and its stream is returned in File
601 int vfat_lookup(direntry_t *direntry, const char *filename, int length,
602 int flags, char *shortname, char *longname)
604 dirCacheEntry_t *dce;
608 wchar_t wfilename[MAX_VNAMELEN+1];
609 doscp_t *cp = GET_DOSCONVERT(direntry->Dir);
611 if(length == -1 && filename)
612 length = strlen(filename);
615 length = native_to_wchar(filename, wfilename, MAX_VNAMELEN,
620 if (direntry->entry == -2)
623 cache = allocDirCache(direntry->Dir, direntry->entry+1);
625 fprintf(stderr, "Out of memory error in vfat_lookup [0]\n");
630 dce = vfat_lookup_loop_for_read(cp, direntry, cache, &io_error);
634 fprintf(stderr, "Out of memory error in vfat_lookup\n");
637 result = checkNameForMatch(direntry, dce,
640 } while(result == RES_NOMATCH);
642 if(result == RES_MATCH){
645 wchar_to_native(dce->longName,
646 longname, MAX_VNAMELEN);
651 wchar_to_native(dce->shortName, shortname, 12);
652 direntry->beginSlot = dce->beginSlot;
653 direntry->endSlot = dce->endSlot-1;
654 return 0; /* file found */
656 direntry->entry = -2;
657 return -1; /* no file found */
661 static __inline__ dirCacheEntry_t *vfat_lookup_loop_for_insert(doscp_t *cp,
662 direntry_t *direntry,
666 dirCacheEntry_t *dce;
669 dce = cache->entries[initpos];
670 if(dce && dce->type != DCET_END) {
673 direntry->entry = initpos - 1;
674 dce = vfat_lookup_loop_common(cp,
675 direntry, cache, 1, &io_error);
681 "Out of memory error in vfat_lookup_loop\n");
684 return cache->entries[initpos];
688 static void accountFreeSlots(struct scan_state *ssp, dirCacheEntry_t *dce)
693 if(ssp->free_end != dce->beginSlot) {
694 ssp->free_start = dce->beginSlot;
696 ssp->free_end = dce->endSlot;
698 if(ssp->free_end - ssp->free_start >= ssp->size_needed) {
700 ssp->slot = ssp->free_start + ssp->size_needed - 1;
704 static void clear_scan(wchar_t *longname, int use_longname,
705 struct scan_state *s)
707 s->shortmatch = s->longmatch = s->slot = -1;
708 s->free_end = s->got_slots = s->free_start = 0;
710 if (use_longname & 1)
712 (wcslen(longname) + VSE_NAMELEN - 1)/VSE_NAMELEN;
717 /* lookup_for_insert replaces the old scandir function. It directly
718 * calls into vfat_lookup_loop, thus eliminating the overhead of the
721 int lookupForInsert(Stream_t *Dir,
722 struct direntry_t *direntry,
725 struct scan_state *ssp,
728 int pessimisticShortRename,
733 dirCacheEntry_t *dce;
735 int pos; /* position _before_ the next answered entry */
736 wchar_t shortName[13];
737 wchar_t wlongname[MAX_VNAMELEN+1];
738 doscp_t *cp = GET_DOSCONVERT(Dir);
740 native_to_wchar(longname, wlongname, MAX_VNAMELEN+1, 0, 0);
741 clear_scan(wlongname, use_longname, ssp);
743 ignore_match = (ignore_entry == -2 );
745 initializeDirentry(&entry, Dir);
748 /* hash bitmap of already encountered names. Speeds up batch appends
749 * to huge directories, because in the best case, we only need to scan
750 * the new entries rather than the whole directory */
751 cache = allocDirCache(Dir, 1);
753 fprintf(stderr, "Out of memory error in lookupForInsert\n");
758 unix_name(cp, dosname->base, dosname->ext, 0, shortName);
760 pos = cache->nrHashed;
761 if(source_entry >= 0 ||
762 (pos && isHashed(cache, wlongname))) {
764 } else if(pos && !ignore_match && isHashed(cache, shortName)) {
765 if(pessimisticShortRename) {
766 ssp->shortmatch = -2;
770 } else if(growDirCache(cache, pos) < 0) {
771 fprintf(stderr, "Out of memory error in vfat_looup [0]\n");
775 dce = vfat_lookup_loop_for_insert(cp, &entry, pos, cache);
778 accountFreeSlots(ssp, dce);
781 if(!(dce->dir.attr & 0x8) &&
782 (signed int)dce->endSlot-1 == source_entry)
783 accountFreeSlots(ssp, dce);
785 /* labels never match, neither does the
787 if( (dce->dir.attr & 0x8) ||
788 ((signed int)dce->endSlot-1==ignore_entry))
791 /* check long name */
793 !wcscasecmp(dce->longName, wlongname)) ||
795 !wcscasecmp(dce->shortName, wlongname))) {
796 ssp->longmatch = dce->endSlot - 1;
797 /* long match is a reason for
799 direntry->beginSlot = dce->beginSlot;
800 direntry->endSlot = dce->endSlot-1;
804 /* Long name or not, always check for
805 * short name match */
807 !wcscasecmp(shortName, dce->shortName))
808 ssp->shortmatch = dce->endSlot - 1;
814 } while(dce->type != DCET_END);
815 if (ssp->shortmatch > -1)
817 ssp->max_entry = dce->beginSlot;
819 return 6; /* Success */
821 /* Need more room. Can we grow the directory? */
823 return 5; /* OK, try to grow the directory */
825 fprintf(stderr, "No directory slots\n");