summaryrefslogtreecommitdiff
path: root/cmake/platforms/windows/filemgr.cpp
blob: 22058250ab04de6890c191e3fa8b82688841978b (plain)
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
/******************************************************************************
 *  filemgr.cpp    - implementation of class FileMgr used for pooling file
 *                      handles
 *
 * $Id: filemgr.cpp 2245 2009-02-10 23:22:28Z scribe $
 *
 * Copyright 1998 CrossWire Bible Society (http://www.crosswire.org)
 *    CrossWire Bible Society
 *    P. O. Box 2528
 *    Tempe, AZ  85280-2528
 *
 * 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 version 2.
 *
 * 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.
 *
 */

#include <filemgr.h>
#include <utilstr.h>

#include <dirent.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <stdio.h>
#include <string.h>
#include <swbuf.h>
#if !defined(__GNUC__) && !defined(_WIN32_WCE)
#include <io.h>
#include <direct.h>
#else
#include <unistd.h>
#endif


#ifndef O_BINARY
#define O_BINARY 0
#endif

#ifndef S_IRGRP
#define S_IRGRP 0
#endif

#ifndef S_IROTH
#define S_IROTH 0
#endif

// Fix for VC6
#ifndef S_IREAD
#ifdef _S_IREAD
#define S_IREAD _S_IREAD
#define S_IWRITE _S_IWRITE
#endif
#endif
// -----------


SWORD_NAMESPACE_START


int FileMgr::CREAT = O_CREAT;
int FileMgr::APPEND = O_APPEND;
int FileMgr::TRUNC = O_TRUNC;
int FileMgr::RDONLY = O_RDONLY;
int FileMgr::RDWR = O_RDWR;
int FileMgr::WRONLY = O_WRONLY;
int FileMgr::IREAD = S_IREAD;
int FileMgr::IWRITE = S_IWRITE;


// ---------------- statics -----------------
FileMgr *FileMgr::systemFileMgr = 0;

class __staticsystemFileMgr {
public:
    __staticsystemFileMgr() { }
    ~__staticsystemFileMgr() { delete FileMgr::systemFileMgr; }
} _staticsystemFileMgr;


FileMgr *FileMgr::getSystemFileMgr() {
    if (!systemFileMgr)
        systemFileMgr = new FileMgr();

    return systemFileMgr;
}


void FileMgr::setSystemFileMgr(FileMgr *newFileMgr) {
    if (systemFileMgr)
        delete systemFileMgr;
    systemFileMgr = newFileMgr;
}

// --------------- end statics --------------


FileDesc::FileDesc(FileMgr *parent, const char *path, int mode, int perms, bool tryDowngrade) {
    this->parent = parent;
    this->path = 0;
    stdstr(&this->path, path);
    this->mode = mode;
    this->perms = perms;
    this->tryDowngrade = tryDowngrade;
    offset = 0;
    fd = -77;
}


FileDesc::~FileDesc() {
    if (fd > 0)
        close(fd);

    if (path)
        delete [] path;
}


int FileDesc::getFd() {
    if (fd == -77)
        fd = parent->sysOpen(this);
//    if ((fd < -1) && (fd != -77))  // kludge to hand ce
//        return 777;
    return fd;
}


long FileDesc::seek(long offset, int whence) {
    return lseek(getFd(), offset, whence);
}


long FileDesc::read(void *buf, long count) {
    int fd = getFd();
    if (fd < 0) {
        return 0;
    }
    return ::read(fd, buf, count);
}


long FileDesc::write(const void *buf, long count) {
    return ::write(getFd(), buf, count);
}


FileMgr::FileMgr(int maxFiles) {
    this->maxFiles = maxFiles;        // must be at least 2
    files = 0;
}


FileMgr::~FileMgr() {
    FileDesc *tmp;

    while(files) {
        tmp = files->next;
        delete files;
        files = tmp;
    }
}


