Imported Upstream version 4.0.40
[platform/upstream/mtools.git] / fat_free.c
1 /*  Copyright 1986-1992 Emmet P. Gray.
2  *  Copyright 1996-1998,2001,2002,2009 Alain Knaff.
3  *  This file is part of mtools.
4  *
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.
9  *
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.
14  *
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/>.
17  */
18
19 #include "sysincludes.h"
20 #include "fsP.h"
21 #include "mtoolsDirentry.h"
22
23 /*
24  * Remove a string of FAT entries (delete the file).  The argument is
25  * the beginning of the string.  Does not consider the file length, so
26  * if FAT is corrupted, watch out!
27  */
28
29 int fat_free(Stream_t *Dir, unsigned int fat)
30 {
31         Stream_t *Stream = GetFs(Dir);
32         DeclareThis(Fs_t);
33         unsigned int next_no_step;
34                                         /* a zero length file? */
35         if (fat == 0)
36                 return(0);
37         /* CONSTCOND */
38         while (!This->fat_error) {
39                 /* get next cluster number */
40                 next_no_step = fatDecode(This,fat);
41                 /* mark current cluster as empty */
42                 fatDeallocate(This,fat);
43                 if (next_no_step >= This->last_fat)
44                         break;
45                 fat = next_no_step;
46         }
47         return(0);
48 }
49
50 int fatFreeWithDir(Stream_t *Dir, struct directory *dir)
51 {
52         uint32_t first;
53
54         if((!strncmp(dir->name,".      ",8) ||
55             !strncmp(dir->name,"..     ",8)) &&
56            !strncmp(dir->ext,"   ",3)) {
57                 fprintf(stderr,"Trying to remove . or .. entry\n");
58                 return -1;
59         }
60
61         first = START(dir);
62         if(fat32RootCluster(Dir))
63                 first |= (uint32_t) STARTHI(dir) << 16;
64         return fat_free(Dir, first);
65 }
66
67 int fatFreeWithDirentry(direntry_t *entry)
68 {
69         return fatFreeWithDir(entry->Dir, &entry->dir);
70 }