1
10
13
14
20
21
22
23
24
25
26
27
28
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
101
102
103
104
106
107
108
109
110
116
117
118
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
214
215
216
217
218
219
220
221
223
224
225
226
227
228
229
230
231
232
233
234
235
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
280
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
328
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
368
369
370
371
372
373
374
375
376
377
...
...
...
#define FX_SOURCE_CODE
#include "fx_api.h"
#include "fx_system.h"
#include "fx_directory.h"
#include "fx_file.h"
#include "fx_utility.h"
5 includes#ifdef FX_ENABLE_FAULT_TOLERANT
#include "fx_fault_tolerant.h"
#endif
...
...
UINT _fx_file_extended_truncate(FX_FILE *file_ptr, ULONG64 size)
{
UINT status;
ULONG cluster;
ULONG contents = 0;
ULONG bytes_per_cluster;
ULONG last_cluster;
ULONG cluster_count;
ULONG64 bytes_remaining;
FX_MEDIA *media_ptr;
#ifndef FX_DONT_UPDATE_OPEN_FILES
ULONG open_count;
FX_FILE *search_ptr;/* ... */
#endif
#ifdef TX_ENABLE_EVENT_TRACE
TX_TRACE_BUFFER_ENTRY *trace_event;
ULONG trace_timestamp;/* ... */
#endif
if (file_ptr -> fx_file_id != FX_FILE_ID)
{
return(FX_NOT_OPEN);
}if (file_ptr -> fx_file_id != FX_FILE_ID) { ... }
#ifndef FX_MEDIA_STATISTICS_DISABLE
media_ptr = file_ptr -> fx_file_media_ptr;
media_ptr -> fx_media_file_truncates++;/* ... */
#endif
media_ptr = file_ptr -> fx_file_media_ptr;
FX_TRACE_IN_LINE_INSERT(FX_TRACE_FILE_TRUNCATE, file_ptr, size, file_ptr -> fx_file_current_file_size, 0, FX_TRACE_FILE_EVENTS, &trace_event, &trace_timestamp)
FX_PROTECT
if (file_ptr -> fx_file_open_mode != FX_OPEN_FOR_WRITE)
{
FX_UNPROTECT
/* ... */
return(FX_ACCESS_ERROR);
}if (file_ptr -> fx_file_open_mode != FX_OPEN_FOR_WRITE) { ... }
if (media_ptr -> fx_media_driver_write_protect)
{
FX_UNPROTECT
return(FX_WRITE_PROTECT);
}if (media_ptr -> fx_media_driver_write_protect) { ... }
if (size < file_ptr -> fx_file_current_file_size)
{
file_ptr -> fx_file_current_file_size = size;
file_ptr -> fx_file_modified = FX_TRUE;
FX_TRACE_EVENT_UPDATE(trace_event, trace_timestamp, FX_TRACE_FILE_TRUNCATE, 0, 0, 0, size)
}if (size < file_ptr -> fx_file_current_file_size) { ... }
else
{
FX_TRACE_EVENT_UPDATE(trace_event, trace_timestamp, FX_TRACE_FILE_TRUNCATE, 0, 0, 0, file_ptr -> fx_file_current_file_size)
FX_UNPROTECT
return(FX_SUCCESS);
}else { ... }
#ifndef FX_DONT_UPDATE_OPEN_FILES
open_count = media_ptr -> fx_media_opened_file_count;
search_ptr = media_ptr -> fx_media_opened_file_list;
while (open_count)
{
if ((search_ptr != file_ptr) &&
(search_ptr -> fx_file_dir_entry.fx_dir_entry_log_sector ==
file_ptr -> fx_file_dir_entry.fx_dir_entry_log_sector) &&
(search_ptr -> fx_file_dir_entry.fx_dir_entry_byte_offset ==
file_ptr -> fx_file_dir_entry.fx_dir_entry_byte_offset))
{
search_ptr -> fx_file_current_file_size = size;
search_ptr -> fx_file_dir_entry.fx_dir_entry_file_size = size;
}if ((search_ptr != file_ptr) && (search_ptr -> fx_file_dir_entry.fx_dir_entry_log_sector == file_ptr -> fx_file_dir_entry.fx_dir_entry_log_sector) && (search_ptr -> fx_file_dir_entry.fx_dir_entry_byte_offset == file_ptr -> fx_file_dir_entry.fx_dir_entry_byte_offset)) { ... }
search_ptr = search_ptr -> fx_file_opened_next;
open_count--;
}while (open_count) { ... }
/* ... */#endif
/* ... */
if (file_ptr -> fx_file_current_file_offset > file_ptr -> fx_file_current_file_size)
{
bytes_per_cluster = ((ULONG)media_ptr -> fx_media_bytes_per_sector) *
((ULONG)media_ptr -> fx_media_sectors_per_cluster);
/* ... */
cluster = file_ptr -> fx_file_first_physical_cluster;
bytes_remaining = size;
last_cluster = 0;
cluster_count = 0;
while ((cluster >= FX_FAT_ENTRY_START) && (cluster < media_ptr -> fx_media_fat_reserved))
{
cluster_count++;
#ifdef FX_ENABLE_EXFAT
if (file_ptr -> fx_file_dir_entry.fx_dir_entry_dont_use_fat & 1)
{
if (cluster >= file_ptr -> fx_file_last_physical_cluster)
{
contents = FX_LAST_CLUSTER_exFAT;
}if (cluster >= file_ptr -> fx_file_last_physical_cluster) { ... }
else
{
contents = cluster + 1;
}else { ... }
}if (file_ptr -> fx_file_dir_entry.fx_dir_entry_dont_use_fat & 1) { ... }
else
{
#endif
status = _fx_utility_FAT_entry_read(media_ptr, cluster, &contents);
if (status != FX_SUCCESS)
{
FX_UNPROTECT
return(status);
}if (status != FX_SUCCESS) { ... }
#ifdef FX_ENABLE_EXFAT
}else { ... }
#endif
last_cluster = cluster;
cluster = contents;
if (bytes_remaining >= bytes_per_cluster)
{
bytes_remaining = bytes_remaining - bytes_per_cluster;
}if (bytes_remaining >= bytes_per_cluster) { ... }
else
{
break;
}else { ... }
}while ((cluster >= FX_FAT_ENTRY_START) && (cluster < media_ptr -> fx_media_fat_reserved)) { ... }
if (size > (((ULONG64) bytes_per_cluster) * ((ULONG64) cluster_count)))
{
FX_UNPROTECT
return(FX_FILE_CORRUPT);
}if (size > (((ULONG64) bytes_per_cluster) * ((ULONG64) cluster_count))) { ... }
file_ptr -> fx_file_current_physical_cluster = last_cluster;
file_ptr -> fx_file_current_relative_cluster = cluster_count - 1;
file_ptr -> fx_file_current_logical_sector = ((ULONG)media_ptr -> fx_media_data_sector_start) +
(((ULONG64)file_ptr -> fx_file_current_physical_cluster - FX_FAT_ENTRY_START) *
((ULONG)media_ptr -> fx_media_sectors_per_cluster)) +
(bytes_remaining / (ULONG)media_ptr -> fx_media_bytes_per_sector);
file_ptr -> fx_file_current_relative_sector = (UINT)((bytes_remaining / (ULONG)media_ptr -> fx_media_bytes_per_sector));
file_ptr -> fx_file_current_file_offset = size;
file_ptr -> fx_file_current_logical_offset = (ULONG)bytes_remaining % ((ULONG)media_ptr -> fx_media_bytes_per_sector);
}if (file_ptr -> fx_file_current_file_offset > file_ptr -> fx_file_current_file_size) { ... }
#ifdef FX_ENABLE_FAULT_TOLERANT
if (media_ptr -> fx_media_fault_tolerant_enabled)
{
_fx_fault_tolerant_transaction_start(media_ptr);
file_ptr -> fx_file_dir_entry.fx_dir_entry_file_size = file_ptr -> fx_file_current_file_size;
#ifdef FX_ENABLE_EXFAT
if (media_ptr -> fx_media_FAT_type == FX_exFAT)
{
status = _fx_directory_exFAT_entry_write(media_ptr, &(file_ptr -> fx_file_dir_entry), UPDATE_STREAM);
}if (media_ptr -> fx_media_FAT_type == FX_exFAT) { ... }
else
{
#endif
status = _fx_directory_entry_write(media_ptr, &(file_ptr -> fx_file_dir_entry));
#ifdef FX_ENABLE_EXFAT
}else { ... }
#endif
if (status != FX_SUCCESS)
{
FX_FAULT_TOLERANT_TRANSACTION_FAIL(media_ptr);
FX_UNPROTECT
return(status);
}if (status != FX_SUCCESS) { ... }
status = _fx_fault_tolerant_transaction_end(media_ptr);
if (status != FX_SUCCESS)
{
FX_UNPROTECT
return(status);
}if (status != FX_SUCCESS) { ... }
if (size < file_ptr -> fx_file_maximum_size_used)
{
file_ptr -> fx_file_maximum_size_used = size;
}if (size < file_ptr -> fx_file_maximum_size_used) { ... }
}if (media_ptr -> fx_media_fault_tolerant_enabled) { ... }
/* ... */#endif
FX_UNPROTECT
return(FX_SUCCESS);
}{ ... }