1 /*
2 libparted
3 Copyright (C) 1998, 1999, 2000, 2007 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 "fat.h"
21 #include "fatio.h"
22
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <sys/types.h>
26 #include <sys/stat.h>
27 #include <fcntl.h>
28 #include <errno.h>
29 #include <ctype.h>
30
31 #ifndef DISCOVER_ONLY
32
33 int
fat_read_fragments(PedFileSystem * fs,char * buf,FatFragment frag,FatFragment count)34 fat_read_fragments (PedFileSystem* fs, char* buf, FatFragment frag,
35 FatFragment count)
36 {
37 FatSpecific* fs_info = FAT_SPECIFIC (fs);
38 PedSector sector = fat_frag_to_sector (fs, frag);
39 PedSector sector_count = count * fs_info->frag_sectors;
40
41 PED_ASSERT (frag >= 0 && frag < fs_info->frag_count, return 0);
42
43 return ped_geometry_read (fs->geom, buf, sector, sector_count);
44 }
45
46 int
fat_read_fragment(PedFileSystem * fs,char * buf,FatFragment frag)47 fat_read_fragment (PedFileSystem* fs, char* buf, FatFragment frag)
48 {
49 return fat_read_fragments (fs, buf, frag, 1);
50 }
51
52 int
fat_write_fragments(PedFileSystem * fs,char * buf,FatFragment frag,FatFragment count)53 fat_write_fragments (PedFileSystem* fs, char* buf, FatFragment frag,
54 FatFragment count)
55 {
56 FatSpecific* fs_info = FAT_SPECIFIC (fs);
57 PedSector sector = fat_frag_to_sector (fs, frag);
58 PedSector sector_count = count * fs_info->frag_sectors;
59
60 PED_ASSERT (frag >= 0 && frag < fs_info->frag_count, return 0);
61
62 return ped_geometry_write (fs->geom, buf, sector, sector_count);
63 }
64
65 int
fat_write_fragment(PedFileSystem * fs,char * buf,FatFragment frag)66 fat_write_fragment (PedFileSystem* fs, char* buf, FatFragment frag)
67 {
68 return fat_write_fragments (fs, buf, frag, 1);
69 }
70
71 int
fat_write_sync_fragments(PedFileSystem * fs,char * buf,FatFragment frag,FatFragment count)72 fat_write_sync_fragments (PedFileSystem* fs, char* buf, FatFragment frag,
73 FatFragment count)
74 {
75 if (!fat_write_fragments (fs, buf, frag, count))
76 return 0;
77 if (!ped_geometry_sync (fs->geom))
78 return 0;
79 return 1;
80 }
81
82 int
fat_write_sync_fragment(PedFileSystem * fs,char * buf,FatFragment frag)83 fat_write_sync_fragment (PedFileSystem* fs, char* buf, FatFragment frag)
84 {
85 return fat_write_sync_fragments (fs, buf, frag, 1);
86 }
87
88 int
fat_read_clusters(PedFileSystem * fs,char * buf,FatCluster cluster,FatCluster count)89 fat_read_clusters (PedFileSystem* fs, char *buf, FatCluster cluster,
90 FatCluster count)
91 {
92 FatSpecific* fs_info = FAT_SPECIFIC (fs);
93 PedSector sector = fat_cluster_to_sector (fs, cluster);
94 PedSector sector_count = count * fs_info->cluster_sectors;
95
96 PED_ASSERT (cluster >= 2
97 && cluster + count - 1 < fs_info->cluster_count + 2,
98 return 0);
99
100 return ped_geometry_read (fs->geom, buf, sector, sector_count);
101 }
102
103 int
fat_read_cluster(PedFileSystem * fs,char * buf,FatCluster cluster)104 fat_read_cluster (PedFileSystem* fs, char *buf, FatCluster cluster)
105 {
106 return fat_read_clusters (fs, buf, cluster, 1);
107 }
108
109 int
fat_write_clusters(PedFileSystem * fs,char * buf,FatCluster cluster,FatCluster count)110 fat_write_clusters (PedFileSystem* fs, char *buf, FatCluster cluster,
111 FatCluster count)
112 {
113 FatSpecific* fs_info = FAT_SPECIFIC (fs);
114 PedSector sector = fat_cluster_to_sector (fs, cluster);
115 PedSector sector_count = count * fs_info->cluster_sectors;
116
117 PED_ASSERT (cluster >= 2
118 && cluster + count - 1 < fs_info->cluster_count + 2,
119 return 0);
120
121 return ped_geometry_write (fs->geom, buf, sector, sector_count);
122 }
123
124 int
fat_write_cluster(PedFileSystem * fs,char * buf,FatCluster cluster)125 fat_write_cluster (PedFileSystem* fs, char *buf, FatCluster cluster)
126 {
127 return fat_write_clusters (fs, buf, cluster, 1);
128 }
129
130 int
fat_write_sync_clusters(PedFileSystem * fs,char * buf,FatCluster cluster,FatCluster count)131 fat_write_sync_clusters (PedFileSystem* fs, char *buf, FatCluster cluster,
132 FatCluster count)
133 {
134 if (!fat_write_clusters (fs, buf, cluster, count))
135 return 0;
136 if (!ped_geometry_sync (fs->geom))
137 return 0;
138 return 1;
139 }
140
141 int
fat_write_sync_cluster(PedFileSystem * fs,char * buf,FatCluster cluster)142 fat_write_sync_cluster (PedFileSystem* fs, char *buf, FatCluster cluster)
143 {
144 if (!fat_write_cluster (fs, buf, cluster))
145 return 0;
146 if (!ped_geometry_sync (fs->geom))
147 return 0;
148 return 1;
149 }
150
151 #endif /* !DISCOVER_ONLY */
152