summaryrefslogtreecommitdiff
path: root/src/libaudcore/vfs_local.c
blob: d91317a4166858bda3ce72625a9c9e87d5ce60a9 (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
/*
 * vfs_local.c
 * Copyright 2009-2014 John Lindgren
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * 1. Redistributions of source code must retain the above copyright notice,
 *    this list of conditions, and the following disclaimer.
 *
 * 2. Redistributions in binary form must reproduce the above copyright notice,
 *    this list of conditions, and the following disclaimer in the documentation
 *    provided with the distribution.
 *
 * This software is provided "as is" and without any warranty, express or
 * implied. In no event shall the authors be liable for any damages arising from
 * the use of this software.
 */

#include "vfs_local.h"

#include <errno.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>

#include <glib.h>
#include <glib/gstdio.h>

#include "audstrings.h"

#ifdef _WIN32
#define fseeko fseeko64
#define ftello ftello64
#endif

typedef enum {
    OP_NONE,
    OP_READ,
    OP_WRITE
} LocalOp;

typedef struct {
    char * path;
    FILE * stream;
    int64_t cached_size;
    LocalOp last_op;
} LocalFile;

static void * local_fopen (const char * uri, const char * mode)
{
    char * path = uri_to_filename (uri);
    g_return_val_if_fail (path, NULL);

    const char * suffix = "";

#ifdef _WIN32
    if (! strchr (mode, 'b'))  /* binary mode (Windows) */
        suffix = "b";
#else
    if (! strchr (mode, 'e'))  /* close on exec (POSIX) */
        suffix = "e";
#endif

    SCONCAT2 (mode2, mode, suffix);

    FILE * stream = g_fopen (path, mode2);

    if (! stream)
    {
        perror (path);
        str_unref (path);
        return NULL;
    }

    LocalFile * local = g_slice_new (LocalFile);

    local->path = path;
    local->stream = stream;
    local->cached_size = -1;
    local->last_op = OP_NONE;

    return local;
}

static int local_fclose (VFSFile * file)
{
    LocalFile * local = vfs_get_handle (file);

    int result = fclose (local->stream);
    if (result < 0)
        perror (local->path);

    str_unref (local->path);
    g_slice_free (LocalFile, local);

    return result;
}

static int64_t local_fread (void * ptr, int64_t size, int64_t nitems, VFSFile * file)
{
    LocalFile * local = vfs_get_handle (file);

    if (local->last_op == OP_WRITE)
    {
        if (fseeko (local->stream, 0, SEEK_CUR) < 0)  /* flush buffers */
        {
            perror (local->path);
            return 0;
        }
    }

    local->last_op = OP_READ;

    clearerr (local->stream);

    int64_t result = fread (ptr, size, nitems, local->stream);
    if (result < nitems && ferror (local->stream))
        perror (local->path);

    return result;
}

static int64_t local_fwrite (const void * ptr, int64_t size, int64_t nitems, VFSFile * file)
{
    LocalFile * local = vfs_get_handle (file);

    if (local->last_op == OP_READ)
    {
        if (fseeko (local->stream, 0, SEEK_CUR) < 0)  /* flush buffers */
        {
            perror (local->path);
            return 0;
        }
    }

    local->last_op = OP_WRITE;
    local->cached_size = -1;

    clearerr (local->stream);

    int64_t result = fwrite (ptr, size, nitems, local->stream);
    if (result < nitems && ferror (local->stream))
        perror (local->path);

    return result;
}

static int local_fseek (VFSFile * file, int64_t offset, int whence)
{
    LocalFile * local = vfs_get_handle (file);

    int result = fseeko (local->stream, offset, whence);
    if (result < 0)
        perror (local->path);

    if (result == 0)
        local->last_op = OP_NONE;

    return result;
}

static int64_t local_ftell (VFSFile * file)
{
    LocalFile * local = vfs_get_handle (file);
    return ftello (local->stream);
}

static bool_t local_feof (VFSFile * file)
{
    LocalFile * local = vfs_get_handle (file);
    return feof (local->stream);
}

static int local_ftruncate (VFSFile * file, int64_t length)
{
    LocalFile * local = vfs_get_handle (file);

    if (local->last_op != OP_NONE)
    {
        if (fseeko (local->stream, 0, SEEK_CUR) < 0)  /* flush buffers */
        {
            perror (local->path);
            return 0;
        }
    }

    int result = ftruncate (fileno (local->stream), length);
    if (result < 0)
        perror (local->path);

    if (result == 0)
    {
        local->last_op = OP_NONE;
        local->cached_size = length;
    }

    return result;
}

static int64_t local_fsize (VFSFile * file)
{
    LocalFile * local = vfs_get_handle (file);

    if (local->cached_size >= 0)
        return local->cached_size;

    int64_t saved_pos = ftello (local->stream);

    if (local_fseek (file, 0, SEEK_END) < 0)
        return -1;

    int64_t length = ftello (local->stream);

    if (local_fseek (file, saved_pos, SEEK_SET) < 0)
        return -1;

    return length;
}

VFSConstructor vfs_local_vtable = {
    .vfs_fopen_impl = local_fopen,
    .vfs_fclose_impl = local_fclose,
    .vfs_fread_impl = local_fread,
    .vfs_fwrite_impl = local_fwrite,
    .vfs_fseek_impl = local_fseek,
    .vfs_ftell_impl = local_ftell,
    .vfs_feof_impl = local_feof,
    .vfs_ftruncate_impl = local_ftruncate,
    .vfs_fsize_impl = local_fsize
};