OSDN Git Service

9b78ac464cd995f985daa89d15105b9940c0f618
[android-x86/external-parted.git] / libparted / fs / fat / fatio.c
1 /*
2     libparted
3     Copyright (C) 1998-2000, 2007, 2009-2010 Free Software Foundation,
4     Inc.
5
6     This program is free software; you can redistribute it and/or modify
7     it under the terms of the GNU General Public License as published by
8     the Free Software Foundation; either version 3 of the License, or
9     (at your option) any later version.
10
11     This program is distributed in the hope that it will be useful,
12     but WITHOUT ANY WARRANTY; without even the implied warranty of
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14     GNU General Public License for more details.
15
16     You should have received a copy of the GNU General Public License
17     along with this program.  If not, see <http://www.gnu.org/licenses/>.
18 */
19
20 #include <config.h>
21 #include "fat.h"
22 #include "fatio.h"
23
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <sys/types.h>
27 #include <sys/stat.h>
28 #include <fcntl.h>
29 #include <errno.h>
30 #include <ctype.h>
31
32 #ifndef DISCOVER_ONLY
33
34 int
35 fat_read_fragments (PedFileSystem* fs, char* buf, FatFragment frag,
36                     FatFragment count)
37 {
38         FatSpecific*    fs_info = FAT_SPECIFIC (fs);
39         PedSector       sector = fat_frag_to_sector (fs, frag);
40         PedSector       sector_count = count * fs_info->frag_sectors;
41
42         PED_ASSERT (frag >= 0 && frag < fs_info->frag_count, return 0);
43
44         return ped_geometry_read (fs->geom, buf, sector, sector_count);
45 }
46
47 int
48 fat_read_fragment (PedFileSystem* fs, char* buf, FatFragment frag)
49 {
50         return fat_read_fragments (fs, buf, frag, 1);
51 }
52
53 int
54 fat_write_fragments (PedFileSystem* fs, char* buf, FatFragment frag,
55                      FatFragment count)
56 {
57         FatSpecific*    fs_info = FAT_SPECIFIC (fs);
58         PedSector       sector = fat_frag_to_sector (fs, frag);
59         PedSector       sector_count = count * fs_info->frag_sectors;
60
61         PED_ASSERT (frag >= 0 && frag < fs_info->frag_count, return 0);
62
63         return ped_geometry_write (fs->geom, buf, sector, sector_count);
64 }
65
66 int
67 fat_write_fragment (PedFileSystem* fs, char* buf, FatFragment frag)
68 {
69         return fat_write_fragments (fs, buf, frag, 1);
70 }
71
72 int
73 fat_write_sync_fragments (PedFileSystem* fs, char* buf, FatFragment frag,
74                           FatFragment count)
75 {
76         if (!fat_write_fragments (fs, buf, frag, count))
77                 return 0;
78         if (!ped_geometry_sync (fs->geom))
79                 return 0;
80         return 1;
81 }
82
83 int
84 fat_write_sync_fragment (PedFileSystem* fs, char* buf, FatFragment frag)
85 {
86         return fat_write_sync_fragments (fs, buf, frag, 1);
87 }
88
89 int
90 fat_read_clusters (PedFileSystem* fs, char *buf, FatCluster cluster,
91                    FatCluster count)
92 {
93         FatSpecific*    fs_info = FAT_SPECIFIC (fs);
94         PedSector       sector = fat_cluster_to_sector (fs, cluster);
95         PedSector       sector_count = count * fs_info->cluster_sectors;
96
97         PED_ASSERT (cluster >= 2
98                     && cluster + count - 1 < fs_info->cluster_count + 2,
99                     return 0);
100
101         return ped_geometry_read (fs->geom, buf, sector, sector_count);
102 }
103
104 int
105 fat_read_cluster (PedFileSystem* fs, char *buf, FatCluster cluster)
106 {
107         return fat_read_clusters (fs, buf, cluster, 1);
108 }
109
110 int
111 fat_write_clusters (PedFileSystem* fs, char *buf, FatCluster cluster,
112                     FatCluster count)
113 {
114         FatSpecific*    fs_info = FAT_SPECIFIC (fs);
115         PedSector       sector = fat_cluster_to_sector (fs, cluster);
116         PedSector       sector_count = count * fs_info->cluster_sectors;
117
118         PED_ASSERT (cluster >= 2
119                     && cluster + count - 1 < fs_info->cluster_count + 2,
120                     return 0);
121
122         return ped_geometry_write (fs->geom, buf, sector, sector_count);
123 }
124
125 int
126 fat_write_cluster (PedFileSystem* fs, char *buf, FatCluster cluster)
127 {
128         return fat_write_clusters (fs, buf, cluster, 1);
129 }
130
131 int
132 fat_write_sync_clusters (PedFileSystem* fs, char *buf, FatCluster cluster,
133                          FatCluster count)
134 {
135         if (!fat_write_clusters (fs, buf, cluster, count))
136                 return 0;
137         if (!ped_geometry_sync (fs->geom))
138                 return 0;
139         return 1;
140 }
141
142 int
143 fat_write_sync_cluster (PedFileSystem* fs, char *buf, FatCluster cluster)
144 {
145         if (!fat_write_cluster (fs, buf, cluster))
146                 return 0;
147         if (!ped_geometry_sync (fs->geom))
148                 return 0;
149         return 1;
150 }
151
152 #endif /* !DISCOVER_ONLY */