aboutsummaryrefslogtreecommitdiffstats
path: root/util/system/file.cpp
blob: 9c52ee1d83911b8a988dfae708103de09fbe0f87 (plain) (blame)
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
#include "file.h"
#include "flock.h"
#include "fstat.h"
#include "sysstat.h"
#include "align.h"
#include "info.h" 

#include <array>

#include <util/string/util.h>
#include <util/string/cast.h>
#include <util/string/builder.h>

#include <util/stream/hex.h>
#include <util/stream/format.h>

#include <util/random/random.h>

#include <util/generic/size_literals.h>
#include <util/generic/string.h>
#include <util/generic/ylimits.h>
#include <util/generic/yexception.h>

#include <util/datetime/base.h>

#include <errno.h>

#if defined(_unix_)
    #include <fcntl.h>

    #if defined(_linux_) && (!defined(_android_) || __ANDROID_API__ >= 21) && !defined(FALLOC_FL_KEEP_SIZE)
        #include <linux/falloc.h>
    #endif

    #include <stdlib.h>
    #include <unistd.h>
    #include <sys/mman.h>
#elif defined(_win_)
    #include "winint.h"
    #include "fs_win.h"
    #include <io.h>
#endif

#if defined(_bionic_)
    #include <sys/sendfile.h>
    #define HAVE_POSIX_FADVISE 0
    #define HAVE_SYNC_FILE_RANGE 0
#elif defined(_linux_)
    #include <sys/sendfile.h>
    #define HAVE_POSIX_FADVISE 1
    #define HAVE_SYNC_FILE_RANGE 1
#elif defined(__FreeBSD__) && !defined(WITH_VALGRIND)
    #include <sys/param.h>
    #define HAVE_POSIX_FADVISE (__FreeBSD_version >= 900501)
    #define HAVE_SYNC_FILE_RANGE 0
#else
    #define HAVE_POSIX_FADVISE 0
    #define HAVE_SYNC_FILE_RANGE 0
#endif

static bool IsStupidFlagCombination(EOpenMode oMode) {
    // ForAppend will actually not be applied in the following combinations:
    return (oMode & (CreateAlways | ForAppend)) == (CreateAlways | ForAppend) || (oMode & (TruncExisting | ForAppend)) == (TruncExisting | ForAppend) || (oMode & (CreateNew | ForAppend)) == (CreateNew | ForAppend);
}

