filesystem.cpp 35.1 KB
Newer Older
schultezub's avatar
schultezub committed
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
/**********************************************************************
 *                                                                    *
 * tgt - Tiny Graphics Toolbox                                        *
 *                                                                    *
 * Copyright (C) 2006-2011 Visualization and Computer Graphics Group, *
 * Department of Computer Science, University of Muenster, Germany.   *
 * <http://viscg.uni-muenster.de>                                     *
 *                                                                    *
 * This file is part of the tgt library. This library is free         *
 * software; you can redistribute it and/or modify it under the terms *
 * of the GNU Lesser General Public License version 2.1 as published  *
 * by the Free Software Foundation.                                   *
 *                                                                    *
 * This library 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 Lesser General Public License for more details.                *
 *                                                                    *
 * You should have received a copy of the GNU Lesser General Public   *
 * License in the file "LICENSE.txt" along with this library.         *
 * If not, see <http://www.gnu.org/licenses/>.                        *
 *                                                                    *
 **********************************************************************/

#include "tgt/filesystem.h"

#include "tgt/types.h"

#include <algorithm>
#include <string>
#include <cstring>
#include <cctype> // std::tolower
#include <iostream>
#include <sstream>
#include <fstream>
#include <stack>
#include <sys/stat.h>

#ifndef WIN32
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <dirent.h>
#else
#include <windows.h>
#include <tchar.h>
#include <direct.h>
#endif

using std::string;

//TODO: should really switch to exceptions instead of just logging with LERROR. joerg
// see http://en.wikipedia.org/wiki/Aliasing_%28computing%29#Conflicts_with_optimization

// anonymous namespace
namespace {

enum {
    TEMP_BUFFERSIZE = 128,
    LINE_BUFFERSIZE = 512,

    /*
     * Standard Archive Format - Standard TAR - USTAR
     */
    RECORDSIZE = 512,
    NAMSIZ = 100,
    TUNMLEN = 32,
    TGNMLEN = 32
};

//TODO: these definitions with arrays of chars break strict-aliasing rules for gcc

union record {
    char charptr[RECORDSIZE];
    struct header {
        char    name[NAMSIZ];
        char    mode[8];
        char    uid[8];
        char    gid[8];
        char    size[12];
        char    mtime[12];
        char    chksum[8];
        char    linkflag;
        char    linkname[NAMSIZ];
        char    magic[8];
        char    uname[TUNMLEN];
        char    gname[TGNMLEN];
        char    devmajor[8];
        char    devminor[8];
    } header;
};

size_t findDelim(char* buf, size_t size, char delim) {
    size_t pos = 0;
    while (pos < (size - 1)) {
        if (buf[pos] == delim)
            return pos;
        ++pos;
    }
    return size+1;
}

} // anonymous namespace

