~youscribe/parted/3.1

« back to all changes in this revision

Viewing changes to libparted/fs/fat/fat.c

  • Committer: Guilhem Lettron
  • Date: 2012-10-22 14:37:59 UTC
  • Revision ID: guilhem+ubuntu@lettron.fr-20121022143759-m403kecgz13sknvp
3.1 from tarball

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
    libparted
 
3
    Copyright (C) 1998-2001, 2007-2012 Free Software Foundation, Inc.
 
4
 
 
5
    This program 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
    This program 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 this program.  If not, see <http://www.gnu.org/licenses/>.
 
17
*/
 
18
 
 
19
#include <config.h>
 
20
#include <string.h>
 
21
#include <uuid/uuid.h>
 
22
 
 
23
#include "fat.h"
 
24
 
 
25
PedFileSystem*
 
26
fat_alloc (const PedGeometry* geom)
 
27
{
 
28
        PedFileSystem*          fs;
 
29
 
 
30
        fs = (PedFileSystem*) ped_malloc (sizeof (PedFileSystem));
 
31
        if (!fs)
 
32
                goto error;
 
33
 
 
34
        fs->type_specific = (FatSpecific*) ped_malloc (sizeof (FatSpecific));
 
35
        if (!fs->type_specific)
 
36
                goto error_free_fs;
 
37
 
 
38
        fs->geom = ped_geometry_duplicate (geom);
 
39
        if (!fs->geom)
 
40
                goto error_free_type_specific;
 
41
 
 
42
        fs->checked = 0;
 
43
        return fs;
 
44
 
 
45
error_free_type_specific:
 
46
        free (fs->type_specific);
 
47
error_free_fs:
 
48
        free (fs);
 
49
error:
 
50
        return NULL;
 
51
}
 
52
 
 
53
void
 
54
fat_free (PedFileSystem* fs)
 
55
{
 
56
        ped_geometry_destroy (fs->geom);
 
57
        free (fs->type_specific);
 
58
        free (fs);
 
59
}
 
60
 
 
61
PedGeometry*
 
62
fat_probe (PedGeometry* geom, FatType* fat_type)
 
63
{
 
64
        PedFileSystem*          fs;
 
65
        FatSpecific*            fs_info;
 
66
        PedGeometry*            result;
 
67
 
 
68
        fs = fat_alloc (geom);
 
69
        if (!fs)
 
70
                goto error;
 
71
        fs_info = (FatSpecific*) fs->type_specific;
 
72
 
 
73
        if (!fat_boot_sector_read (&fs_info->boot_sector, geom))
 
74
                goto error_free_fs;
 
75
        if (!fat_boot_sector_analyse (&fs_info->boot_sector, fs))
 
76
                goto error_free_fs;
 
77
 
 
78
        *fat_type = fs_info->fat_type;
 
79
        result = ped_geometry_new (geom->dev, geom->start,
 
80
                                   fs_info->sector_count);
 
81
 
 
82
        fat_free (fs);
 
83
        return result;
 
84
 
 
85
error_free_fs:
 
86
        fat_free (fs);
 
87
error:
 
88
        return NULL;
 
89
}
 
90
 
 
91
PedGeometry*
 
92
fat_probe_fat16 (PedGeometry* geom)
 
93
{
 
94
        FatType         fat_type;
 
95
        PedGeometry*    probed_geom = fat_probe (geom, &fat_type);
 
96
 
 
97
        if (probed_geom) {
 
98
                if (fat_type == FAT_TYPE_FAT16)
 
99
                        return probed_geom;
 
100
                ped_geometry_destroy (probed_geom);
 
101
        }
 
102
        return NULL;
 
103
}
 
104
 
 
105
PedGeometry*
 
106
fat_probe_fat32 (PedGeometry* geom)
 
107
{
 
108
        FatType         fat_type;
 
109
        PedGeometry*    probed_geom = fat_probe (geom, &fat_type);
 
110
 
 
111
        if (probed_geom) {
 
112
                if (fat_type == FAT_TYPE_FAT32)
 
113
                        return probed_geom;
 
114
                ped_geometry_destroy (probed_geom);
 
115
        }
 
116
        return NULL;
 
117
}
 
118
 
 
119
static PedFileSystemOps fat16_ops = {
 
120
        probe:          fat_probe_fat16,
 
121
};
 
122
 
 
123
static PedFileSystemOps fat32_ops = {
 
124
        probe:          fat_probe_fat32,
 
125
};
 
126
 
 
127
#define FAT_BLOCK_SIZES ((int[2]){512, 0})
 
128
 
 
129
PedFileSystemType fat16_type = {
 
130
        next:           NULL,
 
131
        ops:            &fat16_ops,
 
132
        name:           "fat16",
 
133
        block_sizes:    FAT_BLOCK_SIZES
 
134
};
 
135
 
 
136
PedFileSystemType fat32_type = {
 
137
        next:           NULL,
 
138
        ops:            &fat32_ops,
 
139
        name:           "fat32",
 
140
        block_sizes:    FAT_BLOCK_SIZES
 
141
};
 
142
 
 
143
void
 
144
ped_file_system_fat_init ()
 
145
{
 
146
        if (sizeof (FatBootSector) != 512) {
 
147
                ped_exception_throw (PED_EXCEPTION_BUG, PED_EXCEPTION_CANCEL,
 
148
                        _("GNU Parted was miscompiled: the FAT boot sector "
 
149
                        "should be 512 bytes.  FAT support will be disabled."));
 
150
        } else {
 
151
                ped_file_system_type_register (&fat16_type);
 
152
                ped_file_system_type_register (&fat32_type);
 
153
        }
 
154
}
 
155
 
 
156
void
 
157
ped_file_system_fat_done ()
 
158
{
 
159
        ped_file_system_type_unregister (&fat16_type);
 
160
        ped_file_system_type_unregister (&fat32_type);
 
161
}