FileDesc *FileMgr::open(const char *path, int mode, bool tryDowngrade) {
    return open(path, mode, S_IREAD|S_IWRITE|S_IRGRP|S_IROTH, tryDowngrade);
}


FileDesc *FileMgr::open(const char *path, int mode, int perms, bool tryDowngrade) {
    FileDesc **tmp, *tmp2;

    for (tmp = &files; *tmp; tmp = &((*tmp)->next)) {
        if ((*tmp)->fd < 0)        // insert as first non-system_open file
            break;
    }

    tmp2 = new FileDesc(this, path, mode, perms, tryDowngrade);
    tmp2->next = *tmp;
    *tmp = tmp2;

    return tmp2;
}


void FileMgr::close(FileDesc *file) {
    FileDesc **loop;

    for (loop = &files; *loop; loop = &((*loop)->next)) {
        if (*loop == file) {
            *loop = (*loop)->next;
            delete file;
            break;
        }
    }
}


int FileMgr::sysOpen(FileDesc *file) {
    FileDesc **loop;
    int openCount = 1;        // because we are presently opening 1 file, and we need to be sure to close files to accomodate, if necessary

    for (loop = &files; *loop; loop = &((*loop)->next)) {

        if ((*loop)->fd > 0) {
            if (++openCount > maxFiles) {
                (*loop)->offset = lseek((*loop)->fd, 0, SEEK_CUR);
                ::close((*loop)->fd);
                (*loop)->fd = -77;
            }
        }

        if (*loop == file) {
            if (*loop != files) {
                *loop = (*loop)->next;
                file->next = files;
                files = file;
            }
            if ((!access(file->path, 04)) || ((file->mode & O_CREAT) == O_CREAT)) {    // check for at least file exists / read access before we try to open
                char tries = (((file->mode & O_RDWR) == O_RDWR) && (file->tryDowngrade)) ? 2 : 1;  // try read/write if possible
                for (int i = 0; i < tries; i++) {
                    if (i > 0) {
                        file->mode = (file->mode & ~O_RDWR);    // remove write access
                        file->mode = (file->mode | O_RDONLY);// add read access
                    }
                    file->fd = ::open(file->path, file->mode|O_BINARY, file->perms);

                    if (file->fd >= 0)
                        break;
                }

                if (file->fd >= 0)
                    lseek(file->fd, file->offset, SEEK_SET);
            }
            else file->fd = -1;
            if (!*loop)
                break;
        }
    }
    return file->fd;
}


// to truncate a file at its current position
// leaving byte at current possition intact
// deleting everything afterward.
signed char FileMgr::trunc(FileDesc *file) {

    static const char *writeTest = "x";
    long size = file->seek(1, SEEK_CUR);
    if (size == 1) // was empty
        size = 0;
    char nibble [ 32767 ];
    bool writable = file->write(writeTest, 1);
    int bytes = 0;

    if (writable) {
        // get tmpfilename
        char *buf = new char [ strlen(file->path) + 10 ];
        int i;
        for (i = 0; i < 9999; i++) {
            sprintf(buf, "%stmp%.4d", file->path, i);
            if (!existsFile(buf))
                break;
        }
        if (i == 9999)
            return -2;

        int fd = ::open(buf, O_CREAT|O_RDWR, S_IREAD|S_IWRITE|S_IRGRP|S_IROTH);
        if (fd < 0)
            return -3;

        file->seek(0, SEEK_SET);
        while (size > 0) {
            bytes = file->read(nibble, 32767);
            write(fd, nibble, (bytes < size)?bytes:size);
            size -= bytes;
        }
        // zero out the file
        ::close(file->fd);
        file->fd = ::open(file->path, O_TRUNC, S_IREAD|S_IWRITE|S_IRGRP|S_IROTH);
        ::close(file->fd);
        file->fd = -77;    // force file open by filemgr
        // copy tmp file back (dumb, but must preserve file permissions)
        lseek(fd, 0, SEEK_SET);
        do {
            bytes = read(fd, nibble, 32767);
            file->write(nibble, bytes);
        } while (bytes == 32767);

        ::close(fd);
        ::close(file->fd);
        removeFile(buf);        // remove our tmp file
        file->fd = -77;    // causes file to be swapped out forcing open on next call to getFd()
    }
    else { // put offset back and return failure
        file->seek(-1, SEEK_CUR);
        return -1;
    }
    return 0;
}