TFileHandle::TFileHandle(const TString& fName, EOpenMode oMode) noexcept {
    ui32 fcMode = 0;
    EOpenMode createMode = oMode & MaskCreation;
    Y_VERIFY(!IsStupidFlagCombination(oMode), "oMode %d makes no sense", static_cast<int>(oMode));
    if (!(oMode & MaskRW)) {
        oMode |= RdWr;
    }
    if (!(oMode & AMask)) {
        oMode |= ARW;
    }

#ifdef _win_

    switch (createMode) {
        case OpenExisting:
            fcMode = OPEN_EXISTING;
            break;
        case TruncExisting:
            fcMode = TRUNCATE_EXISTING;
            break;
        case OpenAlways:
            fcMode = OPEN_ALWAYS;
            break;
        case CreateNew:
            fcMode = CREATE_NEW;
            break;
        case CreateAlways:
            fcMode = CREATE_ALWAYS;
            break;
        default:
            abort();
            break;
    }

    ui32 faMode = 0;
    if (oMode & RdOnly) {
        faMode |= GENERIC_READ;
    }
    if (oMode & WrOnly) {
        // WrOnly or RdWr
        faMode |= GENERIC_WRITE;
    }
    if (oMode & ::ForAppend) {
        faMode |= GENERIC_WRITE;
        faMode |= FILE_APPEND_DATA;
        faMode &= ~FILE_WRITE_DATA;
    }

    bool inheritHandle = !(oMode & CloseOnExec);

    ui32 shMode = FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE;

    ui32 attrMode = FILE_ATTRIBUTE_NORMAL;
    if ((createMode == OpenExisting || createMode == OpenAlways) && ((oMode & AMask) == (oMode & AR))) {
        attrMode |= FILE_ATTRIBUTE_READONLY;
    }
    if (oMode & Seq) {
        attrMode |= FILE_FLAG_SEQUENTIAL_SCAN;
    }
    if (oMode & Temp) {
        // we use TTempFile instead of FILE_FLAG_DELETE_ON_CLOSE
        attrMode |= FILE_ATTRIBUTE_TEMPORARY;
    }
    if (oMode & Transient) {
        attrMode |= FILE_FLAG_DELETE_ON_CLOSE;
    }
    if ((oMode & (Direct | DirectAligned)) && (oMode & WrOnly)) {
        // WrOnly or RdWr
        attrMode |= /*FILE_FLAG_NO_BUFFERING |*/ FILE_FLAG_WRITE_THROUGH;
    }

    Fd_ = NFsPrivate::CreateFileWithUtf8Name(fName, faMode, shMode, fcMode, attrMode, inheritHandle);

    if ((oMode & ::ForAppend) && (Fd_ != INVALID_FHANDLE)) {
        ::SetFilePointer(Fd_, 0, 0, FILE_END);
    }

#elif defined(_unix_)

    switch (createMode) {
        case OpenExisting:
            fcMode = 0;
            break;
        case TruncExisting:
            fcMode = O_TRUNC;
            break;
        case OpenAlways:
            fcMode = O_CREAT;
            break;
        case CreateNew:
            fcMode = O_CREAT | O_EXCL;
            break;
        case CreateAlways:
            fcMode = O_CREAT | O_TRUNC;
            break;
        default:
            abort();
            break;
    }

    if ((oMode & RdOnly) && (oMode & WrOnly)) {
        fcMode |= O_RDWR;
    } else if (oMode & RdOnly) {
        fcMode |= O_RDONLY;
    } else if (oMode & WrOnly) {
        fcMode |= O_WRONLY;
    }

    if (oMode & ::ForAppend) {
        fcMode |= O_APPEND;
    }

    if (oMode & CloseOnExec) {
        fcMode |= O_CLOEXEC;
    }

    /* I don't now about this for unix...
    if (oMode & Temp) {
    }
    */
    #if defined(_freebsd_)
    if (oMode & (Direct | DirectAligned)) {
        fcMode |= O_DIRECT;
    }

    if (oMode & Sync) {
        fcMode |= O_SYNC;
    }
    #elif defined(_linux_)
    if (oMode & DirectAligned) {
        /*
         * O_DIRECT in Linux requires aligning request size and buffer address
         * to size of hardware sector (see hw_sector_size or ioctl BLKSSZGET).
         * Usually 512 bytes, but modern hardware works better with 4096 bytes.
         */
        fcMode |= O_DIRECT;
    }
    if (oMode & Sync) {
        fcMode |= O_SYNC;
    }
    #endif

    #if defined(_linux_)
    fcMode |= O_LARGEFILE;
    #endif

    ui32 permMode = 0;
    if (oMode & AXOther) {
        permMode |= S_IXOTH;
    }
    if (oMode & AWOther) {
        permMode |= S_IWOTH;
    }
    if (oMode & AROther) {
        permMode |= S_IROTH;
    }
    if (oMode & AXGroup) {
        permMode |= S_IXGRP;
    }
    if (oMode & AWGroup) {
        permMode |= S_IWGRP;
    }
    if (oMode & ARGroup) {
        permMode |= S_IRGRP;
    }
    if (oMode & AXUser) {
        permMode |= S_IXUSR;
    }
    if (oMode & AWUser) {
        permMode |= S_IWUSR;
    }
    if (oMode & ARUser) {
        permMode |= S_IRUSR;
    }

    do {
        Fd_ = ::open(fName.data(), fcMode, permMode);
    } while (Fd_ == -1 && errno == EINTR);

    #if HAVE_POSIX_FADVISE
    if (Fd_ >= 0) {
        if (oMode & NoReuse) {
            ::posix_fadvise(Fd_, 0, 0, POSIX_FADV_NOREUSE);
        }

        if (oMode & Seq) {
            ::posix_fadvise(Fd_, 0, 0, POSIX_FADV_SEQUENTIAL);
        }

        if (oMode & NoReadAhead) {
            ::posix_fadvise(Fd_, 0, 0, POSIX_FADV_RANDOM);
        }
    }
    #endif

    //temp file
    if (Fd_ >= 0 && (oMode & Transient)) {
        unlink(fName.data());
    }
#else
    #error unsupported platform
#endif
}

bool TFileHandle::Close() noexcept {
    bool isOk = true;
#ifdef _win_
    if (Fd_ != INVALID_FHANDLE) {
        isOk = (::CloseHandle(Fd_) != 0);
    }
    if (!isOk) {
        Y_VERIFY(GetLastError() != ERROR_INVALID_HANDLE,
                 "must not quietly close invalid handle");
    }
#elif defined(_unix_)
    if (Fd_ != INVALID_FHANDLE) {
        isOk = (::close(Fd_) == 0 || errno == EINTR);
    }
    if (!isOk) {
        // Do not quietly close bad descriptor,
        // because often it means double close
        // that is disasterous
        Y_VERIFY(errno != EBADF, "must not quietly close bad descriptor: fd=%d", int(Fd_));
    }
#else
    #error unsupported platform
#endif
    Fd_ = INVALID_FHANDLE;
    return isOk;
}

