-
Notifications
You must be signed in to change notification settings - Fork 15
/
dtfs.c
1993 lines (1875 loc) · 59.1 KB
/
dtfs.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
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
275
276
277
278
279
280
281
282
283
284
285
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
324
325
326
327
328
329
330
331
332
333
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
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/****************************************************************************
* *
* COPYRIGHT (c) 1988 - 2021 *
* This Software Provided *
* By *
* Robin's Nest Software Inc. *
* *
* Permission to use, copy, modify, distribute and sell this software and *
* its documentation for any purpose and without fee is hereby granted, *
* provided that the above copyright notice appear in all copies and that *
* both that copyright notice and this permission notice appear in the *
* supporting documentation, and that the name of the author not be used *
* in advertising or publicity pertaining to distribution of the software *
* without specific, written prior permission. *
* *
* THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, *
* INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN *
* NO EVENT SHALL HE BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL *
* DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR *
* PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS *
* ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF *
* THIS SOFTWARE. *
* *
****************************************************************************/
/*
* Module: dtfs.c
* Author: Robin T. Miller
* Date: July 15th, 2013
*
* Description:
* File system operations.
*
* Modification History:
*
* May 5th, 2015 by Robin T. Miller
* When checking for file/path existance, on Windows expect an
* additional error (ERROR_PATH_NOT_FOUND) when a portion of the path
* does NOT exist. POSIX uses the same error (ENOENT) for both.
*
* March 17th, 2015 by Robin T. Miller
* Add a deleting files flag, so I/O monitoring does NOT cancel our
* thread, if this cleanup takes longer than the term wait time.
*
* February 5th, 2015 by Robin T. Miller
* Added dt_lock_unlock() for file locking support.
*
* January 27th, 2015 by Robin T. Miller
* Fixed a bug in process_next_subdir() where the status variable was
* NOT initialized, which prevented us from processing all subdirectories!
* My regression tests missed this, since it only occurred when optimized.
*
* May 7th, 2014 by Robin T. Miller
* Enhanced reopen_after_disconnect() to use new parameters added to
* error information structure.
*
* July 15th, 2013 by Robin T Miller
* Moving file system related functions here.
*/
#include "dt.h"
#if !defined(WIN32)
# if defined(_QNX_SOURCE)
# include <fcntl.h>
# else /* !defined(_QNX_SOURCE) */
# include <sys/file.h>
# endif /* defined(_QNX_SOURCE) */
#endif /* !defined(WIN32) */
/*
* Forward References:
*/
/*
* isFsFullOk() - Checks for File System Full Condition.
*
* Inputs:
* dip = The device information pointer.
* op = The operation message.
* path = The file path (directory or file name).
*
* Return Value:
* Returns True/False = File System Full / Not FS Full Condition.
* Actually only returns True when processing multiple files!
*/
hbool_t
isFsFullOk(struct dinfo *dip, char *op, char *path)
{
/*
* The limit checks control whether file system full is acceptable.
*
* Note: Disk full conditions should only occur on write operations.
*/
int error = os_get_error();
/* Note: We don't set FS full flag here, since we are used by non-write operations! */
if ( os_isDiskFull(error) ) {
if (dip->di_verbose_flag) {
char *disk_full_msg = os_getDiskFullMsg(error);
Wprintf(dip, " File path: %s\n", path);
Wprintf(dip, " Operation: %s failed, error %d - %s\n", op, error, disk_full_msg);
Wprintf(dip, "Statistics: file #%lu, record #%lu, %s " FUF " file bytes, " FUF " total bytes\n",
(dip->di_files_written + 1), (dip->di_records_written + 1), "wrote",
dip->di_fbytes_written, dip->di_dbytes_written);
} else if (dip->di_debug_flag || dip->di_fDebugFlag) {
char *emsg = os_get_error_msg(error);
Printf(dip, "DEBUG: File path: %s\n", path);
Printf(dip, "DEBUG: %s failed, error %d - %s\n", op, error, emsg);
os_free_error_msg(emsg);
}
/* Note: This should not be required anymore, but leaving for now! */
errno = os_mapDiskFullError(error); /* Map to Unix equivalent for caller. */
/* We assume file system full is Ok for multiple files and data written! */
if ( dip->di_multiple_files && dip->di_maxdata_written ) {
dip->di_no_space_left = True;
return(True);
}
}
return (False);
}
/*
* make_dir_filename() - Generate a Directory with File Name.
*
* Description:
* This function is called when a directory is specified without a file
* name, and since some folks like tools that generate their own file names,
* we will append out default file name to suite their desires. ;)
*
* Inputs:
* dip = The device information pointer.
* dirpath = The directory name. (caller must check!)
*
* Return Value:
* Returns a pointer to the new directory with filename.
*/
char *
make_dir_filename(struct dinfo *dip, char *dirpath)
{
char path[PATH_BUFFER_SIZE];
(void)sprintf(path, "%s%c%s", dirpath, dip->di_dir_sep, DEFAULT_DATA_FILE_NAME);
return( strdup(path) );
}
/*
* make_file_name() - Generate a New File Name.
*
* Description:
* A new file anme is created using the directory path (if any),
* and the file number.
*
* Inputs:
* dip = The device information pointer.
*
* Outputs:
* dip->di_dname points to the new file name.
* The prefix is updated if it contains the device name.
*
* Return Value:
* Returns a pointer to the new device name.
*/
char *
make_file_name(struct dinfo *dip)
{
char path[PATH_BUFFER_SIZE];
char *bp = path;
/*
* Construct new file name with directory path and file number appended.
*/
if (dip->di_dirpath || dip->di_subdir) {
/* Start with the top level directory. */
if (dip->di_dirpath) {
bp += sprintf(bp, "%s%c", dip->di_dirpath, dip->di_dir_sep);
}
/* Append subdirectory (if any). */
if (dip->di_subdir) {
bp += sprintf(bp, "%s%c", dip->di_subdir, dip->di_dir_sep);
}
if (dip->di_file_limit) {
(void)sprintf(bp, "%s-%08u",
dip->di_bname, (dip->di_file_number + 1) );
} else {
(void)sprintf(bp, "%s", dip->di_bname);
}
} else {
if (dip->di_file_limit) {
(void)sprintf(path, "%s-%08u", dip->di_bname, (dip->di_file_number + 1) );
} else {
(void)sprintf(path, "%s", dip->di_bname);
}
}
Free(dip, dip->di_dname);
dip->di_dname = strdup(path);
/*
* Update the prefix string, *if* it contains the device name.
*/
if ( (dip->di_fsfile_flag == True) &&
dip->di_prefix_string && strstr(dip->di_prefix_string, "%d") ) {
/* Recreate prefix with new file path and/or name. */
(void)FmtPrefix(dip, dip->di_prefix_string, dip->di_prefix_size);
}
return(dip->di_dname);
}
/*
* end_file_processing() - End of File Processing.
*
* Inputs:
* dip = The device information pointer.
*
* Return Value:
* Returns SUCCESS / FAILURE
*/
int
end_file_processing(struct dinfo *dip)
{
struct dtfuncs *dtf = dip->di_funcs;
int rc, status = SUCCESS;
/*
* Remember: AIO close cancels outstanding IO's!
*/
if (dip->di_mode == WRITE_MODE) {
rc = (*dtf->tf_flush_data)(dip);
if (rc == FAILURE) status = rc;
}
rc = (*dip->di_funcs->tf_close)(dip);
if (rc == FAILURE) status = rc;
rc = do_post_eof_processing(dip);
if (rc == FAILURE) status = rc;
(void)(*dip->di_funcs->tf_end_test)(dip);
return(status);
}
int
do_post_eof_processing(dinfo_t *dip)
{
int status = SUCCESS;
/*
* If file trim is enabled, do during writing if read-after-write was
* enabled, or do after reading *if* we previously wrote this file.
*/
if ( (dip->di_io_mode == TEST_MODE) &&
dip->di_fsfile_flag && dip->di_fstrim_flag &&
( ( (dip->di_mode == WRITE_MODE) && (dip->di_raw_flag == True) ) ||
( dip->di_output_file && (dip->di_mode == READ_MODE) ) ) ) {
unsigned long files;
files = (dip->di_mode == READ_MODE) ? dip->di_files_read : dip->di_files_written;
if (dip->di_fstrim_frequency && files) {
if ((files % dip->di_fstrim_frequency) == 0) {
status = do_file_trim(dip);
}
} else {
status = do_file_trim(dip);
}
}
return(status);
}
/*
* process_next_dir() - Process The Next Directory.
*
* Note: Not used yet, but added for future enhancements.
*
* Inputs:
* dip = The device information pointer.
*
* Return Value:
* Returns SUCCESS/FAILURE/WARNING =
* Next Directory Ready / Preparation Failed / No More Dirs.
*/
int
process_next_dir(struct dinfo *dip)
{
char dirpath[PATH_BUFFER_SIZE];
int status;
if (dip->di_user_dir_limit && (++dip->di_dir_number < dip->di_user_dir_limit) ) {
;
} else {
return (WARNING);
}
/*
* Track the max levels reached for cleanup purposes.
*/
if (dip->di_dir_number > dip->di_max_dir_number) {
dip->di_max_dir_number = dip->di_dir_number;
}
/*
* Create the next top level directory.
*/
(void)sprintf(dirpath, "%s-%05u", dip->di_dir, dip->di_dir_number);
if (dip->di_mode == WRITE_MODE) {
hbool_t isDiskFull, isFileExists;
status = dt_create_directory(dip, dirpath, &isDiskFull, &isFileExists, EnableErrors);
if (status == FAILURE) {
if (isFileExists == True) {
status = SUCCESS;
} else {
--dip->di_dir_number;
if (isDiskFull == True) {
status = WARNING;
}
}
}
}
if (status == SUCCESS) {
if (dip->di_dirpath) Free(dip, dip->di_dirpath);
dip->di_dirpath = strdup(dirpath);
}
return(status);
}
/*
* process_next_subdir() - Process The Next Sub-Directory.
*
* Inputs:
* dip = The device information pointer.
*
* Return Value:
* Returns SUCCESS/FAILURE/WARNING =
* Next Directory Ready / Preparation Failed / No More Dirs.
*/
int
process_next_subdir(struct dinfo *dip)
{
char path[PATH_BUFFER_SIZE];
char dirpath[PATH_BUFFER_SIZE];
u_int subdir_number;
hbool_t processing_subdir_flag = False;
int status = SUCCESS;
/*
* Handle both top level directories and subdirectories.
*/
if ( (dip->di_subdir_depth + 1) > dip->di_user_subdir_depth) {
if (dip->di_user_subdir_limit && (dip->di_subdir_number < dip->di_user_subdir_limit) ) {
if (dip->di_subdir) Free(dip, dip->di_subdir);
dip->di_subdir = NULL; /* Starting new subdirectory. */
dip->di_subdir_number++;
if (dip->di_user_subdir_depth) dip->di_subdir_depth = 1;
subdir_number = dip->di_subdir_number;
processing_subdir_flag = True;
} else {
return (WARNING);
}
} else {
if (dip->di_subdir_depth == 0) {
dip->di_subdir_number++;
}
subdir_number = ++dip->di_subdir_depth;
}
/*
* Track the max levels reached for cleanup purposes.
*/
if (dip->di_subdir_number > dip->di_max_subdir_number) {
dip->di_max_subdir_number = dip->di_subdir_number;
}
if (dip->di_subdir_depth > dip->di_max_subdir_depth) {
dip->di_max_subdir_depth = dip->di_subdir_depth;
}
dip->di_file_number = 0;
/*
* Create directory or next subdirectory.
*
* Note: Using short directory names, so we can nest deeper!
*/
if (dip->di_subdir) {
size_t sdir_size = (strlen(dip->di_subdir) + strlen(dip->di_dirprefix) + 10);
if (dip->di_dirpath) sdir_size += strlen(dip->di_dirpath);
sdir_size += strlen(dip->di_bname);
/* Sanity check to avoid buffer overuns! */
if ( sdir_size > PATH_BUFFER_SIZE) {
Printf(dip, "Subdirectory name (%u) is too long for our path buffer (%u)!\n",
sdir_size, PATH_BUFFER_SIZE);
return (FAILURE);
}
(void)sprintf(path, "%s%c%s%u",
dip->di_subdir, dip->di_dir_sep, dip->di_dirprefix, subdir_number);
} else {
(void)sprintf(path, "%s%u", dip->di_dirprefix, subdir_number);
}
if (dip->di_subdir) Free(dip, dip->di_subdir);
dip->di_subdir = strdup(path);
if (dip->di_dirpath) {
(void)sprintf(dirpath, "%s%c%s", dip->di_dirpath, dip->di_dir_sep, dip->di_subdir);
} else {
(void)sprintf(dirpath, "%s", dip->di_subdir);
}
if (dip->di_mode == WRITE_MODE) {
hbool_t isDiskFull, isFileExists;
status = dt_create_directory(dip, dirpath, &isDiskFull, &isFileExists, EnableErrors);
if (status == FAILURE) {
if (isFileExists == True) {
status = SUCCESS;
} else {
if (processing_subdir_flag) {
--dip->di_subdir_number;
} else {
--dip->di_subdir_depth;
}
if (isDiskFull == True) {
char *op = OS_CREATE_DIRECTORY_OP;
/* Report the file system full condition. */
(void)isFsFullOk(dip, op, dirpath);
status = WARNING; /* Caller must handle this error! */
}
}
}
}
return(status);
}
/*
* process_next_file() - Process the next file (with files= option).
*
* Inputs:
* dip = The device information pointer.
*
* Return Value:
* Returns SUCCESS/FAILURE = Next File Ready / Preparation Failed.
* WARNING indicates no more files available to read (multiple files).
*/
int
process_next_file(struct dinfo *dip)
{
int status;
if ( dip->di_output_file && (dip->di_mode == READ_MODE) ) {
/* Reopen a file written during the write pass. */
status = (*dip->di_funcs->tf_reopen_file)(dip, dip->di_oflags);
} else {
status = (*dip->di_funcs->tf_open)(dip, dip->di_oflags);
if (status == WARNING) {
/* Handle writing with multiple files and file system full. */
if ( (dip->di_no_space_left == False) && (dip->di_verbose_flag) ) {
Printf(dip, "Warning: File %s does NOT exist, reading stopped after %u files!\n",
dip->di_dname, dip->di_file_number);
}
return(status);
}
#if !defined(WIN32)
/*
* Note: Special handling here, to avoid excessive errors during cleanup!
* Assumes this is the first file being created in the directory.
*/
if ( (status == FAILURE) && (errno == ENAMETOOLONG) && dip->di_subdir_depth) {
dip->di_subdir_depth--; /* Assume we've gone too deep! */
(void)remove_current_directory(dip);
}
#endif /* !defined(WIN32) */
}
if (status == FAILURE) return (status);
/* Note: This is now done as part of common open initialization. */
#if 0
/*
* Reset counters in preparation for the next file.
*/
if (dip->di_mode == READ_MODE) {
dip->di_fbytes_read = (large_t) 0;
dip->di_records_read = (large_t) 0;
} else {
dip->di_fbytes_written = (large_t) 0;
dip->di_records_written = (large_t) 0;
}
#endif /* 0 */
if (dip->di_user_pattern == False) {
/* Use a different pattern for each file. */
dip->di_pattern = data_patterns[(dip->di_pattern_index + dip->di_file_number) % npatterns];
if (dip->di_pattern_buffer) copy_pattern (dip->di_pattern, dip->di_pattern_buffer);
if (dip->di_debug_flag) {
Printf (dip, "Using data pattern 0x%08x for file number %u\n",
dip->di_pattern, (dip->di_file_number + 1));
}
}
/* This is very important for memory mapped files! */
(void)(*dip->di_funcs->tf_start_test)(dip);
return(status);
}
/*
* create_directory() - Create a Directory.
*
* Inputs:
* dip = The device information pointer.
* dir = Pointer to directory to be created.
*
* Return Value:
* Returns SUCCESS/FAILURE/WARNING
* SUCCESS = Directory created.
* FAILURE = Not a directory or creation failed.
* WARNING = Directory already exists.
*/
int
create_directory(struct dinfo *dip, char *dir)
{
hbool_t is_dir;
int status = WARNING;
/*
* Ensure user specified a directory, or create as necessary.
*/
if ( os_file_information(dir, NULL, &is_dir, NULL) == SUCCESS ) {
if ( is_dir == False ) {
Fprintf(dip, "%s is not a directory!\n", dir);
status = FAILURE;
}
} else {
int rc = SUCCESS;
if (dip->di_debug_flag || dip->di_fDebugFlag) {
Printf(dip, "Creating directory %s...\n", dir);
}
dip->di_retry_count = 0;
do {
ENABLE_NOPROG(dip, MKDIR_OP);
status = os_create_directory(dir, DIR_CREATE_MODE);
DISABLE_NOPROG(dip);
if (status == FAILURE) {
char *op = OS_CREATE_DIRECTORY_OP;
os_error_t error = os_get_error();
INIT_ERROR_INFO(eip, dir, op, MKDIR_OP, NULL, 0, (Offset_t)0, (size_t)0,
error, logLevelError, PRT_SYSLOG, RPT_NOFLAGS);
/* Handle file system full conditions (once). */
if ( os_isDiskFull(error) && (dip->di_retry_count == 0) ) {
(void)isFsFullOk(dip, op, dir); /* Report file system full. */
/* If space becomes available, then retry. */
if ( do_free_space_wait(dip, dip->di_fsfree_retries) ) {
dip->di_retry_count++;
rc = RETRYABLE;
continue;
}
} else {
/* Note: This keeps file system full from being retried! */
if ( isFsFullOk(dip, op, dir) ) return(status);
}
if ( os_isFileExists(error) ) return(WARNING);
rc = ReportRetryableError(dip, eip, "Failed to create directory %s", dir);
}
} while ( (status == FAILURE) && (rc == RETRYABLE) );
}
return(status);
}
/*
* remove_current_directory() - Remove The Current Directory.
*
* Inputs:
* dip = The device information pointer.
*
* Return Value:
* Returns SUCCESS/FAILURE = Directory Removed/Not Removed.
*/
int
remove_current_directory(struct dinfo *dip)
{
char dirpath[PATH_BUFFER_SIZE];
char *bp = dirpath;
*bp = '\0';
if (dip->di_dirpath) {
bp += sprintf(bp, "%s%c", dip->di_dirpath, dip->di_dir_sep);
}
/* Append subdirectory (if any). */
if (dip->di_subdir) {
bp += sprintf(bp, "%s", dip->di_subdir);
}
return( remove_directory(dip, dirpath) );
}
/*
* remove_directory() - Remove a Directory (assumed to be empty)
*
* Inputs:
* dip = The device information pointer.
* dir = Pointer to directory to be removed.
*
* Return Value:
* Returns SUCCESS/FAILURE = Directory Removed / Not Removed.
* Note: Returns success for "file not found" error w/disconnects.
*/
int
remove_directory(struct dinfo *dip, char *dir)
{
int status;
int rc = SUCCESS;
if (dip->di_debug_flag || dip->di_fDebugFlag) {
Printf(dip, "Removing directory %s...\n", dir);
}
dip->di_retry_count = 0;
do {
ENABLE_NOPROG(dip, RMDIR_OP);
status = os_remove_directory(dir);
DISABLE_NOPROG(dip);
if (status == FAILURE) {
os_error_t error = os_get_error();
INIT_ERROR_INFO(eip, dir, OS_REMOVE_DIRECTORY_OP, RMDIR_OP, NULL, 0, (Offset_t)0,
(size_t)0, error, logLevelError, PRT_SYSLOG, RPT_NOFLAGS);
if ( (dip->di_retry_disconnects == True) &&
(dip->di_retry_count > 0) &&
os_isFileNotFound(error) ) {
status = SUCCESS;
break;
}
rc = ReportRetryableError(dip, eip, "Failed to remove directory %s", dir);
}
} while ( (status == FAILURE) && (rc == RETRYABLE) );
return (status);
}
int
setup_directory_info(struct dinfo *dip)
{
int status = SUCCESS;
if (dip->di_dir == NULL) return(status);
/* Format the directory based on user control strings. */
if ( strchr(dip->di_dir, '%') ) {
char *dir = FmtFilePath(dip, dip->di_dir, True);
if (dir) {
Free(dip, dip->di_dir);
dip->di_dir = dir;
}
}
/*
* Setup for multiple directories and/or make the directory unique.
*
* When writing files with multiple processes, make the directory
* name unique, just as we do for regular files.
*/
/* Note: Don't reset, since we may be called twice (e.g. %uuid). */
//dip->di_dir_created = False;
if (dip->di_mode == WRITE_MODE) {
dip->di_dirpath = strdup(dip->di_dir);
dip->di_existing_file = False; /* Assume files don't exist. */
/* Note: Need to add check before creating output files. */
status = create_directory(dip, dip->di_dirpath);
if (status == SUCCESS) {
dip->di_dir_created = True; /* Show we created directory. */
} else if (status == WARNING) {
/* Note (status == WARNING) if the directory already exist!*/
status = SUCCESS;
}
} else {
hbool_t is_dir;
/*
* For input files, the directory better exist!
*/
dip->di_dirpath = strdup(dip->di_dir);
if ( os_file_information(dip->di_dirpath, NULL, &is_dir, NULL) == FAILURE) {
os_perror(dip, "Can't access directory %s", dip->di_dirpath);
} else if (is_dir == False) {
Fprintf(dip, "%s is NOT a directory!\n", dip->di_dirpath);
status = FAILURE;
}
}
return (status);
}
/*
* dt_delete_file() - Delete the specified file with retries.
*
* Inputs:
* dip = The device information pointer.
* file = Pointer to the file to delete.
* errors = The error control flag.
*
* Outputs:
* Returns SUCCESS/FAILURE = File Delete/Not Deleted.
*/
int
dt_delete_file(struct dinfo *dip, char *file, hbool_t errors)
{
int status;
int rc = SUCCESS;
if (dip->di_debug_flag || dip->di_fDebugFlag) {
Printf(dip, "Deleting file %s...\n", file);
}
dip->di_retry_count = 0;
do {
ENABLE_NOPROG(dip, DELETE_OP);
status = os_delete_file(file);
DISABLE_NOPROG(dip);
if (status == FAILURE) {
os_error_t error = os_get_error();
INIT_ERROR_INFO(eip, file, OS_DELETE_FILE_OP, DELETE_OP, NULL, 0, (Offset_t)0, 0,
error, logLevelError, PRT_SYSLOG, RPT_NOFLAGS);
if ( (dip->di_retry_disconnects == True) &&
(dip->di_retry_count > 0) &&
os_isFileNotFound(error) ) {
status = SUCCESS;
break;
}
if (errors == False) eip->ei_rpt_flags |= RPT_NOERRORS;
rc = ReportRetryableError(dip, eip, "Failed to delete file %s", file);
}
} while ( (status == FAILURE) && (rc == RETRYABLE) );
return (status);
}
/*
* delete_files() - Delete All Test Files.
*
* Description:
* This function deletes one or more files, and one or more
* directories, assuming we created them.
*
* Inputs:
* dip = The device information pointer.
* delete_topdir = Boolean to control deleting top level directory.
*
* Return Value:
* Returns SUCCESS/FAILURE = File Deleted/Deletion Failed
*/
int
delete_files(struct dinfo *dip, hbool_t delete_topdir)
{
int error, status = SUCCESS;
dip->di_deleting_flag = True;
if (!dip->di_file_limit && !dip->di_user_dir_limit && !dip->di_user_subdir_limit && !dip->di_user_subdir_depth) {
;
/* Maybe a directory specified! */
//status = dt_delete_file(dip, dip->di_dname);
//return (status); /* Single file! */
} else if (dip->di_file_limit && !dip->di_user_dir_limit && !dip->di_user_subdir_limit && !dip->di_user_subdir_depth) {
if (dip->di_debug_flag || dip->di_fDebugFlag) {
Printf(dip, "Removing up to %u files...\n", dip->di_file_limit);
}
/* Fall through to delete top level files/directory. */
} else if (dip->di_user_subdir_limit && !dip->di_user_subdir_depth) {
char spath[PATH_BUFFER_SIZE];
u_int max_subdir = dip->di_max_subdir_number;
u_int subdir;
if (dip->di_debug_flag || dip->di_fDebugFlag) {
Printf(dip, "Removing %u subdirs...\n", max_subdir);
}
/*
* Delete subdirectory files and directories.
*/
for (subdir = 0; (subdir < max_subdir); ) {
subdir++;
(void)sprintf(spath, "%s%u", dip->di_dirprefix, subdir);
error = delete_subdir_files(dip, spath);
if (error && (status == SUCCESS)) {
status = error;
}
}
/* Fall through to delete top level files/directory. */
} else if (!dip->di_user_subdir_limit && dip->di_user_subdir_depth) {
char spath[PATH_BUFFER_SIZE];
char *bp = spath;
u_int depth;
u_int max_depth = dip->di_max_subdir_depth;
if (dip->di_debug_flag || dip->di_fDebugFlag) {
Printf(dip, "Removing subdirs with depth of %u...\n", max_depth);
}
while (max_depth) {
bp = spath;
/*
* Start at bottom of tree.
*/
for (depth = 0; (depth < max_depth); ) {
depth++;
if (bp == spath) {
bp += sprintf(bp, "%s%u", dip->di_dirprefix, depth);
} else {
bp += sprintf(bp, "%c%s%u", dip->di_dir_sep, dip->di_dirprefix, depth);
}
}
error = delete_subdir_files(dip, spath);
if (error && (status == SUCCESS)) {
status = error;
}
max_depth--;
}
/* Fall through to delete top level files/directory. */
} else {
char spath[PATH_BUFFER_SIZE];
char *bp;
u_int subdir, depth;
u_int max_subdir, max_depth;
max_subdir = dip->di_max_subdir_number;
max_depth = dip->di_max_subdir_depth;
/*
* For each subdirectory, remove all files and directories.
*/
if (dip->di_debug_flag || dip->di_fDebugFlag) {
Printf(dip, "Removing %u subdirs with depth of %u...\n", max_subdir, max_depth);
}
while (max_subdir && max_depth) {
while (max_depth) {
for (subdir=0; (subdir < max_subdir); ) {
bp = spath;
subdir++;
bp += sprintf(bp, "%s%u", dip->di_dirprefix, subdir);
for (depth = 1; (depth < max_depth); ) {
depth++;
bp += sprintf(bp, "%c%s%u", dip->di_dir_sep, dip->di_dirprefix, depth);
}
error = delete_subdir_files(dip, spath);
if (error && (status == SUCCESS)) {
status = error;
}
}
max_depth--;
}
max_subdir--;
}
/* Fall through to delete top level files/directory. */
}
/* Delete the top level files. */
error = delete_subdir_files(dip, NULL);
if (error && (status == SUCCESS)) {
status = error;
}
/* Ok, now delete the directory (if we created it). */
if ( (status == SUCCESS) &&
(dip->di_dir_created && dip->di_dirpath) ) {
error = remove_directory(dip, dip->di_dirpath);
if (error) status = error;
}
/* Ok, now delete the top level directory (if we created it). */
if ( (status == SUCCESS) && (delete_topdir == True) &&
(dip->di_topdir_created && dip->di_topdirpath) ) {
if ( dt_file_exists(dip, dip->di_topdirpath) == True ) {
error = remove_directory(dip, dip->di_topdirpath);
if (error) status = error;
}
}
dip->di_deleting_flag = False;
return (status);
}
/*
* delete_subdir_files() - Delete Subdirectory Files.
*
* Description:
* This function sets up the subdirectory path, then deletes
* one or more files, then the subdirectory itself.
*
* Inputs:
* dip = The device information pointer.
* spath = The subdirectory to remove.
*
* Return Value:
* Returns SUCCESS/FAILURE = File/Directory Deleted/Deletion Failed
*/
int
delete_subdir_files(struct dinfo *dip, char *spath)
{
char dirpath[PATH_BUFFER_SIZE];
char *file;
int status = SUCCESS;
*dirpath = '\0';
if (spath) {
if (dip->di_dirpath) {
(void)sprintf(dirpath, "%s%c%s", dip->di_dirpath, dip->di_dir_sep, spath);
} else {
(void)sprintf(dirpath, "%s", spath);
}
/*
* When terminating prematurely, we may be interrupting the creation
* of the directory or file, so we must check for existance, otherwise
* we'll encounter false failures.
*/
if ( dt_file_exists(dip, dirpath) == False ) {
return (SUCCESS);
}
}
if (dip->di_subdir) {
Free(dip, dip->di_subdir);
dip->di_subdir = NULL;
}
/*
* Allow no subdirectory to setup for top level files.
*/
if (spath) {
dip->di_subdir = strdup(spath);
}
if (dip->di_file_limit == 0) {
file = make_file_name(dip);
if ( dt_file_exists(dip, file) == True ) {
status = dt_delete_file(dip, file, EnableErrors);
}
} else {
int error;
dip->di_file_number = 0;
do {
file = make_file_name(dip);
/*
* All files may not exist if the file system is full,
* or we terminated prematurely (runtime, signal, etc).
*/
if ( dt_file_exists(dip, file) == True ) {
error = dt_delete_file(dip, file, EnableErrors);
if (error && (status == SUCCESS)) {
status = error;
}
} else {
break; /* Try to remove directory below. */
}
} while ( ++dip->di_file_number < dip->di_file_limit );
}
/*
* If all files were removed, delete the subdirectory too!
*/
if ( (status == SUCCESS) && spath) {
status = remove_directory(dip, dirpath);
}
return (status);
}
/*
* dt_file_exists() - Check for file existance.
*
* Inputs:
* dip = The device information pointer.
* file = File name to check for existance.
*
* Return Value:
* Returns True/False = File exists / Does not exist.
*/
hbool_t
dt_file_exists(struct dinfo *dip, char *file)
{
hbool_t exists;
int rc = SUCCESS;
dip->di_retry_count = 0;
do {
ENABLE_NOPROG(dip, GETATTR_OP);
exists = os_file_exists(file);
DISABLE_NOPROG(dip);
if (exists == False) {
os_error_t error = os_get_error();
INIT_ERROR_INFO(eip, file, OS_GET_FILE_ATTR_OP, GETATTR_OP, NULL, 0, (Offset_t)0,
(size_t)0, error, logLevelError, PRT_SYSLOG, RPT_NOFLAGS);
/* Note: We only expect "file/path not found" errors! */
/* These occur with multiple dirs/files and file system full. */
if (os_isFileNotFound(error) == True) break;
/* Note: We run a small chance of callers not expecting this error. */
if (os_isDirectoryNotFound(error) == True) break;
rc = ReportRetryableError(dip, eip, "Failed get attributes for %s", file);
}
} while ( (exists == False) && (rc == RETRYABLE) );
return(exists);
}
/*
* dt_get_file_size() - Get The Current File Size.
*
* Inputs:
* dip = The device information pointer.
* file = Pointer to the file name.
* fd = Pointer to the file handle.
* errors = The error control flag.
*
* Outputs:
* Returns the file size or FAILURE.
*/
large_t
dt_get_file_size(struct dinfo *dip, char *file, HANDLE *fd, hbool_t errors)
{
int rc = SUCCESS;
large_t filesize;
if (dip->di_debug_flag || dip->di_fDebugFlag) {
Printf(dip, "Getting file size for file %s...\n", file);
}
dip->di_retry_count = 0;
do {
ENABLE_NOPROG(dip, GETATTR_OP);
filesize = os_get_file_size(file, *fd);
DISABLE_NOPROG(dip);
if (filesize == (large_t)FAILURE) {
os_error_t error = os_get_error();
INIT_ERROR_INFO(eip, file, OS_GET_FILE_ATTR_OP, GETATTR_OP, fd, 0, (Offset_t)0, 0,
error, logLevelError, PRT_SYSLOG, RPT_NOFLAGS);
if (errors == False) eip->ei_rpt_flags |= RPT_NOERRORS;