signed char FileMgr::existsFile(const char *ipath, const char *ifileName)
{
    int len = strlen(ipath) + ((ifileName)?strlen(ifileName):0) + 3;
    char *ch;
    char *path = new char [ len ];
    strcpy(path, ipath);

    if ((path[strlen(path)-1] == '\\') || (path[strlen(path)-1] == '/'))
        path[strlen(path)-1] = 0;

    if (ifileName) {
        ch = path + strlen(path);
        sprintf(ch, "/%s", ifileName);
    }
    signed char retVal = !access(path, 04);
    delete [] path;
    return retVal;
}


signed char FileMgr::existsDir(const char *ipath, const char *idirName)
{
    char *ch;
    int len = strlen(ipath) + ((idirName)?strlen(idirName):0) + 1;
    if (idirName)
        len +=  strlen(idirName);
    char *path = new char [ len ];
    strcpy(path, ipath);

    if ((path[strlen(path)-1] == '\\') || (path[strlen(path)-1] == '/'))
        path[strlen(path)-1] = 0;

    if (idirName) {
        ch = path + strlen(path);
        sprintf(ch, "/%s", idirName);
    }
    signed char retVal = !access(path, 04);
    delete [] path;
    return retVal;
}


int FileMgr::createParent(const char *pName) {
    char *buf = new char [ strlen(pName) + 1 ];
    int retCode = 0;

    strcpy(buf, pName);
    int end = strlen(buf) - 1;
    while (end) {
        if ((buf[end] == '/') || (buf[end] == '\\'))
            break;
        end--;
    }
    buf[end] = 0;
    if (strlen(buf)>0) {
        if (access(buf, 02)) {  // not exists with write access?
            if ((retCode = mkdir(buf
#ifndef WIN32
                    , 0755
#endif
                    ))) {
                createParent(buf);
                retCode = mkdir(buf
#ifndef WIN32
                    , 0755
#endif
                    );
            }
        }
    }
    else retCode = -1;
    delete [] buf;
    return retCode;
}


int FileMgr::openFileReadOnly(const char *fName) {
    int fd = ::open(fName, O_RDONLY|O_BINARY, S_IREAD|S_IWRITE|S_IRGRP|S_IROTH);
    return fd;
}


int FileMgr::createPathAndFile(const char *fName) {
    int fd;

    fd = ::open(fName, O_CREAT|O_WRONLY|O_BINARY, S_IREAD|S_IWRITE|S_IRGRP|S_IROTH);
    if (fd < 1) {
        createParent(fName);
        fd = ::open(fName, O_CREAT|O_WRONLY|O_BINARY, S_IREAD|S_IWRITE|S_IRGRP|S_IROTH);
    }
    return fd;
}


int FileMgr::copyFile(const char *sourceFile, const char *targetFile) {
    int sfd, dfd, len;
    char buf[4096];

    if ((sfd = ::open(sourceFile, O_RDONLY|O_BINARY, S_IREAD|S_IWRITE|S_IRGRP|S_IROTH)) < 1)
        return -1;
    if ((dfd = createPathAndFile(targetFile)) < 1)
        return -1;

    do {
        len = read(sfd, buf, 4096);
        write(dfd, buf, len);
    }
    while(len == 4096);
    ::close(dfd);
    ::close(sfd);

    return 0;
}


int FileMgr::removeFile(const char *fName) {
    return ::remove(fName);
}