static inline i64 DoSeek(FHANDLE h, i64 offset, SeekDir origin) noexcept {
    if (h == INVALID_FHANDLE) {
        return -1L;
    }
#if defined(_win_)
    static ui32 dir[] = {FILE_BEGIN, FILE_CURRENT, FILE_END};
    LARGE_INTEGER pos;
    pos.QuadPart = offset;
    pos.LowPart = ::SetFilePointer(h, pos.LowPart, &pos.HighPart, dir[origin]);
    if (pos.LowPart == INVALID_SET_FILE_POINTER && GetLastError() != NO_ERROR) {
        pos.QuadPart = -1;
    }
    return pos.QuadPart;
#elif defined(_unix_)
    static int dir[] = {SEEK_SET, SEEK_CUR, SEEK_END};
    #if defined(_sun_)
    return ::llseek(h, (offset_t)offset, dir[origin]);
    #else
    return ::lseek(h, (off_t)offset, dir[origin]);
    #endif
#else
    #error unsupported platform
#endif
}

i64 TFileHandle::GetPosition() const noexcept {
    return DoSeek(Fd_, 0, sCur);
}

i64 TFileHandle::Seek(i64 offset, SeekDir origin) noexcept {
    return DoSeek(Fd_, offset, origin);
}

i64 TFileHandle::GetLength() const noexcept {
    // XXX: returns error code, but does not set errno
    if (!IsOpen()) {
        return -1L;
    }
    return GetFileLength(Fd_);
}

bool TFileHandle::Resize(i64 length) noexcept {
    if (!IsOpen()) {
        return false;
    }
    i64 currentLength = GetLength();
    if (length == currentLength) {
        return true;
    }
#if defined(_win_)
    i64 currentPosition = GetPosition();
    if (currentPosition == -1L) {
        return false;
    }
    Seek(length, sSet);
    if (!::SetEndOfFile(Fd_)) {
        return false;
    }
    if (currentPosition < length) {
        Seek(currentPosition, sSet);
    }
    return true;
#elif defined(_unix_)
    return (0 == ftruncate(Fd_, (off_t)length));
#else
    #error unsupported platform
#endif
}

bool TFileHandle::Reserve(i64 length) noexcept {
    // FIXME this should reserve disk space with fallocate
    if (!IsOpen()) {
        return false;
    }
    i64 currentLength = GetLength();
    if (length <= currentLength) {
        return true;
    }
    if (!Resize(length)) {
        return false;
    }
#if defined(_win_)
    if (!::SetFileValidData(Fd_, length)) {
        Resize(currentLength);
        return false;
    }
#elif defined(_unix_)
// No way to implement this under FreeBSD. Just do nothing
#else
    #error unsupported platform
#endif
    return true;
}

bool TFileHandle::FallocateNoResize(i64 length) noexcept {
    if (!IsOpen()) {
        return false;
    }
#if defined(_linux_) && (!defined(_android_) || __ANDROID_API__ >= 21)
    return !fallocate(Fd_, FALLOC_FL_KEEP_SIZE, 0, length);
#else
    Y_UNUSED(length);
    return true;
#endif
}

// Pair for FallocateNoResize
bool TFileHandle::ShrinkToFit() noexcept {
    if (!IsOpen()) {
        return false;
    }
#if defined(_linux_) && (!defined(_android_) || __ANDROID_API__ >= 21)
    return !ftruncate(Fd_, (off_t)GetLength());
#else
    return true;
#endif
}

bool TFileHandle::Flush() noexcept {
    if (!IsOpen()) {
        return false;
    }
#if defined(_win_)
    bool ok = ::FlushFileBuffers(Fd_) != 0;
    /*
     * FlushFileBuffers fails if hFile is a handle to the console output.
     * That is because the console output is not buffered.
     * The function returns FALSE, and GetLastError returns ERROR_INVALID_HANDLE.
     */
    return ok || GetLastError() == ERROR_INVALID_HANDLE;
#elif defined(_unix_)
    int ret = ::fsync(Fd_);

    /*
     * Ignore EROFS, EINVAL - fd is bound to a special file
     * (PIPE, FIFO, or socket) which does not support synchronization.
     * Fail in case of EIO, ENOSPC, EDQUOT - data might be lost.
     */
    return ret == 0 || errno == EROFS || errno == EINVAL
    #if defined(_darwin_)
           // ENOTSUP fd does not refer to a vnode
           || errno == ENOTSUP
    #endif
        ;
#else
    #error unsupported platform
#endif
}

bool TFileHandle::FlushData() noexcept {
#if defined(_linux_)
    if (!IsOpen()) {
        return false;
    }

    int ret = ::fdatasync(Fd_);

    // Same loginc in error handling as for fsync above.
    return ret == 0 || errno == EROFS || errno == EINVAL;
#else
    return Flush();
#endif
}

