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 "msdos.h"
21#include "fsP.h"
22#include "mtoolsDirentry.h"
23
24/*
25 * Remove a string of FAT entries (delete the file).  The argument is
26 * the beginning of the string.  Does not consider the file length, so
27 * if FAT is corrupted, watch out!
28 */
29
30int fat_free(Stream_t *Dir, unsigned int fat)
31{
32	Stream_t *Stream = GetFs(Dir);
33	DeclareThis(Fs_t);
34	unsigned int next_no_step;
35					/* a zero length file? */
36	if (fat == 0)
37		return(0);
38	/* CONSTCOND */
39	while (!This->fat_error) {
40		/* get next cluster number */
41		next_no_step = fatDecode(This,fat);
42		/* mark current cluster as empty */
43		fatDeallocate(This,fat);
44		if (next_no_step >= This->last_fat)
45			break;
46		fat = next_no_step;
47	}
48	return(0);
49}
50
51int fatFreeWithDir(Stream_t *Dir, struct directory *dir)
52{
53	unsigned int first;
54
55	if((!strncmp(dir->name,".      ",8) ||
56	    !strncmp(dir->name,"..     ",8)) &&
57	   !strncmp(dir->ext,"   ",3)) {
58		fprintf(stderr,"Trying to remove . or .. entry\n");
59		return -1;
60	}
61
62	first = START(dir);
63  	if(fat32RootCluster(Dir))
64		first |= STARTHI(dir) << 16;
65	return fat_free(Dir, first);
66}
67
68int fatFreeWithDirentry(direntry_t *entry)
69{
70	return fatFreeWithDir(entry->Dir, &entry->dir);
71}
72
73