1*d5c9a868SElliott Hughes /* Copyright 1986-1992 Emmet P. Gray.
2*d5c9a868SElliott Hughes * Copyright 1996-1998,2001,2002,2009 Alain Knaff.
3*d5c9a868SElliott Hughes * This file is part of mtools.
4*d5c9a868SElliott Hughes *
5*d5c9a868SElliott Hughes * Mtools is free software: you can redistribute it and/or modify
6*d5c9a868SElliott Hughes * it under the terms of the GNU General Public License as published by
7*d5c9a868SElliott Hughes * the Free Software Foundation, either version 3 of the License, or
8*d5c9a868SElliott Hughes * (at your option) any later version.
9*d5c9a868SElliott Hughes *
10*d5c9a868SElliott Hughes * Mtools is distributed in the hope that it will be useful,
11*d5c9a868SElliott Hughes * but WITHOUT ANY WARRANTY; without even the implied warranty of
12*d5c9a868SElliott Hughes * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13*d5c9a868SElliott Hughes * GNU General Public License for more details.
14*d5c9a868SElliott Hughes *
15*d5c9a868SElliott Hughes * You should have received a copy of the GNU General Public License
16*d5c9a868SElliott Hughes * along with Mtools. If not, see <http://www.gnu.org/licenses/>.
17*d5c9a868SElliott Hughes */
18*d5c9a868SElliott Hughes
19*d5c9a868SElliott Hughes #include "sysincludes.h"
20*d5c9a868SElliott Hughes #include "msdos.h"
21*d5c9a868SElliott Hughes #include "fsP.h"
22*d5c9a868SElliott Hughes #include "mtoolsDirentry.h"
23*d5c9a868SElliott Hughes
24*d5c9a868SElliott Hughes /*
25*d5c9a868SElliott Hughes * Remove a string of FAT entries (delete the file). The argument is
26*d5c9a868SElliott Hughes * the beginning of the string. Does not consider the file length, so
27*d5c9a868SElliott Hughes * if FAT is corrupted, watch out!
28*d5c9a868SElliott Hughes */
29*d5c9a868SElliott Hughes
fat_free(Stream_t * Dir,unsigned int fat)30*d5c9a868SElliott Hughes int fat_free(Stream_t *Dir, unsigned int fat)
31*d5c9a868SElliott Hughes {
32*d5c9a868SElliott Hughes Stream_t *Stream = GetFs(Dir);
33*d5c9a868SElliott Hughes DeclareThis(Fs_t);
34*d5c9a868SElliott Hughes unsigned int next_no_step;
35*d5c9a868SElliott Hughes /* a zero length file? */
36*d5c9a868SElliott Hughes if (fat == 0)
37*d5c9a868SElliott Hughes return(0);
38*d5c9a868SElliott Hughes /* CONSTCOND */
39*d5c9a868SElliott Hughes while (!This->fat_error) {
40*d5c9a868SElliott Hughes /* get next cluster number */
41*d5c9a868SElliott Hughes next_no_step = fatDecode(This,fat);
42*d5c9a868SElliott Hughes /* mark current cluster as empty */
43*d5c9a868SElliott Hughes fatDeallocate(This,fat);
44*d5c9a868SElliott Hughes if (next_no_step >= This->last_fat)
45*d5c9a868SElliott Hughes break;
46*d5c9a868SElliott Hughes fat = next_no_step;
47*d5c9a868SElliott Hughes }
48*d5c9a868SElliott Hughes return(0);
49*d5c9a868SElliott Hughes }
50*d5c9a868SElliott Hughes
fatFreeWithDir(Stream_t * Dir,struct directory * dir)51*d5c9a868SElliott Hughes int fatFreeWithDir(Stream_t *Dir, struct directory *dir)
52*d5c9a868SElliott Hughes {
53*d5c9a868SElliott Hughes uint32_t first;
54*d5c9a868SElliott Hughes
55*d5c9a868SElliott Hughes if((!strncmp(dir->name,". ",8) ||
56*d5c9a868SElliott Hughes !strncmp(dir->name,".. ",8)) &&
57*d5c9a868SElliott Hughes !strncmp(dir->ext," ",3)) {
58*d5c9a868SElliott Hughes fprintf(stderr,"Trying to remove . or .. entry\n");
59*d5c9a868SElliott Hughes return -1;
60*d5c9a868SElliott Hughes }
61*d5c9a868SElliott Hughes
62*d5c9a868SElliott Hughes first = START(dir);
63*d5c9a868SElliott Hughes if(fat32RootCluster(Dir))
64*d5c9a868SElliott Hughes first |= (uint32_t) STARTHI(dir) << 16;
65*d5c9a868SElliott Hughes return fat_free(Dir, first);
66*d5c9a868SElliott Hughes }
67*d5c9a868SElliott Hughes
fatFreeWithDirentry(direntry_t * entry)68*d5c9a868SElliott Hughes int fatFreeWithDirentry(direntry_t *entry)
69*d5c9a868SElliott Hughes {
70*d5c9a868SElliott Hughes return fatFreeWithDir(entry->Dir, &entry->dir);
71*d5c9a868SElliott Hughes }
72