summaryrefslogtreecommitdiff
path: root/src/w_file_posix.c
blob: c193ecb35090db445ba6e7143763058b9d75a713 (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
//
// Copyright(C) 1993-1996 Id Software, Inc.
// Copyright(C) 2005-2014 Simon Howard
//
// 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.
//
// DESCRIPTION:
//	WAD I/O functions.
//

#include "config.h"

#ifdef HAVE_MMAP

#include <errno.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/mman.h>
#include <string.h>

#include "w_file.h"
#include "z_zone.h"

typedef struct
{
    wad_file_t wad;
    int handle;
} posix_wad_file_t;

extern wad_file_class_t posix_wad_file;

static void MapFile(posix_wad_file_t *wad, char *filename)
{
    void *result;
    int protection;
    int flags;

    // Mapped area can be read and written to.  Ideally
    // this should be read-only, as none of the Doom code should 
    // change the WAD files after being read.  However, there may
    // be code lurking in the source that does.

    protection = PROT_READ|PROT_WRITE;

    // Writes to the mapped area result in private changes that are
    // *not* written to disk.

    flags = MAP_PRIVATE;

    result = mmap(NULL, wad->wad.length,
                  protection, flags, 
                  wad->handle, 0);

    wad->wad.mapped = result;

    if (result == NULL)
    {
        fprintf(stderr, "W_POSIX_OpenFile: Unable to mmap() %s - %s\n",
                        filename, strerror(errno));
    }
}

unsigned int GetFileLength(int handle)
{
    return lseek(handle, 0, SEEK_END);
}
   
static wad_file_t *W_POSIX_OpenFile(char *path)
{
    posix_wad_file_t *result;
    int handle;

    handle = open(path, 0);

    if (handle < 0)
    {
        return NULL;
    }

    // Create a new posix_wad_file_t to hold the file handle.

    result = Z_Malloc(sizeof(posix_wad_file_t), PU_STATIC, 0);
    result->wad.file_class = &posix_wad_file;
    result->wad.length = GetFileLength(handle);
    result->handle = handle;

    // Try to map the file into memory with mmap:

    MapFile(result, path);

    return &result->wad;
}

static void W_POSIX_CloseFile(wad_file_t *wad)
{
    posix_wad_file_t *posix_wad;

    posix_wad = (posix_wad_file_t *) wad;

    // If mapped, unmap it.

    // Close the file
  
    close(posix_wad->handle);
    Z_Free(posix_wad);
}

// Read data from the specified position in the file into the 
// provided buffer.  Returns the number of bytes read.

size_t W_POSIX_Read(wad_file_t *wad, unsigned int offset,
                   void *buffer, size_t buffer_len)
{
    posix_wad_file_t *posix_wad;
    byte *byte_buffer;
    size_t bytes_read;
    int result;

    posix_wad = (posix_wad_file_t *) wad;

    // Jump to the specified position in the file.

    lseek(posix_wad->handle, offset, SEEK_SET);

    // Read into the buffer.

    bytes_read = 0;
    byte_buffer = buffer;

    while (buffer_len > 0) {
        result = read(posix_wad->handle, byte_buffer, buffer_len);

        if (result < 0) {
            perror("W_POSIX_Read");
            break;
        } else if (result == 0) {
            break;
        }

        // Successfully read some bytes

        byte_buffer += result;
        buffer_len -= result;
        bytes_read += result;
    }

    return bytes_read;
}


wad_file_class_t posix_wad_file = 
{
    W_POSIX_OpenFile,
    W_POSIX_CloseFile,
    W_POSIX_Read,
};


#endif /* #ifdef HAVE_MMAP */