forked from Exiv2/exiv2
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathFileIo.cpp
1084 lines (1001 loc) · 35 KB
/
FileIo.cpp
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) 2004-2019 Exiv2 authors
* This program is part of the Exiv2 distribution.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, 5th Floor, Boston, MA 02110-1301 USA.
*/
#include "basicio.hpp"
#include "error.hpp"
#include "futils.hpp"
#include <sys/stat.h> // for stat, chmod
#include <sys/types.h> // for stat, chmod
#ifdef EXV_HAVE_SYS_MMAN_H
#include <sys/mman.h> // for mmap and munmap
#endif
#ifdef EXV_HAVE_UNISTD_H
#include <unistd.h> // for getpid, stat
#endif
// Platform specific headers for handling extended attributes (xattr)
#if defined(__APPLE__)
#include <sys/xattr.h>
#endif
#if defined(__MINGW__) || (defined(WIN32) && !defined(__CYGWIN))
// Windows doesn't provide nlink_t
typedef short nlink_t;
#include <io.h>
#include <windows.h>
#endif
#include <cstdio> // for remove, rename
#include <cassert> /// \todo check usages of assert and try to cover the negative case with unit tests.
#include <fcntl.h> // _O_BINARY in FileIo::FileIo
#include <ctime> // timestamp for the name of temporary file
#include <iostream>
#include <fstream> // write the temporary file
#define mode_t unsigned short
namespace Exiv2
{
//! Internal Pimpl structure of class FileIo.
class FileIo::Impl
{
public:
//! Constructor
explicit Impl(const std::string& path);
#ifdef EXV_UNICODE_PATH
//! Constructor accepting a unicode path in an std::wstring
explicit Impl(const std::wstring& wpath);
#endif
// Enumerations
//! Mode of operation
enum OpMode
{
opRead,
opWrite,
opSeek
};
#ifdef EXV_UNICODE_PATH
//! Used to indicate if the path is stored as a standard or unicode
//! string
enum WpMode
{
wpStandard,
wpUnicode
};
#endif
// DATA
std::string path_; //!< (Standard) path
#ifdef EXV_UNICODE_PATH
std::wstring wpath_; //!< Unicode path
WpMode wpMode_; //!< Indicates which path is in use
#endif
std::string openMode_; //!< File open mode
FILE* fp_{nullptr}; //!< File stream pointer
OpMode opMode_{opSeek}; //!< File open mode
#if defined WIN32 && !defined __CYGWIN__
HANDLE hFile_{nullptr}; //!< Duplicated fd
HANDLE hMap_{nullptr}; //!< Handle from CreateFileMapping
#endif
byte* pMappedArea_{nullptr}; //!< Pointer to the memory-mapped area
size_t mappedLength_{0}; //!< Size of the memory-mapped area
bool isMalloced_{false}; //!< Is the mapped area allocated?
bool isWriteable_{false}; //!< Can the mapped area be written to?
// TYPES
//! Simple struct stat wrapper for internal use
struct StructStat
{
mode_t st_mode{0}; //!< Permissions
off_t st_size{0}; //!< Size
nlink_t st_nlink{0}; //!< Number of hard links (broken on Windows,
};
/*!
@brief Switch to a new access mode, reopening the file if needed.
Optimized to only reopen the file when it is really necessary.
@param opMode The mode to switch to.
@return 0 if successful
*/
int switchMode(OpMode opMode);
//! stat wrapper for internal use
int stat(StructStat& buf) const;
Impl& operator=(const Impl& rhs) = delete;
Impl& operator=(const Impl&& rhs) = delete;
Impl(const Impl& rhs) = delete;
Impl(const Impl&& rhs) = delete;
};
FileIo::Impl::Impl(const std::string& path)
: path_(path)
#ifdef EXV_UNICODE_PATH
, wpMode_(wpStandard)
#endif
{
}
#ifdef EXV_UNICODE_PATH
FileIo::Impl::Impl(const std::wstring& wpath)
: wpath_(wpath)
, wpMode_(wpUnicode)
{
}
#endif
int FileIo::Impl::switchMode(OpMode opMode)
{
if (fp_ == nullptr)
return 1;
if (opMode_ == opMode)
return 0;
OpMode oldOpMode = opMode_;
opMode_ = opMode;
bool reopen = true;
switch (opMode) {
case opRead:
// Flush if current mode allows reading, else reopen (in mode
// "r+b" as in this case we know that we can write to the file)
if (openMode_[0] == 'r' || openMode_[1] == '+')
reopen = false;
break;
case opWrite:
// Flush if current mode allows writing, else reopen
if (openMode_[0] != 'r' || openMode_[1] == '+')
reopen = false;
break;
case opSeek:
reopen = false;
break;
}
if (!reopen) {
// Do not do anything when switching _from_ opSeek mode; we
// flush when switching _to_ opSeek.
if (oldOpMode == opSeek)
return 0;
// Flush. On msvcrt fflush does not do the job
std::fseek(fp_, 0, SEEK_CUR);
return 0;
}
// Reopen the file
long offset = std::ftell(fp_);
if (offset == -1)
return -1;
// 'Manual' open("r+b") to avoid munmap()
std::fclose(fp_);
openMode_ = "r+b";
opMode_ = opSeek;
#ifdef EXV_UNICODE_PATH
if (wpMode_ == wpUnicode) {
fp_ = ::_wfopen(wpath_.c_str(), s2ws(openMode_).c_str());
} else
#endif
{
fp_ = std::fopen(path_.c_str(), openMode_.c_str());
}
if (fp_ == nullptr)
return 1;
return std::fseek(fp_, offset, SEEK_SET);
}
int FileIo::Impl::stat(StructStat& buf) const
{
int ret = 0;
#ifdef EXV_UNICODE_PATH
#ifdef _WIN64
struct _stat64 st;
ret = ::_wstati64(wpath_.c_str(), &st);
if (0 == ret) {
buf.st_size = static_cast<off_t>(st.st_size);
buf.st_mode = st.st_mode;
buf.st_nlink = st.st_nlink;
}
#else
struct _stat st;
ret = ::_wstat(wpath_.c_str(), &st);
if (0 == ret) {
buf.st_size = st.st_size;
buf.st_mode = st.st_mode;
buf.st_nlink = st.st_nlink;
}
#endif
else
#endif
{
struct stat st;
ret = ::stat(path_.c_str(), &st);
if (0 == ret) {
buf.st_size = st.st_size;
buf.st_nlink = st.st_nlink;
buf.st_mode = st.st_mode;
}
}
return ret;
}
FileIo::FileIo(const std::string& path) noexcept
: p_(new Impl(path))
{
}
#ifdef EXV_UNICODE_PATH
FileIo::FileIo(const std::wstring& wpath)
: p_(new Impl(wpath))
{
}
#endif
FileIo::~FileIo()
{
close();
}
int FileIo::munmap()
{
int rc = 0;
if (p_->pMappedArea_ != nullptr) {
#if defined EXV_HAVE_MMAP && defined EXV_HAVE_MUNMAP
if (::munmap(p_->pMappedArea_, p_->mappedLength_) != 0) {
rc = 1;
}
#elif defined WIN32 && !defined __CYGWIN__
UnmapViewOfFile(p_->pMappedArea_);
CloseHandle(p_->hMap_);
p_->hMap_ = 0;
CloseHandle(p_->hFile_);
p_->hFile_ = nullptr;
#else
if (p_->isWriteable_) {
seek(0, BasicIo::beg);
write(p_->pMappedArea_, p_->mappedLength_);
}
if (p_->isMalloced_) {
delete[] p_->pMappedArea_;
p_->isMalloced_ = false;
}
#endif
}
if (p_->isWriteable_) {
if (p_->fp_ != nullptr)
p_->switchMode(Impl::opRead);
p_->isWriteable_ = false;
}
p_->pMappedArea_ = nullptr;
p_->mappedLength_ = 0;
return rc;
}
byte* FileIo::mmap(bool isWriteable)
{
assert(p_->fp_ != nullptr);
if (munmap() != 0) {
#ifdef EXV_UNICODE_PATH
if (p_->wpMode_ == Impl::wpUnicode) {
throw WError(kerCallFailed, wpath(), strError().c_str(), "munmap");
} else
#endif
{
throw Error(kerCallFailed, path(), strError(), "munmap");
}
}
p_->mappedLength_ = size();
p_->isWriteable_ = isWriteable;
if (p_->isWriteable_ && p_->switchMode(Impl::opWrite) != 0) {
#ifdef EXV_UNICODE_PATH
if (p_->wpMode_ == Impl::wpUnicode) {
throw WError(kerFailedToMapFileForReadWrite, wpath(), strError().c_str());
} else
#endif
{
throw Error(kerFailedToMapFileForReadWrite, path(), strError());
}
}
#if defined EXV_HAVE_MMAP && defined EXV_HAVE_MUNMAP
int prot = PROT_READ;
if (p_->isWriteable_) {
prot |= PROT_WRITE;
}
void* rc = ::mmap(0, p_->mappedLength_, prot, MAP_SHARED, fileno(p_->fp_), 0);
if (MAP_FAILED == rc) {
#ifdef EXV_UNICODE_PATH
if (p_->wpMode_ == Impl::wpUnicode) {
throw WError(kerCallFailed, wpath(), strError().c_str(), "mmap");
} else
#endif
{
throw Error(kerCallFailed, path(), strError(), "mmap");
}
}
p_->pMappedArea_ = static_cast<byte*>(rc);
#elif defined WIN32 && !defined __CYGWIN__
// Windows implementation
// TODO: An attempt to map a file with a length of 0 (zero) fails with
// an error code of ERROR_FILE_INVALID.
// Applications should test for files with a length of 0 (zero) and
// reject those files.
DWORD dwAccess = FILE_MAP_READ;
DWORD flProtect = PAGE_READONLY;
if (isWriteable) {
dwAccess = FILE_MAP_WRITE;
flProtect = PAGE_READWRITE;
}
HANDLE hPh = GetCurrentProcess();
HANDLE hFd = (HANDLE)_get_osfhandle(fileno(p_->fp_));
if (hFd == INVALID_HANDLE_VALUE) {
#ifdef EXV_UNICODE_PATH
if (p_->wpMode_ == Impl::wpUnicode) {
throw WError(kerCallFailed, wpath(), "MSG1", "_get_osfhandle");
} else
#endif
{
throw Error(kerCallFailed, path(), "MSG1", "_get_osfhandle");
}
}
if (!DuplicateHandle(hPh, hFd, hPh, &p_->hFile_, 0, false, DUPLICATE_SAME_ACCESS)) {
#ifdef EXV_UNICODE_PATH
if (p_->wpMode_ == Impl::wpUnicode) {
throw WError(kerCallFailed, wpath(), "MSG2", "DuplicateHandle");
} else
#endif
{
throw Error(kerCallFailed, path(), "MSG2", "DuplicateHandle");
}
}
p_->hMap_ = CreateFileMapping(p_->hFile_, 0, flProtect, 0, (DWORD)p_->mappedLength_, 0);
if (p_->hMap_ == 0) {
#ifdef EXV_UNICODE_PATH
if (p_->wpMode_ == Impl::wpUnicode) {
throw WError(kerCallFailed, wpath(), "MSG3", "CreateFileMapping");
} else
#endif
{
throw Error(kerCallFailed, path(), "MSG3", "CreateFileMapping");
}
}
void* rc = MapViewOfFile(p_->hMap_, dwAccess, 0, 0, 0);
if (rc == 0) {
#ifdef EXV_UNICODE_PATH
if (p_->wpMode_ == Impl::wpUnicode) {
throw WError(kerCallFailed, wpath(), "MSG4", "CreateFileMapping");
} else
#endif
{
throw Error(kerCallFailed, path(), "MSG4", "CreateFileMapping");
}
}
p_->pMappedArea_ = static_cast<byte*>(rc);
#else
// Workaround for platforms without mmap: Read the file into memory
DataBuf buf(static_cast<long>(p_->mappedLength_));
if (read(buf.pData_, buf.size_) != buf.size_) {
#ifdef EXV_UNICODE_PATH
if (p_->wpMode_ == Impl::wpUnicode) {
throw WError(kerCallFailed, wpath(), strError().c_str(), "FileIo::read");
} else
#endif
{
throw Error(kerCallFailed, path(), strError(), "FileIo::read");
}
}
if (error()) {
#ifdef EXV_UNICODE_PATH
if (p_->wpMode_ == Impl::wpUnicode) {
throw WError(kerCallFailed, wpath(), strError().c_str(), "FileIo::mmap");
} else
#endif
{
throw Error(kerCallFailed, path(), strError(), "FileIo::mmap");
}
}
p_->pMappedArea_ = buf.release().first;
p_->isMalloced_ = true;
#endif
return p_->pMappedArea_;
}
void FileIo::setPath(const std::string& path)
{
close();
#ifdef EXV_UNICODE_PATH
if (p_->wpMode_ == Impl::wpUnicode) {
std::wstring wpath;
wpath.assign(path.begin(), path.end());
p_->wpath_ = wpath;
}
p_->path_ = path;
#else
p_->path_ = path;
#endif
}
#ifdef EXV_UNICODE_PATH
void FileIo::setPath(const std::wstring& wpath)
{
close();
if (p_->wpMode_ == Impl::wpStandard) {
std::string path;
path.assign(wpath.begin(), wpath.end());
p_->path_ = path;
} else {
p_->wpath_ = wpath;
}
}
#endif
size_t FileIo::write(const byte* data, size_t wcount)
{
if (p_->fp_ == nullptr || p_->switchMode(Impl::opWrite) != 0)
return 0;
return std::fwrite(data, 1, wcount, p_->fp_);
}
size_t FileIo::write(BasicIo& src)
{
if (p_->fp_ == nullptr || static_cast<BasicIo*>(this) == &src || !src.isopen() ||
p_->switchMode(Impl::opWrite) != 0) {
return 0;
}
byte buf[4096];
size_t readCount = 0;
size_t writeTotal = 0;
while ((readCount = src.read(buf, sizeof(buf)))) {
const size_t writeCount = std::fwrite(buf, 1, readCount, p_->fp_);
writeTotal += writeCount;
if (writeCount != readCount) {
// try to reset back to where write stopped
src.seek(writeCount - readCount, BasicIo::cur);
break;
}
}
return writeTotal;
}
void FileIo::transfer(BasicIo& src)
{
const bool wasOpen = (p_->fp_ != nullptr);
const std::string lastMode(p_->openMode_);
FileIo* fileIo = dynamic_cast<FileIo*>(&src);
if (fileIo) {
// Optimization if src is another instance of FileIo
fileIo->close();
// Check if the file can be written to, if it already exists
if (open("a+b") != 0) {
// Remove the (temporary) file
#ifdef EXV_UNICODE_PATH
if (fileIo->p_->wpMode_ == Impl::wpUnicode) {
::_wremove(fileIo->wpath().c_str());
} else
#endif
{
::remove(fileIo->path().c_str());
}
#ifdef EXV_UNICODE_PATH
if (p_->wpMode_ == Impl::wpUnicode) {
throw WError(kerFileOpenFailed, wpath(), "a+b", strError().c_str());
} else
#endif
{
throw Error(kerFileOpenFailed, path(), "a+b", strError());
}
}
close();
bool statOk = true;
mode_t origStMode = 0;
std::string spf;
char* pf{nullptr};
#ifdef EXV_UNICODE_PATH
std::wstring wspf;
wchar_t* wpf = 0;
if (p_->wpMode_ == Impl::wpUnicode) {
wspf = wpath();
wpf = const_cast<wchar_t*>(wspf.c_str());
} else
#endif
{
spf = path();
pf = const_cast<char*>(spf.c_str());
}
// Get the permissions of the file, or linked-to file, on platforms
// which have lstat
#ifdef EXV_HAVE_LSTAT
#ifdef EXV_UNICODE_PATH
#error EXV_UNICODE_PATH and EXV_HAVE_LSTAT are not compatible. Stop.
#endif
struct stat buf1;
if (::lstat(pf, &buf1) == -1) {
statOk = false;
#ifndef SUPPRESS_WARNINGS
EXV_WARNING << Error(kerCallFailed, pf, strError(), "::lstat") << "\n";
#endif
}
origStMode = buf1.st_mode;
DataBuf lbuf; // So that the allocated memory is freed. Must have
// same scope as pf
// In case path() is a symlink, get the path of the linked-to file
if (statOk && S_ISLNK(buf1.st_mode)) {
lbuf.alloc(buf1.st_size + 1);
memset(lbuf.pData_, 0x0, lbuf.size_);
pf = reinterpret_cast<char*>(lbuf.pData_);
if (::readlink(path().c_str(), pf, lbuf.size_ - 1) == -1) {
throw Error(kerCallFailed, path(), strError(), "readlink");
}
// We need the permissions of the file, not the symlink
if (::stat(pf, &buf1) == -1) {
statOk = false;
#ifndef SUPPRESS_WARNINGS
EXV_WARNING << Error(kerCallFailed, pf, strError(), "::stat") << "\n";
#endif
}
origStMode = buf1.st_mode;
}
#else // EXV_HAVE_LSTAT
Impl::StructStat buf1;
if (p_->stat(buf1) == -1) {
statOk = false;
}
origStMode = buf1.st_mode;
#endif // !EXV_HAVE_LSTAT
// MSVCRT rename that does not overwrite existing files
#ifdef EXV_UNICODE_PATH
if (p_->wpMode_ == Impl::wpUnicode) {
#if defined(WIN32) && defined(REPLACEFILE_IGNORE_MERGE_ERRORS)
// Windows implementation that deals with the fact that ::rename
// fails if the target filename still exists, which regularly
// happens when that file has been opened with FILE_SHARE_DELETE
// by another process, like a virus scanner or disk indexer (see
// also http://stackoverflow.com/a/11023068)
typedef BOOL(WINAPI * ReplaceFileW_t)(LPCWSTR, LPCWSTR, LPCWSTR, DWORD, LPVOID, LPVOID);
HMODULE hKernel = ::GetModuleHandleA("kernel32.dll");
if (hKernel) {
ReplaceFileW_t pfcn_ReplaceFileW = (ReplaceFileW_t)GetProcAddress(hKernel, "ReplaceFileW");
if (pfcn_ReplaceFileW) {
BOOL ret = pfcn_ReplaceFileW(wpf, fileIo->wpath().c_str(), nullptr,
REPLACEFILE_IGNORE_MERGE_ERRORS, nullptr, nullptr);
if (ret == 0) {
if (GetLastError() == ERROR_FILE_NOT_FOUND) {
if (::_wrename(fileIo->wpath().c_str(), wpf) == -1) {
throw WError(kerFileRenameFailed, fileIo->wpath(), wpf, strError().c_str());
}
::_wremove(fileIo->wpath().c_str());
} else {
throw WError(kerFileRenameFailed, fileIo->wpath(), wpf, strError().c_str());
}
}
} else {
if (fileExists(wpf) && ::_wremove(wpf) != 0) {
throw WError(kerCallFailed, wpf, strError().c_str(), "::_wremove");
}
if (::_wrename(fileIo->wpath().c_str(), wpf) == -1) {
throw WError(kerFileRenameFailed, fileIo->wpath(), wpf, strError().c_str());
}
::_wremove(fileIo->wpath().c_str());
}
}
#else
if (fileExists(wpf) && ::_wremove(wpf) != 0) {
throw WError(kerCallFailed, wpf, strError().c_str(), "::_wremove");
}
if (::_wrename(fileIo->wpath().c_str(), wpf) == -1) {
throw WError(kerFileRenameFailed, fileIo->wpath(), wpf, strError().c_str());
}
::_wremove(fileIo->wpath().c_str());
#endif
// Check permissions of new file
struct _stat buf2;
if (statOk && ::_wstat(wpf, &buf2) == -1) {
statOk = false;
#ifndef SUPPRESS_WARNINGS
EXV_WARNING << Error(kerCallFailed, wpf, strError(), "::_wstat") << "\n";
#endif
}
if (statOk && origStMode != buf2.st_mode) {
// Set original file permissions
if (::_wchmod(wpf, origStMode) == -1) {
#ifndef SUPPRESS_WARNINGS
EXV_WARNING << Error(kerCallFailed, wpf, strError(), "::_wchmod") << "\n";
#endif
}
}
} // if (p_->wpMode_ == Impl::wpUnicode)
else
#endif // EXV_UNICODE_PATH
{
#if defined(WIN32) && defined(REPLACEFILE_IGNORE_MERGE_ERRORS)
// Windows implementation that deals with the fact that ::rename
// fails if the target filename still exists, which regularly
// happens when that file has been opened with FILE_SHARE_DELETE
// by another process, like a virus scanner or disk indexer (see
// also http://stackoverflow.com/a/11023068)
typedef BOOL(WINAPI * ReplaceFileA_t)(LPCSTR, LPCSTR, LPCSTR, DWORD, LPVOID, LPVOID);
HMODULE hKernel = ::GetModuleHandleA("kernel32.dll");
if (hKernel) {
ReplaceFileA_t pfcn_ReplaceFileA = (ReplaceFileA_t)GetProcAddress(hKernel, "ReplaceFileA");
if (pfcn_ReplaceFileA) {
BOOL ret = pfcn_ReplaceFileA(pf, fileIo->path().c_str(), nullptr,
REPLACEFILE_IGNORE_MERGE_ERRORS, nullptr, nullptr);
if (ret == 0) {
if (GetLastError() == ERROR_FILE_NOT_FOUND) {
if (::rename(fileIo->path().c_str(), pf) == -1) {
throw Error(kerFileRenameFailed, fileIo->path(), pf, strError());
}
::remove(fileIo->path().c_str());
} else {
throw Error(kerFileRenameFailed, fileIo->path(), pf, strError());
}
}
} else {
if (fileExists(pf) && ::remove(pf) != 0) {
throw Error(kerCallFailed, pf, strError(), "::remove");
}
if (::rename(fileIo->path().c_str(), pf) == -1) {
throw Error(kerFileRenameFailed, fileIo->path(), pf, strError());
}
::remove(fileIo->path().c_str());
}
}
#else
if (fileExists(pf) && ::remove(pf) != 0) {
throw Error(kerCallFailed, pf, strError(), "::remove");
}
if (::rename(fileIo->path().c_str(), pf) == -1) {
throw Error(kerFileRenameFailed, fileIo->path(), pf, strError());
}
::remove(fileIo->path().c_str());
#endif
// Check permissions of new file
struct stat buf2
{
};
if (statOk && ::stat(pf, &buf2) == -1) {
statOk = false;
#ifndef SUPPRESS_WARNINGS
EXV_WARNING << Error(kerCallFailed, pf, strError(), "::stat") << "\n";
#endif
}
if (statOk && origStMode != buf2.st_mode) {
// Set original file permissions
if (::chmod(pf, origStMode) == -1) {
#ifndef SUPPRESS_WARNINGS
EXV_WARNING << Error(kerCallFailed, pf, strError(), "::chmod") << "\n";
#endif
}
}
}
} // if (fileIo)
else {
// Generic handling, reopen both to reset to start
if (open("w+b") != 0) {
#ifdef EXV_UNICODE_PATH
if (p_->wpMode_ == Impl::wpUnicode) {
throw WError(kerFileOpenFailed, wpath(), "w+b", strError().c_str());
} else
#endif
{
throw Error(kerFileOpenFailed, path(), "w+b", strError());
}
}
if (src.open() != 0) {
#ifdef EXV_UNICODE_PATH
if (p_->wpMode_ == Impl::wpUnicode) {
throw WError(kerDataSourceOpenFailed, src.wpath(), strError().c_str());
} else
#endif
{
throw Error(kerDataSourceOpenFailed, src.path(), strError());
}
}
write(src);
src.close();
}
if (wasOpen) {
if (open(lastMode) != 0) {
#ifdef EXV_UNICODE_PATH
if (p_->wpMode_ == Impl::wpUnicode) {
throw WError(kerFileOpenFailed, wpath(), lastMode.c_str(), strError().c_str());
} else
#endif
{
throw Error(kerFileOpenFailed, path(), lastMode, strError());
}
}
} else
close();
if (error() || src.error()) {
#ifdef EXV_UNICODE_PATH
if (p_->wpMode_ == Impl::wpUnicode) {
throw WError(kerTransferFailed, wpath(), strError().c_str());
} else
#endif
{
throw Error(kerTransferFailed, path(), strError());
}
}
} // FileIo::transfer
int FileIo::putb(byte data)
{
if (p_->fp_ == nullptr || p_->switchMode(Impl::opWrite) != 0)
return EOF;
return putc(data, p_->fp_);
}
int FileIo::seek(int64 offset, Position pos)
{
if (p_->switchMode(Impl::opSeek) != 0)
return 1;
int fileSeek = 0;
switch (pos) {
case BasicIo::cur:
fileSeek = SEEK_CUR;
break;
case BasicIo::beg:
fileSeek = SEEK_SET;
break;
case BasicIo::end:
fileSeek = SEEK_END;
break;
}
#ifdef _WIN64
return _fseeki64(p_->fp_, offset, fileSeek);
#else
return std::fseek(p_->fp_, static_cast<long>(offset), fileSeek);
#endif
}
int64 FileIo::tell() const
{
if (p_->fp_ == nullptr) {
return -1;
}
return std::ftell(p_->fp_);
}
size_t FileIo::size() const
{
// Flush and commit only if the file is open for writing
if (p_->fp_ != nullptr && (p_->openMode_[0] != 'r' || p_->openMode_[1] == '+')) {
std::fflush(p_->fp_);
#if defined WIN32 && !defined __CYGWIN__
// This is required on msvcrt before stat after writing to a file
_commit(_fileno(p_->fp_));
#endif
}
Impl::StructStat buf;
const int ret = p_->stat(buf);
if (ret != 0)
return std::numeric_limits<size_t>::max();
return static_cast<size_t>(buf.st_size);
}
int FileIo::open()
{
// Default open is in read-only binary mode
return open("rb");
}
int FileIo::open(const std::string& mode)
{
close();
p_->openMode_ = mode;
p_->opMode_ = Impl::opSeek;
#ifdef EXV_UNICODE_PATH
if (p_->wpMode_ == Impl::wpUnicode) {
p_->fp_ = ::_wfopen(wpath().c_str(), s2ws(mode).c_str());
} else
#endif
{
p_->fp_ = ::fopen(path().c_str(), mode.c_str());
}
if (!p_->fp_)
return 1;
return 0;
}
bool FileIo::isopen() const
{
return p_->fp_ != nullptr;
}
int FileIo::close()
{
int rc = 0;
if (munmap() != 0)
rc = 2;
if (p_->fp_ != nullptr) {
if (std::fclose(p_->fp_) != 0)
rc |= 1;
p_->fp_ = nullptr;
}
return rc;
}
DataBuf FileIo::read(size_t rcount) noexcept
{
if (p_->fp_ == nullptr || rcount > size()) {
return {};
}
DataBuf buf(static_cast<size_t>(rcount));
size_t readCount = read(buf.pData_, buf.size_);
buf.size_ = readCount;
return buf;
}
size_t FileIo::read(byte* buf, size_t rcount)
{
if (p_->fp_ == nullptr || p_->switchMode(Impl::opRead) != 0) {
return 0;
}
return std::fread(buf, 1, rcount, p_->fp_);
}
int FileIo::getb()
{
if (p_->fp_ == nullptr || p_->switchMode(Impl::opRead) != 0)
return EOF;
return getc(p_->fp_);
}
int FileIo::error() const
{
return p_->fp_ != nullptr ? ferror(p_->fp_) : 0;
}
bool FileIo::eof() const
{
if (p_->fp_ == nullptr)
return true;
return feof(p_->fp_) != 0 || tell() >= static_cast<int64>(size());
}
std::string FileIo::path() const
{
#ifdef EXV_UNICODE_PATH
if (p_->wpMode_ == Impl::wpUnicode) {
return ws2s(p_->wpath_);
}
#endif
return p_->path_;
}
#ifdef EXV_UNICODE_PATH
std::wstring FileIo::wpath() const
{
if (p_->wpMode_ == Impl::wpStandard) {
return s2ws(p_->path_);
}
return p_->wpath_;
}
#endif
void FileIo::populateFakeData()
{
}
DataBuf readFile(const std::string& path)
{
FileIo file(path);
if (file.open("rb") != 0) {
throw Error(kerFileOpenFailed, path, "rb", strError());
}
struct stat st;
if (0 != ::stat(path.c_str(), &st)) {
throw Error(kerCallFailed, path, strError(), "::stat");
}
DataBuf buf(st.st_size);
size_t len = file.read(buf.pData_, buf.size_);
if (len != buf.size_) {
throw Error(kerCallFailed, path, strError(), "FileIo::read");
}
return buf;
}
#ifdef EXV_UNICODE_PATH
DataBuf readFile(const std::wstring& wpath)
{
FileIo file(wpath);
if (file.open("rb") != 0) {
throw WError(kerFileOpenFailed, wpath, "rb", strError().c_str());
}
struct _stat st;
if (0 != ::_wstat(wpath.c_str(), &st)) {
throw WError(kerCallFailed, wpath, strError().c_str(), "::_wstat");
}
DataBuf buf(st.st_size);
size_t len = file.read(buf.pData_, buf.size_);
if (len != buf.size_) {
throw WError(kerCallFailed, wpath, strError().c_str(), "FileIo::read");
}
return buf;
}
#endif
size_t writeFile(const DataBuf& buf, const std::string& path)
{
FileIo file(path);
if (file.open("wb") != 0) {
throw Error(kerFileOpenFailed, path, "wb", strError());
}
return file.write(buf.pData_, buf.size_);
}
#ifdef EXV_UNICODE_PATH
size_t writeFile(const DataBuf& buf, const std::wstring& wpath)
{
FileIo file(wpath);
if (file.open("wb") != 0) {
throw WError(kerFileOpenFailed, wpath, "wb", strError().c_str());
}
return file.write(buf.pData_, buf.size_);
}
#endif
const std::string XPathIo::TEMP_FILE_EXT = ".exiv2_temp";
const std::string XPathIo::GEN_FILE_EXT = ".exiv2";
XPathIo::XPathIo(const std::string& orgPath)
: FileIo(XPathIo::writeDataToFile(orgPath))
{
isTemp_ = true;
tempFilePath_ = path();
}
#ifdef EXV_UNICODE_PATH
XPathIo::XPathIo(const std::wstring& wOrgPathpath)
: FileIo(XPathIo::writeDataToFile(wOrgPathpath))
{
isTemp_ = true;
tempFilePath_ = path();
}
#endif
XPathIo::~XPathIo()
{
if (isTemp_ && remove(tempFilePath_.c_str()) != 0) {
// error when removing file
// printf ("Warning: Unable to remove the temp file %s.\n",
// tempFilePath_.c_str());