namespace tgt {

File::File()
    : size_(0)
{}

File::File(const std::string& name)
    : name_(name),
      size_(0)
{}

File::~File()
{}

size_t File::readLine(char *buf, size_t maxCount, char delim) {
    size_t bytesread = 0;
    size_t start = tell();
    char* p = buf;

    if (tell()+maxCount > size())
        maxCount = size()-tell();
    while (bytesread < maxCount) {
        size_t bufsize = TEMP_BUFFERSIZE;
        if ((bytesread + TEMP_BUFFERSIZE) > maxCount)
            bufsize = (maxCount - bytesread);
        read(p, bufsize);
        size_t d = findDelim(p, bufsize, delim);
        if (d < bufsize) {
            bytesread += d;
            p[d] = 0;
            seek(start + bytesread + 1);
            return bytesread;
        }
        p += bufsize;
        bytesread += bufsize;
    }
    return bytesread;
}

std::string File::getLine(char delim) {
    char* buf = new char[LINE_BUFFERSIZE];
    size_t br = readLine(buf, LINE_BUFFERSIZE, delim);
    std::string ret(buf, br);
    while (br == LINE_BUFFERSIZE) {
        br = readLine(buf, LINE_BUFFERSIZE, delim);
        ret.append(buf, br);
    }
    delete[] buf;
    return ret;
}

std::string File::getAsString(void) {
    char* buf = new char[size()];
    read(buf, size());
    std::string ret(buf, size());
    delete[] buf;
    return ret;
}

size_t File::skipLine(char delim) {
    size_t bytesread = 0;
    size_t start = tell();
    char* p = new char[TEMP_BUFFERSIZE];
    while (true) {
        read(p, TEMP_BUFFERSIZE);
        size_t d = findDelim(p, TEMP_BUFFERSIZE, delim);
        if (d < TEMP_BUFFERSIZE) {
            bytesread += d-1;
            delete[] p;
            seek(start + bytesread + 2);
            return bytesread;
        }
        bytesread += TEMP_BUFFERSIZE;
    }
}

//-----------------------------------------------------------------------------

const std::string RegularFile::loggerCat_("tgt.RegularFile");

RegularFile::RegularFile(const std::string& filename)
    : File(filename)
{
    file_.open(filename.c_str(), std::ios::binary);

    if (!file_) {
        LDEBUG("Cannot open file: " << filename);
        return;
    }

    // get file size
    file_.seekg(0, std::ios::end);
    size_ = tell();
    file_.seekg(std::ios::beg);
}

RegularFile::~RegularFile() {
    close();
}

void RegularFile::close() {
    if (isOpen())
        file_.close();
}

size_t RegularFile::read(void* buf, size_t count) {
    if (!isOpen() || eof())
        return 0;

    file_.read(static_cast<char*>(buf), count);
    return static_cast<size_t>(file_.gcount());
}

void RegularFile::skip(size_t count) {
    if (isOpen())
        file_.seekg(count, std::ios::cur);
}

void RegularFile::seek(std::streamoff pos) {
    if (isOpen())
        file_.seekg(pos);
}

void RegularFile::seek(std::streamoff offset, File::SeekDir seekDir) {
    if (isOpen()) {
        switch (seekDir)  {
        case File::BEGIN:
            file_.seekg(offset, std::ios_base::beg);
            break;
        case File::CURRENT:
            file_.seekg(offset, std::ios_base::cur);
            break;
        case File::END:
            file_.seekg(offset, std::ios_base::end);
            break;
        }
    }
}

size_t RegularFile::tell() {
    if (isOpen()) {
        std::streampos p = file_.tellg();
        if (p >= 0)
            return static_cast<size_t>(p);
    }
    return 0;
}

bool RegularFile::eof() {
    if (isOpen()) {
        if (tell() == size())
            return true;
        else
            return file_.eof();
    } else {
        return true;
    }
}

bool RegularFile::isOpen() {
    return file_.is_open();
}

bool RegularFile::good() {
    if (isOpen())
        return file_.good();
    else
        return false;
}

//-----------------------------------------------------------------------------

MemoryFile::MemoryFile(const char* data, size_t size, const std::string& filename, bool deleteData)
  : File(filename),
  data_(data),
  pos_(0),
  deleteData_(deleteData)
{
    size_ = size;
}

MemoryFile::~MemoryFile(){
    if (data_ && deleteData_)
        delete[] data_;
}

void MemoryFile::close() {
    pos_ = 0;
}

size_t MemoryFile::read(void* buf, size_t count) {
    if ((pos_ + count) < size_) {
        memcpy(buf, (const void*) (data_ + pos_), count);
        pos_ += count;
        return count;
    }
    else {
        if (eof())
            return 0;
        size_t r = size_ - pos_;
        memcpy(buf, (const void*) (data_ + pos_), r);
        pos_ = size_ - 1;
        return r;
    }
}

void MemoryFile::skip(size_t count) {
    pos_ += count;
    if (pos_ >= size_)
        pos_ = size_ - 1;
}

void MemoryFile::seek(std::streamoff pos) {
    pos_ = static_cast<size_t>(pos);
    if (pos_ >= size_)
        pos_ = size_ - 1;
}

void MemoryFile::seek(std::streamoff offset, File::SeekDir seekDir) {
    switch (seekDir) {
        case File::BEGIN:
            seek(offset);
            break;
        case File::CURRENT:
            seek(pos_+offset);
            break;
        case File::END:
            pos_ = size_ - 1;
            break;
    }
}

size_t MemoryFile::tell() {
    return pos_;
}

bool MemoryFile::eof() {
    return (pos_ >= size_ - 1);
}

bool MemoryFile::isOpen() {
    return true;
}

bool MemoryFile::good() {
    // only possibility not to be good is on EOF
    return !eof();
}

//-----------------------------------------------------------------------------

const std::string TarFile::loggerCat_("tgt.TarFile");

TarFile::TarFile(const std::string& filename, const std::string& tarfilename, size_t offset, size_t size)
    : File(filename)
{
    file_ = FileSys.open(tarfilename.c_str());
    name_ = filename;

    // Check if file is open:
    if (!file_) {
        std::cout << "Failed to open tar file: " << tarfilename << std::endl;
        return;
    }

    if (!file_->good()) {
        std::cout << "Bad tar file: " << tarfilename << std::endl;
        file_->close();
        delete file_;
        file_ = 0;
        return;
    }

#ifdef TGT_DEBUG
    file_->seek(0, File::END);
    size_ = file_->tell();
    file_->seek(File::BEGIN);

    if ((offset + size) > size_) {
        std::cout << "Wrong tar file size/offset! file: " << filename << " in " << tarfilename << std::endl;
        std::cout << "size_ " << size_ << " offset " << offset << " size " << size << std::endl;
        file_->close();
        delete file_;
        file_ = 0;
        return;
    }
#endif // TGT_DEBUG

    size_ = size;
    offset_ = offset;
    file_->seek(offset_, File::BEGIN);
}

TarFile::~TarFile() {
    close();
}

void TarFile::close() {
    if (file_) {
        file_->close();
        delete file_;
        file_ = 0;
    }
}

size_t TarFile::read(void* buf, size_t count) {
    if (tell() + count > size_)
        count = size_ - tell();
    if (!file_)
        return 0;
    file_->read(static_cast<char*>(buf), count);
    return count;
}

void TarFile::skip(size_t count) {
    if (tell() + count > size_)
        count = size_ - tell();

    if (file_)
        file_->seek(count, File::CURRENT);
}

void TarFile::seek(std::streamoff pos) {
    if (file_)
        file_->seek(offset_ + pos);
}

void TarFile::seek(std::streamoff offset, File::SeekDir seekDir) {
    if (file_)
        file_->seek(offset, seekDir);
}

size_t TarFile::tell() {
    if (file_)
        return ((size_t)(file_->tell())) - offset_;
    else
        return 0;
}

bool TarFile::eof() {
    if (file_)
       return (tell() == size_);
    else
        return true;
}

bool TarFile::isOpen() {
    return (file_ != 0);
}

bool TarFile::good() {
    if (eof())
        return false;

    if (file_)
        return file_->good();
    else
        return false;
}


//-----------------------------------------------------------------------------

MemoryFileFactory::MemoryFileFactory(const std::string& filename, char* data, size_t size) {
    data_ = data;
    size_ = size;
    filename_ = filename;
}

MemoryFileFactory::MemoryFileFactory(const std::string& filename, const std::string& data) {
    size_ = data.length();
    data_ = new char[size_];
    memcpy(data_, data.c_str(), size_);
    filename_ = filename;
}

File* MemoryFileFactory::open(const std::string& filename) {
    if (filename == filename_)
        return new MemoryFile(data_, size_, filename_);
    else
        return 0;
}

std::vector<std::string> MemoryFileFactory::getFilenames() {
     std::vector<std::string> files;
     files.push_back(filename_);
     return files;
}

//-----------------------------------------------------------------------------

const std::string TarFileFactory::loggerCat_("tgt.TarFileFactory");

TarFileFactory::TarFileFactory(const std::string& filename, const std::string& rootpath) {
    filename_ = filename;
    File* file = FileSys.open(filename);

    // Check if file is open
    if (!file) {
        LERROR("Failed to open TAR archive " << filename);
        return;
    }

    if (!file->good()) {
        LERROR("Error opening TAR archive " << filename);
        return;
    }

    bool end = false;

    record* currecord = new record;

    while (!end) {
        file->read(currecord->charptr, RECORDSIZE);

        bool allzero = true;
        //check if record is all zero (=>eof)
        for (int i = 0; i < 512; i++) {
            if (currecord->charptr[i] != 0)
                allzero = false;
        }

        if (!allzero) {
            //convert from ascii-octal to normal int
            size_t filesize = 0;
            size_t mul = 1;
            for (int i = 10; i >= 0; i--) {
                filesize += ((currecord->header.size[i] - 48) * mul);
                mul *= 8;
            }

            if (filesize > 0) {
                size_t blocks = (filesize / RECORDSIZE);
                if ((filesize % RECORDSIZE) != 0)
                    ++blocks;
                LDEBUG("Found file " << currecord->header.name);
                LDEBUG("  Add it as " << rootpath+currecord->header.name
                       << "; filesize: " << filesize << "; offset " << file->tell());
                files_[rootpath+currecord->header.name].size_ = filesize;
                files_[rootpath+currecord->header.name].offset_ = file->tell();

                file->seek(RECORDSIZE*blocks, File::CURRENT);
            }

            if (file->eof())
                end = true;
        }
        else
            end = true;
    }
    file->close();
    delete file;
}

File* TarFileFactory::open(const std::string& filename) {
    LDEBUG("Loading file " << filename << " from " << filename_);
    LDEBUG("  Offset: " << files_[filename].offset_ << "; size: " << files_[filename].size_);
    TarFile* tf = new TarFile(filename, filename_, files_[filename].offset_,
                              files_[filename].size_);
    return tf;
}

std::vector<std::string> TarFileFactory::getFilenames() {
    std::vector<std::string> files;

    std::map<std::string, ArchivedFile>::iterator theIterator;
571
    for (theIterator = files_.begin(); theIterator != files_.end(); ++theIterator)
schultezub's avatar
schultezub committed
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
        files.push_back((*theIterator).first);

    return files;
}


//-----------------------------------------------------------------------------

const std::string FileSystem::loggerCat_("tgt.FileSystem.FileSystem");
#ifdef WIN32
const char FileSystem::goodSlash_ = '\\';
const char FileSystem::badSlash_ = '/';
#else
const char FileSystem::goodSlash_ = '/';
const char FileSystem::badSlash_ = '\\';
#endif

FileSystem::FileSystem() {
}

FileSystem::~FileSystem() {
    while (!factories_.empty()) {
        delete (factories_.back());
        factories_.pop_back();
    }
}

File* FileSystem::open(const std::string& filename) {
    if (virtualFS_.find(filename) != virtualFS_.end()) {
        LDEBUG("Opening file " << filename << " from virtualFS");
        return virtualFS_[filename]->open(filename);
    } else {
        LDEBUG("Opening file " << filename << " from real FS");

        RegularFile* f = new RegularFile(filename);
        if (f->isOpen()) {
            return f;
        } else {
            delete f;
            return 0;
        }
    }
}

bool FileSystem::exists(const std::string& filename) {
    if (virtualFS_.find(filename) != virtualFS_.end()) {
        LDEBUG("Not checking if file " << filename << " exists in virtualFS, unimplemented.");
        //FIXME: this does not check if file exists in virtualFS, unimplemented! joerg
        return true;
    } else {
        std::ifstream file(filename.c_str(), std::ios::binary);
        return file.good();
    }
}

void FileSystem::addFactory(FileFactory* ff) {
    std::vector<std::string> files = ff->getFilenames();
    std::vector<std::string>::iterator theIterator;
630
    for (theIterator = files.begin(); theIterator != files.end(); ++theIterator)
schultezub's avatar
schultezub committed
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
1303
1304
        virtualFS_[*theIterator] = ff;

    factories_.push_back(ff);
}

void FileSystem::addMemoryFile(const std::string& filename, char* data, size_t size) {
    FileFactory* ff = new MemoryFileFactory(filename, data, size);
    addFactory(ff);
}

void FileSystem::addMemoryFile(const std::string& filename, const std::string& data) {
    FileFactory* ff = new MemoryFileFactory(filename, data);
    addFactory(ff);
}

void FileSystem::addPackage(const std::string& filename, const std::string& rootpath) {
    LINFO("adding package " << filename);
    LDEBUG("root path: " << rootpath);

    //TODO: do something like TextureReader in TexMgr...
    std::string::size_type loc = filename.find(".tar", 0);
    if (loc != std::string::npos) {
        LDEBUG("Recognized tar file");
        addFactory(new TarFileFactory(filename, rootpath));
    }
}

namespace {

const string PATH_SEPARATORS = "/\\";

std::string replaceAllCharacters(const std::string& name, const char oldChar,
                                 const char newChar)
{
    // the simplest algorithm but it should be fast enough
    //
    std::string conv(name);
    for (size_t i = 0; i < conv.size(); ++i) {
        if (conv[i] == oldChar)
            conv[i] = newChar;
    }
    return conv;
}

size_t removeTrailingCharacters(std::string& str, const char trailer) {
    if (str.empty())
        return 0;

    size_t pos = str.find_last_not_of(trailer);
    if (pos != std::string::npos) {
        size_t count = str.size() - (pos + 1);
        str.resize(pos + 1);
        return count;
    }

    return 0;
}

} // namespace


//
// static methods for the regular filesystem
//

string FileSystem::absolutePath(const string& path) {
    char* buffer;
#ifdef WIN32
    buffer = static_cast<char*>(malloc(4096));
    buffer[0] = 0;
    if (GetFullPathName(path.c_str(), 4096, buffer, 0) == 0) {
        free(buffer);
        buffer = 0;
    }
#elif _POSIX_VERSION >= 200809L || defined (linux)
    // use safe realpath if available
    buffer = realpath(path.c_str(), 0);
#else
    char* resolvedPath = static_cast<char*>(malloc(4096));
    buffer = realpath(path.c_str(), resolvedPath);
    // on success buffer is equal to resolvedPath and gets freed later
    // on failure we have to free resolvedPath
    if (!buffer)
        free(resolvedPath);
#endif

    if (buffer) {
        string result(buffer);
        free(buffer);
        return cleanupPath(result);
    }
    return path;
}

std::string FileSystem::relativePath(const std::string& path, const std::string& dir) {
    // when there is no dir we just return the path
    if (dir.empty())
        return path;

    // make paths absolute and add trailing separator
    string abspath = cleanupPath(absolutePath(path)) + "/";
    string absdir = cleanupPath(absolutePath(dir)) + "/";

    // if both directories are the same we return an empty relative path
    if (abspath.compare(absdir) == 0)
        return "";

    /*
    // catch differing DOS-style drive names
    if (abspath.size() < 1 || abspath.size() < 1 || abspath[0] != absdir[0]) {
        std::transform(abspath.begin(), abspath.begin()+1, abspath.begin(), static_cast<int (*)(int)>(std::tolower));
        std::transform(absdir.begin(), absdir.begin()+1, absdir.begin(), static_cast<int (*)(int)>(std::tolower));
    }*/

    // do not generate a relative patch across different Windows drives
    if (!abspath.empty() && !absdir.empty() && (abspath[0] != absdir[0])) {
        return path;
    }

    // find common part in path and dir string
    string::size_type pospath = abspath.find_first_of(PATH_SEPARATORS);
    string::size_type posdir = absdir.find_first_of(PATH_SEPARATORS);
    size_t i = 0;
    while (abspath.compare(0, pospath, absdir, 0, posdir) == 0) {
        i = pospath;
        pospath = abspath.find_first_of(PATH_SEPARATORS, pospath + 1);
        posdir = absdir.find_first_of(PATH_SEPARATORS, posdir + 1);
    }

    // now we have remaining then non-common parts of both paths
    string restpath = abspath.substr(i + 1);
    string restdir = absdir.substr(i + 1);

    // the remaining path is our initial relative path
    string relative = restpath;

    // add ".." for each path separator in the remaining part of dir
    string::size_type pos = restdir.find_first_of(PATH_SEPARATORS);
    while (pos != string::npos) {
        relative = "../" + relative;
        pos = restdir.find_first_of(PATH_SEPARATORS, pos + 1);
    }

    // cleanup and return result
    return cleanupPath(relative);
}

string FileSystem::fileName(const string& filepath) {
    string::size_type separator = filepath.find_last_of("/\\");
    if (separator != string::npos)
        return filepath.substr(separator + 1);
    else
        return filepath;
}

string FileSystem::baseName(const string& filepath) {
    string filename = fileName(filepath);
    string::size_type dot = filename.rfind(".");

    if (dot != string::npos)
        return filename.substr(0, dot);
    else
        return filename;
}

string FileSystem::fullBaseName(const string& filepath) {
    string::size_type dot = filepath.rfind(".");

    if (dot != string::npos)
        return filepath.substr(0, dot);
    else
        return filepath;
}

string FileSystem::dirName(const std::string& filepath) {
    if (dirExists(filepath)) //< directory passed
        return filepath;

    string::size_type separator = filepath.find_last_of("/\\");
    if (separator != string::npos)
        return filepath.substr(0, separator);
    else
        return "";
}

std::string FileSystem::parentDir(const std::string& dir) {
    std::string curDir = cleanupPath(dir);

    string::size_type separator = curDir.find_last_of("/\\");
    if (separator != string::npos)
        return curDir.substr(0, separator);
    else
        return "";
}

string FileSystem::fileExtension(const string& path, bool lowercase) {
    string filename = fileName(path);

    string::size_type dot = filename.rfind(".");
    string extension;
    if (dot != string::npos)
        extension = filename.substr(dot + 1);

    if (lowercase)
        std::transform(extension.begin(), extension.end(), extension.begin(), tolower);

    return extension;
}

bool FileSystem::comparePaths(const std::string& path1, const std::string& path2) {

    std::string pathAbs1 = cleanupPath(absolutePath(path1));
    std::string pathAbs2 = cleanupPath(absolutePath(path2));

    return (pathAbs1 == pathAbs2);
}

string FileSystem::cleanupPath(std::string path) {
    string::size_type p = 0;
    while (p != string::npos) {
        // check all combinations of path separators
        p = path.find("//");
        if (p == string::npos)
            p = path.find("\\\\");
        if (p == string::npos)
            p = path.find("\\/");
        if (p == string::npos)
            p = path.find("/\\");

        if (p != string::npos)
            path = path.substr(0, p) + path.substr(p + 1);
    }

    // remove trailing separator
    if (path.find_last_of(PATH_SEPARATORS) == path.size() - 1)
        path = path.substr(0, path.size() - 1);

#ifdef WIN32
    // convert to native windows separators
    path = replaceAllCharacters(path, '/', '\\');
    // convert drive letter to uppercase
    if ((path.size()>1) && isalpha(path[0]) && (path[1] == ':'))
        std::transform(path.begin(), path.begin()+1, path.begin(), toupper);
#endif

    return path;
}

std::vector<std::string> FileSystem::splitPath(std::string path) {
    path = cleanupPath(path);

    // split by goodSlash_
    std::vector<std::string> components;
    std::stringstream stream(path);
    std::string item;
    while(std::getline(stream, item, goodSlash_)) {
        components.push_back(item);
    }
    return components;
}

std::string FileSystem::currentDirectory() {
#ifdef WIN32
    char* buffer = new char[MAX_PATH + 1];
    memset(buffer, 0, MAX_PATH + 1);

    DWORD size = GetCurrentDirectory(MAX_PATH, buffer);
    if (size >= MAX_PATH) {
        delete [] buffer;
        buffer = new char[size + 1];
        memset(buffer, 0, size + 1);
        size = GetCurrentDirectory(size, buffer);
    }
    std::string dir(buffer);
    dir.resize(size);
    delete[] buffer;

    return dir;
#else
    char path[FILENAME_MAX];
    if (getcwd(path, FILENAME_MAX) == 0)
        return "";
    else
        return std::string(path);
#endif
}

bool FileSystem::changeDirectory(const std::string& directory) {
    if (directory.empty())
        return false;

    std::string converted = replaceAllCharacters(directory, badSlash_, goodSlash_);
    removeTrailingCharacters(converted, goodSlash_);
#ifdef WIN32
    return (_chdir(converted.c_str()) == 0);
#else
    return (chdir(converted.c_str()) == 0);
#endif
}

bool FileSystem::createDirectory(const std::string& directory) {
    if (directory.empty())
        return false;

    std::string converted = replaceAllCharacters(directory, badSlash_, goodSlash_);
    removeTrailingCharacters(converted, goodSlash_);

#ifdef WIN32
    return (CreateDirectory(converted.c_str(), 0) != 0);
#else
    return (mkdir(converted.c_str(), 0777) == 0);
#endif
}

bool FileSystem::createDirectoryRecursive(const std::string& directory) {
    if(dirExists(directory))
        return true;
    else {
        if(createDirectoryRecursive(parentDir(directory))) {
            return createDirectory(directory);
        }
        else
            return false;
    }
}

bool FileSystem::deleteDirectory(const std::string& directory) {
    if (directory.empty())
        return false;

    std::string converted = replaceAllCharacters(directory, badSlash_, goodSlash_);
    removeTrailingCharacters(converted, goodSlash_);
#ifdef WIN32
    return (_rmdir(converted.c_str()) == 0);
#else
    return (rmdir(converted.c_str()) == 0);
#endif
}

bool FileSystem::deleteDirectoryRecursive(const std::string& directory) {
    if (directory.empty())
        return false;

    bool success = clearDirectory(directory);
    success &= deleteDirectory(directory);

    return success;
}

bool FileSystem::clearDirectory(const std::string& directory) {
    if (directory.empty())
        return false;

    std::string converted = replaceAllCharacters(directory, badSlash_, goodSlash_);
    removeTrailingCharacters(converted, goodSlash_);

    bool success = true;

    //recursively delete all subdirectories
    std::vector<std::string> subDirs = listSubDirectories(converted);
    for(size_t i=0; i<subDirs.size(); i++) {
        success &= deleteDirectoryRecursive(converted + goodSlash_ + subDirs[i]);
    }
    //delete all files
    std::vector<std::string> files = listFiles(converted);
    for(size_t i=0; i<files.size(); i++) {
        success &= deleteFile(converted + goodSlash_ + files[i]);
    }

    return success;
}

bool FileSystem::deleteFile(const std::string& filename) {
    if (filename.empty())
        return false;

    std::string converted = replaceAllCharacters(filename, badSlash_, goodSlash_);
    removeTrailingCharacters(converted, goodSlash_);

#ifdef WIN32
    return (DeleteFile(converted.c_str()) != 0);
#else
    return (remove(converted.c_str()) == 0);
#endif
}

bool FileSystem::renameFile(const std::string& filename, const std::string& newName,
                            bool ignorePath)
{
    if (FileSystem::fileExists(filename) == false)
        return false;

    std::string converted = replaceAllCharacters(filename, badSlash_, goodSlash_);
    std::string convertedNew = replaceAllCharacters(newName, badSlash_, goodSlash_);
    removeTrailingCharacters(converted, goodSlash_);
    removeTrailingCharacters(convertedNew, goodSlash_);

    bool res = false;
    if (ignorePath == true) {
        std::string name = (FileSystem::dirName(filename) + std::string(&goodSlash_, 1)
            + FileSystem::fileName(newName));
        res = (rename(converted.c_str(), name.c_str()) == 0);
    } else {
        res = (rename(converted.c_str(), convertedNew.c_str()) == 0);
    }
    return res;
}

void FileSystem::copyFile(const std::string& srcFile, const std::string& destFile) throw (tgt::Exception) {
    // check if input file/output dir exists
    if (!fileExists(srcFile))
        throw tgt::FileNotFoundException("srcFile not found", srcFile);
    std::string destDir = dirName(destFile);
    if (destDir != "" && !dirExists(destDir))
        throw tgt::FileNotFoundException("destination directory not found", destDir);

    // open input and output files
    std::ifstream src(srcFile.c_str(), std::ios::in | std::ios::binary);
    if (!src.good())
        throw tgt::FileAccessException("failed to open srcFile fir reading", srcFile);
    std::ofstream dest(destFile.c_str(), std::ios::trunc | std::ios::binary);
    if (!dest.good())
        throw tgt::FileAccessException("failed to open dest file for writing", destFile);

    // copy data
    dest << src.rdbuf();

    // check if copying succeeded
    if (!src.good())
        throw tgt::FileException("reading from source file failed", srcFile);
    if (!dest.good())
        throw tgt::FileException("writing to dest file failed", destFile);
}

bool FileSystem::fileExists(const std::string& filename) {
    if (filename.empty())
        return false;

    std::string converted = replaceAllCharacters(filename, badSlash_, goodSlash_);
    removeTrailingCharacters(converted, goodSlash_);

    struct stat st;
    return (stat(converted.c_str(), &st) == 0);
}

uint64_t FileSystem::dirSize(const std::string& directory, const bool recursive) {
    std::vector<std::string> files;
    if(recursive)
        files = listFilesRecursive(directory, false);
    else
        files = listFiles(directory, false);

    uint64_t size = 0;
    for(size_t i=0; i<files.size(); i++) {
        size += fileSize(directory + goodSlash_ + files[i]);
    }
    return size;
}

uint64_t FileSystem::fileSize(const std::string& filename) {
    if (filename.empty())
        return 0;

    std::string converted = replaceAllCharacters(filename, badSlash_, goodSlash_);
    removeTrailingCharacters(converted, goodSlash_);

    struct stat st;
    if(stat(converted.c_str(), &st) == 0) {
        return st.st_size;
    }
    else {
        // error
        return 0;
    }
}

time_t FileSystem::fileTime(const std::string& filename) {
    if (filename.empty())
        return 0;

    std::string converted = replaceAllCharacters(filename, badSlash_, goodSlash_);
    removeTrailingCharacters(converted, goodSlash_);

    struct stat st;
    if(stat(converted.c_str(), &st) == 0) {
        return st.st_mtime;
    }
    else {
        // error
        return 0;
    }
}

bool FileSystem::dirExists(const string& dirpath) {
#ifdef WIN32
    DWORD result = GetFileAttributes(dirpath.c_str());
    return (result != INVALID_FILE_ATTRIBUTES) && (result & FILE_ATTRIBUTE_DIRECTORY);
#else
    DIR* dir = opendir(dirpath.c_str());
    if (dir) {
        closedir(dir);
        return true;
    }
    else
        return false;
#endif
}

std::vector<std::string> FileSystem::readDirectory(const std::string& directory, const bool sort,
                                                   const bool recursiveSearch) {
    if(recursiveSearch)
        return listFilesRecursive(directory, sort);
    else
        return listFiles(directory, sort);
}

#ifdef _WIN32
std::vector<std::string> FileSystem::listFiles(const std::string& directory, const bool sort) {
    std::vector<std::string> result;
    if (directory.empty())
        return result;

    std::string converted = replaceAllCharacters(directory, badSlash_, goodSlash_);
    removeTrailingCharacters(converted, goodSlash_);

    WIN32_FIND_DATA findFileData = {0};
    HANDLE hFind = 0;

    std::string dir(converted + "\\*");

    hFind = FindFirstFile(dir.c_str(), &findFileData);
    if (hFind != INVALID_HANDLE_VALUE) {
        do {
            std::string file(findFileData.cFileName);
            if (! (findFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)) {
                    result.push_back(file);
            }
        } while (FindNextFile(hFind, &findFileData) != 0);
    }
    FindClose(hFind);

    if (sort)
        std::sort(result.begin(), result.end());

    return result;
}
#else
std::vector<std::string> FileSystem::listFiles(const std::string& directory, const bool sort) {
    std::vector<std::string> result;

    // POSIX directory listing
    DIR *dir;
    struct dirent *ent;
    if ((dir= opendir(directory.c_str())) != NULL) {
        std::string name;
        while ((ent = readdir(dir))) {
            name = ent->d_name;
            if ((name != ".") && (name != "..")) {
                if(ent->d_type != DT_DIR)
                    result.push_back(ent->d_name);
            }
        }
    }
    closedir(dir);

    if (sort)
        std::sort(result.begin(), result.end());

    return result;
}
#endif

std::vector<std::string> FileSystem::listFilesRecursive(const std::string& directory, const bool sort) {
    std::vector<std::string> result;
    if (directory.empty())
        return result;

    result = listFiles(directory, false);

    std::vector<std::string> subDirs = listSubDirectories(directory, false);
    for(size_t i=0; i<subDirs.size(); i++) {
        std::vector<std::string> files = listFilesRecursive(directory + "/" + subDirs[i], false);
        for(size_t j=0; j<files.size(); j++) {
            result.push_back(subDirs[i] + "/" +files[j]);
        }
    }

    if (sort)
        std::sort(result.begin(), result.end());

    return result;
}

#ifdef _WIN32
std::vector<std::string> FileSystem::listSubDirectories(const std::string& directory, const bool sort) {
    std::vector<std::string> result;
    if (directory.empty())
        return result;

    std::string converted = replaceAllCharacters(directory, badSlash_, goodSlash_);
    removeTrailingCharacters(converted, goodSlash_);

    WIN32_FIND_DATA findFileData = {0};
    HANDLE hFind = 0;

    std::string dir(converted + "\\*");

    hFind = FindFirstFile(dir.c_str(), &findFileData);
    if (hFind != INVALID_HANDLE_VALUE) {
        do {
            std::string file(findFileData.cFileName);
            if ((file != ".") && (file != "..")) {
                if (findFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
                        result.push_back(file);
                }
            }
        } while (FindNextFile(hFind, &findFileData) != 0);
    }
    FindClose(hFind);

    if (sort)
        std::sort(result.begin(), result.end());

    return result;
}
#else
std::vector<std::string> FileSystem::listSubDirectories(const std::string& directory, const bool sort) {
    std::vector<std::string> result;

    // POSIX directory listing
    DIR *dir;
    struct dirent *ent;
    if ((dir= opendir(directory.c_str())) != NULL) {
        std::string name;
        while ((ent = readdir(dir))) {
            name = ent->d_name;
            if ((name != ".") && (name != "..")) {
                if(ent->d_type == DT_DIR)
                    result.push_back(ent->d_name);
            }
        }
    }
    closedir(dir);

    if (sort)
        std::sort(result.begin(), result.end());

    return result;
}
#endif

std::vector<std::string> FileSystem::listSubDirectoriesRecursive(const std::string& directory, const bool sort) {
    std::vector<std::string> result;
    if (directory.empty())
        return result;

    result = listSubDirectories(directory, false);

    std::vector<std::string> subDirs = result;
    for(size_t i=0; i<subDirs.size(); i++) {
        std::vector<std::string> dirs = listSubDirectoriesRecursive(directory + "/" + subDirs[i], false);
        for(size_t j=0; j<dirs.size(); j++) {
            result.push_back(subDirs[i] + "/" + dirs[j]);
        }
    }

    if (sort)
        std::sort(result.begin(), result.end());

    return result;
}


} // namespace