i32 TFileHandle::Read(void* buffer, ui32 byteCount) noexcept {
    // FIXME size and return must be 64-bit
    if (!IsOpen()) {
        return -1;
    }
#if defined(_win_)
    DWORD bytesRead = 0;
    if (::ReadFile(Fd_, buffer, byteCount, &bytesRead, nullptr)) {
        return bytesRead;
    }
    return -1;
#elif defined(_unix_)
    i32 ret;
    do {
        ret = ::read(Fd_, buffer, byteCount);
    } while (ret == -1 && errno == EINTR);
    return ret;
#else
    #error unsupported platform
#endif
}

i32 TFileHandle::Write(const void* buffer, ui32 byteCount) noexcept {
    if (!IsOpen()) {
        return -1;
    }
#if defined(_win_)
    DWORD bytesWritten = 0;
    if (::WriteFile(Fd_, buffer, byteCount, &bytesWritten, nullptr)) {
        return bytesWritten;
    }
    return -1;
#elif defined(_unix_)
    i32 ret;
    do {
        ret = ::write(Fd_, buffer, byteCount);
    } while (ret == -1 && errno == EINTR);
    return ret;
#else
    #error unsupported platform
#endif
}

i32 TFileHandle::Pread(void* buffer, ui32 byteCount, i64 offset) const noexcept {
#if defined(_win_)
    OVERLAPPED io;
    Zero(io);
    DWORD bytesRead = 0;
    io.Offset = (ui32)offset;
    io.OffsetHigh = (ui32)(offset >> 32);
    if (::ReadFile(Fd_, buffer, byteCount, &bytesRead, &io)) {
        return bytesRead;
    }
    if (::GetLastError() == ERROR_HANDLE_EOF) {
        return 0;
    }
    return -1;
#elif defined(_unix_)
    i32 ret;
    do {
        ret = ::pread(Fd_, buffer, byteCount, offset);
    } while (ret == -1 && errno == EINTR);
    return ret;
#else
    #error unsupported platform
#endif
}

i32 TFileHandle::Pwrite(const void* buffer, ui32 byteCount, i64 offset) const noexcept {
#if defined(_win_)
    OVERLAPPED io;
    Zero(io);
    DWORD bytesWritten = 0;
    io.Offset = (ui32)offset;
    io.OffsetHigh = (ui32)(offset >> 32);
    if (::WriteFile(Fd_, buffer, byteCount, &bytesWritten, &io)) {
        return bytesWritten;
    }
    return -1;
#elif defined(_unix_)
    i32 ret;
    do {
        ret = ::pwrite(Fd_, buffer, byteCount, offset);
    } while (ret == -1 && errno == EINTR);
    return ret;
#else
    #error unsupported platform
#endif
}

FHANDLE TFileHandle::Duplicate() const noexcept {
    if (!IsOpen()) {
        return INVALID_FHANDLE;
    }
#if defined(_win_)
    FHANDLE dupHandle;
    if (!::DuplicateHandle(GetCurrentProcess(), Fd_, GetCurrentProcess(), &dupHandle, 0, TRUE, DUPLICATE_SAME_ACCESS)) {
        return INVALID_FHANDLE;
    }
    return dupHandle;
#elif defined(_unix_)
    return ::dup(Fd_);
#else
    #error unsupported platform
#endif
}

int TFileHandle::Duplicate2Posix(int dstHandle) const noexcept {
    if (!IsOpen()) {
        return -1;
    }
#if defined(_win_)
    FHANDLE dupHandle = Duplicate();
    if (dupHandle == INVALID_FHANDLE) {
        _set_errno(EMFILE);
        return -1;
    }
    int posixHandle = _open_osfhandle((intptr_t)dupHandle, 0);
    if (posixHandle == -1) {
        CloseHandle(dupHandle);
        return -1;
    }
    if (dup2(posixHandle, dstHandle) == -1) {
        dstHandle = -1;
    }
    _close(posixHandle);
    return dstHandle;
#elif defined(_unix_)
    while (dup2(Fd_, dstHandle) == -1) {
        if (errno != EINTR) {
            return -1;
        }
    }
    return dstHandle;
#else
    #error unsupported platform
#endif
}

bool TFileHandle::LinkTo(const TFileHandle& fh) const noexcept {
#if defined(_unix_)
    while (dup2(fh.Fd_, Fd_) == -1) {
        if (errno != EINTR) {
            return false;
        }
    }
    return true;
#elif defined(_win_)
    TFileHandle nh(fh.Duplicate());

    if (!nh.IsOpen()) {
        return false;
    }

    //not thread-safe
    nh.Swap(*const_cast<TFileHandle*>(this));

    return true;
#else
    #error unsupported
#endif
}

int TFileHandle::Flock(int op) noexcept {
    return ::Flock(Fd_, op);
}