char FileMgr::getLine(FileDesc *fDesc, SWBuf &line) {
    int len;
    bool more = true;
    char chunk[255];

    line = "";

    // assert we have a valid file handle
    if (fDesc->getFd() < 1)
        return 0;

    while (more) {
        more = false;
        long index = fDesc->seek(0, SEEK_CUR);
        len = fDesc->read(chunk, 254);

        // assert we have a readable file (not a directory)
        if (len < 1)
            break;

        int start = 0;
        // clean up any preceding white space if we're at the beginning of line
        if (!line.length()) {
            for (;start < len; start++) {
                if ((chunk[start] != 13) && (chunk[start] != ' ') && (chunk[start] != '\t'))
                    break;
            }
        }

        // find the end
        int end;
        for (end = start; ((end < (len-1)) && (chunk[end] != 10)); end++);

        if ((chunk[end] != 10) && (len == 254)) {
            more = true;
        }
        index += (end + 1);

        // reposition to next valid place to read
        fDesc->seek(index, SEEK_SET);

        // clean up any trailing junk on line if we're at the end
        if (!more) {
            for (; end > start; end--) {
                if ((chunk[end] != 10) && (chunk[end] != 13) && (chunk[end] != ' ') && (chunk[end] != '\t')) {
                    if (chunk[end] == '\\') {
                        more = true;
                        end--;
                    }
                    break;
                }
            }
        }

        int size = (end - start) + 1;

        if (size > 0) {
            // line.appendFormatted("%.*s", size, chunk+start);
            line.append(chunk+start, size);
        }
    }
    return ((len > 0) || line.length());
}


char FileMgr::isDirectory(const char *path) {
    struct stat stats;
    if (stat(path, &stats))
        return 0;
    return ((stats.st_mode & S_IFDIR) == S_IFDIR);
}


int FileMgr::copyDir(const char *srcDir, const char *destDir) {
    DIR *dir;
    struct dirent *ent;
    if ((dir = opendir(srcDir))) {
        rewinddir(dir);
        while ((ent = readdir(dir))) {
            if ((strcmp(ent->d_name, ".")) && (strcmp(ent->d_name, ".."))) {
                SWBuf srcPath  = (SWBuf)srcDir  + (SWBuf)"/" + ent->d_name;
                SWBuf destPath = (SWBuf)destDir + (SWBuf)"/" + ent->d_name;
                if (!isDirectory(srcPath.c_str())) {
                    copyFile(srcPath.c_str(), destPath.c_str());
                }
                else {
                    copyDir(srcPath.c_str(), destPath.c_str());
                }
            }
        }
        closedir(dir);
    }
    return 0;
}


int FileMgr::removeDir(const char *targetDir) {
    DIR *dir = opendir(targetDir);
    struct dirent *ent;
    if (dir) {
        rewinddir(dir);
        while ((ent = readdir(dir))) {
            if ((strcmp(ent->d_name, ".")) && (strcmp(ent->d_name, ".."))) {
                SWBuf targetPath = (SWBuf)targetDir + (SWBuf)"/" + ent->d_name;
                if (!isDirectory(targetPath.c_str())) {
                    FileMgr::removeFile(targetPath.c_str());
                }
                else {
                    removeDir(targetPath.c_str());
                }
            }
        }
        closedir(dir);
        removeFile(targetDir);
    }
    return 0;
}


void FileMgr::flush() {
    FileDesc **loop;

    for (loop = &files; *loop; loop = &((*loop)->next)) {
        if ((*loop)->fd > 0) {
            (*loop)->offset = lseek((*loop)->fd, 0, SEEK_CUR);
            ::close((*loop)->fd);
            (*loop)->fd = -77;
        }
    }
}

long FileMgr::resourceConsumption() {
    long count = 0;
    FileDesc **loop;
    for (loop = &files; *loop; loop = &((*loop)->next)) {
        if ((*loop)->fd > 0) {
            count++;
        }
    }
    return count;
}


SWORD_NAMESPACE_END