bool TFileHandle::SetDirect() {
#ifdef _linux_
    const long flags = fcntl(Fd_, F_GETFL);
    const int r = fcntl(Fd_, F_SETFL, flags | O_DIRECT);

    return !r;
#endif

    return false;
}

void TFileHandle::ResetDirect() {
#ifdef _linux_
    long flags = fcntl(Fd_, F_GETFL);
    fcntl(Fd_, F_SETFL, flags & ~O_DIRECT);
#endif
}

i64 TFileHandle::CountCache(i64 offset, i64 length) const noexcept {
#ifdef _linux_
    const i64 pageSize = NSystemInfo::GetPageSize();
    constexpr size_t vecSize = 512; // Fetch up to 2MiB at once
    const i64 batchSize = vecSize * pageSize;
    std::array<ui8, vecSize> vec;
    void* ptr = nullptr;
    i64 res = 0;

    if (!IsOpen()) {
        return -1;
    }

    if (!length) {
        length = GetLength();
        length -= Min(length, offset);
    }

    if (!length) {
        return 0;
    }

    const i64 begin = AlignDown(offset, pageSize);
    const i64 end = AlignUp(offset + length, pageSize);
    const i64 size = end - begin;

    /*
     * Since fincode is not implemented yet use mmap and mincore.
     * This is not so effective and scalable for frequent usage.
     */
    ptr = ::mmap(
        (caddr_t) nullptr,
        size,
        PROT_READ,
        MAP_SHARED | MAP_NORESERVE,
        Fd_,
        begin);
    if (MAP_FAILED == ptr) {
        return -1;
    }

    for (i64 base = begin; base < end; base += batchSize) {
        const size_t batch = Min(vecSize, size_t((end - base) / pageSize));
        void* batchPtr = static_cast<caddr_t>(ptr) + (base - begin);

        if (::mincore(batchPtr, batch * pageSize, vec.data())) {
            res = -1;
            break;
        }

        for (size_t i = 0; i < batch; i++) {
            // count uptodate complete pages in cache
            if (vec[i] & 1) {
                res += pageSize;
            }
        }

        if (base == begin && (vec[0] & 1)) {
            // cut head of first page
            res -= offset - begin;
        }

        if ((end - base) <= batchSize && (vec[batch - 1] & 1)) {
            // cut tail of last page
            res -= size - (offset - begin) - length;
        }
    }

    ::munmap(ptr, size);

    return res;
#else
    Y_UNUSED(offset);
    Y_UNUSED(length);
    return -1;
#endif
}

void TFileHandle::PrefetchCache(i64 offset, i64 length, bool wait) const noexcept {
#ifdef _linux_
    #if HAVE_POSIX_FADVISE
    // POSIX_FADV_WILLNEED starts reading upto read_ahead_kb in background
    ::posix_fadvise(Fd_, offset, length, POSIX_FADV_WILLNEED);
    #endif

    if (wait) {
        TFileHandle devnull("/dev/null", OpenExisting | WrOnly | CloseOnExec);
        off_t end = length ? (offset + length) : GetLength();
        off_t pos = offset;
        ssize_t ret;

        do {
            ret = ::sendfile((FHANDLE)devnull, Fd_, &pos, end - pos);
        } while (pos < end && (ret > 0 || errno == EINTR));
    }
#else
    Y_UNUSED(offset);
    Y_UNUSED(length);
    Y_UNUSED(wait);
#endif
}

void TFileHandle::EvictCache(i64 offset, i64 length) const noexcept {
#if HAVE_POSIX_FADVISE
    /*
     * This tries to evicts only unmaped, clean, complete pages.
     */
    ::posix_fadvise(Fd_, offset, length, POSIX_FADV_DONTNEED);
#else
    Y_UNUSED(offset);
    Y_UNUSED(length);
#endif
}

bool TFileHandle::FlushCache(i64 offset, i64 length, bool wait) noexcept {
#if HAVE_SYNC_FILE_RANGE
    int flags = SYNC_FILE_RANGE_WRITE;
    if (wait) {
        flags |= SYNC_FILE_RANGE_WAIT_AFTER;
    }
    int ret = ::sync_file_range(Fd_, offset, length, flags);
    return ret == 0 || errno == EROFS;
#else
    Y_UNUSED(offset);
    Y_UNUSED(length);
    if (wait) {
        return FlushData();
    }
    return true;
#endif
}

TString DecodeOpenMode(ui32 mode0) {
    ui32 mode = mode0;

    TStringBuilder r;

#define F(flag)                   \
    if ((mode & flag) == flag) {  \
        mode &= ~flag;            \
        if (r) {                  \
            r << TStringBuf("|"); \
        }                         \
        r << TStringBuf(#flag);   \
    }

    F(RdWr)
    F(RdOnly)
    F(WrOnly)

    F(CreateAlways)
    F(CreateNew)
    F(OpenAlways)
    F(TruncExisting)
    F(ForAppend)
    F(Transient)
    F(CloseOnExec)

    F(Temp)
    F(Sync)
    F(Direct)
    F(DirectAligned)
    F(Seq)
    F(NoReuse)
    F(NoReadAhead)

    F(AX)
    F(AR)
    F(AW)
    F(ARW)

    F(AXOther)
    F(AWOther)
    F(AROther)
    F(AXGroup)
    F(AWGroup)
    F(ARGroup)
    F(AXUser)
    F(AWUser)
    F(ARUser)

#undef F

    if (mode != 0) {
        if (r) {
            r << TStringBuf("|");
        }

        r << Hex(mode);
    }

    if (!r) {
        return "0";
    }

    return r;
}

class TFile::TImpl: public TAtomicRefCount<TImpl> {
public:
    inline TImpl(FHANDLE fd, const TString& fname = TString())
        : Handle_(fd)
        , FileName_(fname)
    {
    }

    inline TImpl(const TString& fName, EOpenMode oMode)
        : Handle_(fName, oMode)
        , FileName_(fName)
    {
        if (!Handle_.IsOpen()) {
            ythrow TFileError() << "can't open " << fName.Quote() << " with mode " << DecodeOpenMode(oMode) << " (" << Hex(oMode.ToBaseType()) << ")";
        }
    }

    inline ~TImpl() = default;

    inline void Close() {
        if (!Handle_.Close()) {
            ythrow TFileError() << "can't close " << FileName_.Quote();
        }
    }

    const TString& GetName() const noexcept {
        return FileName_;
    }

    void SetName(const TString& newName) {
        FileName_ = newName;
    }

    const TFileHandle& GetHandle() const noexcept {
        return Handle_;
    }

    i64 Seek(i64 offset, SeekDir origin) {
        i64 pos = Handle_.Seek(offset, origin);
        if (pos == -1L) {
            ythrow TFileError() << "can't seek " << offset << " bytes in " << FileName_.Quote();
        }
        return pos;
    }

    void Resize(i64 length) {
        if (!Handle_.Resize(length)) {
            ythrow TFileError() << "can't resize " << FileName_.Quote() << " to size " << length;
        }
    }

    void Reserve(i64 length) {
        if (!Handle_.Reserve(length)) {
            ythrow TFileError() << "can't reserve " << length << " for file " << FileName_.Quote();
        }
    }

    void FallocateNoResize(i64 length) {
        if (!Handle_.FallocateNoResize(length)) {
            ythrow TFileError() << "can't allocate " << length << "bytes of space for file " << FileName_.Quote();
        }
    }

    void ShrinkToFit() {
        if (!Handle_.ShrinkToFit()) {
            ythrow TFileError() << "can't shrink " << FileName_.Quote() << " to logical size";
        }
    }

    void Flush() {
        if (!Handle_.Flush()) {
            ythrow TFileError() << "can't flush " << FileName_.Quote();
        }
    }

    void FlushData() {
        if (!Handle_.FlushData()) {
            ythrow TFileError() << "can't flush data " << FileName_.Quote();
        }
    }

    TFile Duplicate() const {
        TFileHandle dupH(Handle_.Duplicate());
        if (!dupH.IsOpen()) {
            ythrow TFileError() << "can't duplicate the handle of " << FileName_.Quote();
        }
        TFile res(dupH);
        dupH.Release();
        return res;
    }

    // Maximum amount of bytes to be read via single system call.
    // Some libraries fail when it is greater than max int.
    // Syscalls can cause contention if they operate on very large data blocks.
    static constexpr size_t MaxReadPortion = 1_GB;

    i32 RawRead(void* bufferIn, size_t numBytes) {
        const size_t toRead = Min(MaxReadPortion, numBytes);
        return Handle_.Read(bufferIn, toRead);
    }

    size_t ReadOrFail(void* buf, size_t numBytes) {
        const i32 reallyRead = RawRead(buf, numBytes);

        if (reallyRead < 0) {
            ythrow TFileError() << "can not read data from " << FileName_.Quote();
        }

        return reallyRead;
    }

    size_t Read(void* bufferIn, size_t numBytes) {
        ui8* buf = (ui8*)bufferIn;

        while (numBytes) {
            const size_t reallyRead = ReadOrFail(buf, numBytes);

            if (reallyRead == 0) {
                // file exhausted
                break;
            }

            buf += reallyRead;
            numBytes -= reallyRead;
        }

        return buf - (ui8*)bufferIn;
    }

    void Load(void* buf, size_t len) {
        if (Read(buf, len) != len) {
            ythrow TFileError() << "can't read " << len << " bytes from " << FileName_.Quote();
        }
    }

    // Maximum amount of bytes to be written via single system call.
    // Some libraries fail when it is greater than max int.
    // Syscalls can cause contention if they operate on very large data blocks.
    static constexpr size_t MaxWritePortion = 1_GB;

    void Write(const void* buffer, size_t numBytes) {
        const ui8* buf = (const ui8*)buffer;

        while (numBytes) {
            const i32 toWrite = (i32)Min(MaxWritePortion, numBytes);
            const i32 reallyWritten = Handle_.Write(buf, toWrite);

            if (reallyWritten < 0) {
                ythrow TFileError() << "can't write " << toWrite << " bytes to " << FileName_.Quote();
            }

            buf += reallyWritten;
            numBytes -= reallyWritten;
        }
    }

    size_t Pread(void* bufferIn, size_t numBytes, i64 offset) const {
        ui8* buf = (ui8*)bufferIn;

        while (numBytes) {
            const i32 toRead = (i32)Min(MaxReadPortion, numBytes);
            const i32 reallyRead = RawPread(buf, toRead, offset);

            if (reallyRead < 0) {
                ythrow TFileError() << "can not read data from " << FileName_.Quote();
            }

            if (reallyRead == 0) {
                // file exausted
                break;
            }

            buf += reallyRead;
            offset += reallyRead;
            numBytes -= reallyRead;
        }

        return buf - (ui8*)bufferIn;
    }

    i32 RawPread(void* buf, ui32 len, i64 offset) const {
        return Handle_.Pread(buf, len, offset);
    }

    void Pload(void* buf, size_t len, i64 offset) const {
        if (Pread(buf, len, offset) != len) {
            ythrow TFileError() << "can't read " << len << " bytes at offset " << offset << " from " << FileName_.Quote();
        }
    }

    void Pwrite(const void* buffer, size_t numBytes, i64 offset) const {
        const ui8* buf = (const ui8*)buffer;

        while (numBytes) {
            const i32 toWrite = (i32)Min(MaxWritePortion, numBytes);
            const i32 reallyWritten = Handle_.Pwrite(buf, toWrite, offset);

            if (reallyWritten < 0) {
                ythrow TFileError() << "can't write " << toWrite << " bytes to " << FileName_.Quote();
            }

            buf += reallyWritten;
            offset += reallyWritten;
            numBytes -= reallyWritten;
        }
    }

    void Flock(int op) {
        if (0 != Handle_.Flock(op)) {
            ythrow TFileError() << "can't flock " << FileName_.Quote();
        }
    }

    void SetDirect() {
        if (!Handle_.SetDirect()) {
            ythrow TFileError() << "can't set direct mode for " << FileName_.Quote();
        }
    }

    void ResetDirect() {
        Handle_.ResetDirect();
    }

    i64 CountCache(i64 offset, i64 length) const noexcept {
        return Handle_.CountCache(offset, length);
    }

    void PrefetchCache(i64 offset, i64 length, bool wait) const noexcept {
        Handle_.PrefetchCache(offset, length, wait);
    }

    void EvictCache(i64 offset, i64 length) const noexcept {
        Handle_.EvictCache(offset, length);
    }

    void FlushCache(i64 offset, i64 length, bool wait) {
        if (!Handle_.FlushCache(offset, length, wait)) {
            ythrow TFileError() << "can't flush data " << FileName_.Quote();
        }
    }

private:
    TFileHandle Handle_;
    TString FileName_;
};

TFile::TFile()
    : Impl_(new TImpl(INVALID_FHANDLE))
{
}

TFile::TFile(FHANDLE fd)
    : Impl_(new TImpl(fd))
{
}

TFile::TFile(FHANDLE fd, const TString& name)
    : Impl_(new TImpl(fd, name))
{
}

TFile::TFile(const TString& fName, EOpenMode oMode)
    : Impl_(new TImpl(fName, oMode))
{
}

TFile::~TFile() = default;

void TFile::Close() {
    Impl_->Close();
}

const TString& TFile::GetName() const noexcept {
    return Impl_->GetName();
}

i64 TFile::GetPosition() const noexcept {
    return Impl_->GetHandle().GetPosition();
}

i64 TFile::GetLength() const noexcept {
    return Impl_->GetHandle().GetLength();
}

bool TFile::IsOpen() const noexcept {
    return Impl_->GetHandle().IsOpen();
}

FHANDLE TFile::GetHandle() const noexcept {
    return Impl_->GetHandle();
}

i64 TFile::Seek(i64 offset, SeekDir origin) {
    return Impl_->Seek(offset, origin);
}

void TFile::Resize(i64 length) {
    Impl_->Resize(length);
}

void TFile::Reserve(i64 length) {
    Impl_->Reserve(length);
}

void TFile::FallocateNoResize(i64 length) {
    Impl_->FallocateNoResize(length);
}

void TFile::ShrinkToFit() {
    Impl_->ShrinkToFit();
}

void TFile::Flush() {
    Impl_->Flush();
}

void TFile::FlushData() {
    Impl_->FlushData();
}

TFile TFile::Duplicate() const {
    TFile res = Impl_->Duplicate();
    res.Impl_->SetName(Impl_->GetName());
    return res;
}

size_t TFile::Read(void* buf, size_t len) {
    return Impl_->Read(buf, len);
}

i32 TFile::RawRead(void* buf, size_t len) {
    return Impl_->RawRead(buf, len);
}

size_t TFile::ReadOrFail(void* buf, size_t len) {
    return Impl_->ReadOrFail(buf, len);
}

void TFile::Load(void* buf, size_t len) {
    Impl_->Load(buf, len);
}

void TFile::Write(const void* buf, size_t len) {
    Impl_->Write(buf, len);
}

size_t TFile::Pread(void* buf, size_t len, i64 offset) const {
    return Impl_->Pread(buf, len, offset);
}

i32 TFile::RawPread(void* buf, ui32 len, i64 offset) const {
    return Impl_->RawPread(buf, len, offset);
}

void TFile::Pload(void* buf, size_t len, i64 offset) const {
    Impl_->Pload(buf, len, offset);
}

void TFile::Pwrite(const void* buf, size_t len, i64 offset) const {
    Impl_->Pwrite(buf, len, offset);
}

void TFile::Flock(int op) {
    Impl_->Flock(op);
}

void TFile::SetDirect() {
    Impl_->SetDirect();
}

void TFile::ResetDirect() {
    Impl_->ResetDirect();
}

i64 TFile::CountCache(i64 offset, i64 length) const noexcept {
    return Impl_->CountCache(offset, length);
}

void TFile::PrefetchCache(i64 offset, i64 length, bool wait) const noexcept {
    Impl_->PrefetchCache(offset, length, wait);
}

void TFile::EvictCache(i64 offset, i64 length) const noexcept {
    Impl_->EvictCache(offset, length);
}

void TFile::FlushCache(i64 offset, i64 length, bool wait) {
    Impl_->FlushCache(offset, length, wait);
}

void TFile::LinkTo(const TFile& f) const {
    if (!Impl_->GetHandle().LinkTo(f.Impl_->GetHandle())) {
        ythrow TFileError() << "can not link fd(" << GetName() << " -> " << f.GetName() << ")";
    }
}

TFile TFile::Temporary(const TString& prefix) {
    //TODO - handle impossible case of name collision
    return TFile(prefix + ToString(MicroSeconds()) + "-" + ToString(RandomNumber<ui64>()), CreateNew | RdWr | Seq | Temp | Transient);
}

TFile TFile::ForAppend(const TString& path) {
    return TFile(path, OpenAlways | WrOnly | Seq | ::ForAppend);
}

TFile Duplicate(FILE* f) {
    return Duplicate(fileno(f));
}

TFile Duplicate(int fd) {
#if defined(_win_)
    /* There are two options of how to duplicate a file descriptor on Windows:
     *
     * 1:
     * - Call dup.
     * - Call _get_osfhandle on the result.
     * - Use returned handle.
     * - Call _close on file descriptor returned by dup. This will also close
     *   the handle.
     *
     * 2:
     * - Call _get_osfhandle.
     * - Call DuplicateHandle on the result.
     * - Use returned handle.
     * - Call CloseHandle.
     *
     * TFileHandle calls CloseHandle when destroyed, leaving us with option #2. */
    FHANDLE handle = reinterpret_cast<FHANDLE>(::_get_osfhandle(fd));

    FHANDLE dupHandle;
    if (!::DuplicateHandle(GetCurrentProcess(), handle, GetCurrentProcess(), &dupHandle, 0, TRUE, DUPLICATE_SAME_ACCESS)) {
        ythrow TFileError() << "can not duplicate file descriptor " << LastSystemError() << Endl;
    }

    return TFile(dupHandle);
#elif defined(_unix_)
    return TFile(::dup(fd));
#else
    #error unsupported platform
#endif
}

bool PosixDisableReadAhead(FHANDLE fileHandle, void* addr) noexcept {
    int ret = -1;

#if HAVE_POSIX_FADVISE
    #if defined(_linux_)
    Y_UNUSED(fileHandle);
    ret = madvise(addr, 0, MADV_RANDOM); // according to klamm@ posix_fadvise does not work under linux, madvise does work
    #else
    Y_UNUSED(addr);
    ret = ::posix_fadvise(fileHandle, 0, 0, POSIX_FADV_RANDOM);
    #endif
#else
    Y_UNUSED(fileHandle);
    Y_UNUSED(addr);
#endif
    return ret == 0;
}