summaryrefslogtreecommitdiff
path: root/src/libaudcore/vfs_local.cc
blob: 7af685f3ff4f1728bbbede2acd152929a21a0f94 (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
/*
 * 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 <errno.h>
#include <string.h>
#include <unistd.h>

#include <glib/gstdio.h>

/* needs to be after system headers for #undef's to take effect */
#define WANT_VFS_STDIO_COMPAT
#include "vfs_local.h"

#include "audstrings.h"
#include "i18n.h"
#include "runtime.h"

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

#define perror(s) AUDERR ("%s: %s\n", (const char *) (s), strerror (errno))

enum LocalOp {
    OP_NONE,
    OP_READ,
    OP_WRITE
};

class LocalFile : public VFSImpl
{
public:
    LocalFile (const char * path, FILE * stream) :
        m_path (path),
        m_stream (stream),
        m_cached_pos (0),
        m_cached_size (-1),
        m_last_op (OP_NONE) {}

    ~LocalFile ();

protected:
    int64_t fread (void * ptr, int64_t size, int64_t nmemb);
    int fseek (int64_t offset, VFSSeekType whence);

    int64_t ftell ();
    int64_t fsize ();
    bool feof ();

    int64_t fwrite (const void * ptr, int64_t size, int64_t nmemb);
    int ftruncate (int64_t length);
    int fflush ();

private:
    String m_path;
    FILE * m_stream;
    int64_t m_cached_pos;
    int64_t m_cached_size;
    LocalOp m_last_op;
};

VFSImpl * LocalTransport::fopen (const char * uri, const char * mode, String & error)
{
    StringBuf path = uri_to_filename (uri);

    if (! path)
    {
        error = String (_("Invalid file name"));
        return nullptr;
    }

    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

    StringBuf mode2 = str_concat ({mode, suffix});

    FILE * stream = ::g_fopen (path, mode2);

    if (! stream)
    {
        int errsave = errno;

        /* try converting to UTF-8, this can solve issues such as:
         * 1) legacy filename used on UTF-8 system
         * 2) UTF-8 filesystem mounted on legacy system */
        if (errsave == ENOENT)
        {
            StringBuf path2 = uri_to_filename (uri, false);
            if (path2 && strcmp (path, path2))
                stream = ::g_fopen (path2, mode2);
        }

        if (! stream)
        {
            perror (path);
            error = String (strerror (errsave));
            return nullptr;
        }
    }

    return new LocalFile (path, stream);
}

VFSImpl * StdinTransport::fopen (const char * uri, const char * mode, String & error)
{
    if (mode[0] != 'r' || strchr (mode, '+'))
    {
        error = String (_("Invalid access mode"));
        return nullptr;
    }

    return new LocalFile ("(stdin)", stdin);
}

VFSImpl * vfs_tmpfile (String & error)
{
    FILE * stream = tmpfile ();

    if (! stream)
    {
        int errsave = errno;
        perror ("(tmpfile)");
        error = String (strerror (errsave));
        return nullptr;
    }

    return new LocalFile ("(tmpfile)", stream);
}

LocalFile::~LocalFile ()
{
    // do not close stdin
    if (m_stream != stdin && fclose (m_stream) < 0)
        perror (m_path);
}

int64_t LocalFile::fread (void * ptr, int64_t size, int64_t nitems)
{
    if (m_last_op == OP_WRITE)
    {
        if (::fflush (m_stream) < 0)
        {
            perror (m_path);
            return 0;
        }
    }

    m_last_op = OP_READ;

    clearerr (m_stream);

    int64_t result = ::fread (ptr, size, nitems, m_stream);
    if (result < nitems && ferror (m_stream))
        perror (m_path);

    if (m_cached_pos >= 0)
        m_cached_pos += size * result;

    return result;
}

int64_t LocalFile::fwrite (const void * ptr, int64_t size, int64_t nitems)
{
    if (m_last_op == OP_READ)
    {
        if (::fflush (m_stream) < 0)
        {
            perror (m_path);
            return 0;
        }
    }

    m_last_op = OP_WRITE;

    clearerr (m_stream);

    int64_t result = ::fwrite (ptr, size, nitems, m_stream);
    if (result < nitems && ferror (m_stream))
        perror (m_path);

    if (m_cached_pos >= 0)
        m_cached_pos += size * result;

    if (m_cached_size >= 0 && m_cached_pos >= 0)
        m_cached_size = aud::max (m_cached_size, m_cached_pos);
    else
        m_cached_size = -1;

    return result;
}

int LocalFile::fseek (int64_t offset, VFSSeekType whence)
{
    int result = fseeko (m_stream, offset, from_vfs_seek_type (whence));
    if (result < 0)
        perror (m_path);

    if (result == 0)
    {
        m_last_op = OP_NONE;

        if (whence == VFS_SEEK_SET)
            m_cached_pos = offset;
        else if (whence == VFS_SEEK_CUR && m_cached_pos >= 0)
            m_cached_pos += offset;
        else
            m_cached_pos = -1;
    }

    return result;
}

int64_t LocalFile::ftell ()
{
    if (m_cached_pos < 0)
        m_cached_pos = ftello (m_stream);

    return m_cached_pos;
}

bool LocalFile::feof ()
{
    return ::feof (m_stream);
}

int LocalFile::ftruncate (int64_t length)
{
    if (m_last_op != OP_NONE)
    {
        if (::fflush (m_stream) < 0)
        {
            perror (m_path);
            return -1;
        }
    }

    int result = ::ftruncate (fileno (m_stream), length);
    if (result < 0)
        perror (m_path);

    if (result == 0)
    {
        m_last_op = OP_NONE;
        m_cached_size = length;
    }

    return result;
}

int LocalFile::fflush ()
{
    if (m_last_op != OP_WRITE)
        return 0;

    int result = ::fflush (m_stream);
    if (result < 0)
        perror (m_path);

    if (result == 0)
        m_last_op = OP_NONE;

    return result;
}

int64_t LocalFile::fsize ()
{
    // size of stdin is unknown
    if (m_stream == stdin)
        return -1;

    if (m_cached_size < 0)
    {
        int64_t saved_pos = ftell ();
        if (saved_pos < 0)
            goto ERR;

        if (fseek (0, VFS_SEEK_END) < 0)
            goto ERR;

        m_last_op = OP_NONE;
        m_cached_pos = -1;

        int64_t length = ftello (m_stream);
        if (length < 0)
            goto ERR;

        if (fseek (saved_pos, VFS_SEEK_SET) < 0)
            goto ERR;

        m_cached_pos = saved_pos;
        m_cached_size = length;
    }

    return m_cached_size;

ERR:
    perror (m_path);
    return -1;
}

VFSFileTest LocalTransport::test_file (const char * uri, VFSFileTest test, String & error)
{
    StringBuf path = uri_to_filename (uri);
    if (! path)
    {
        error = String (_("Invalid file name"));
        return VFSFileTest (test & VFS_NO_ACCESS);
    }

    int passed = 0;
    bool need_stat = true;
    GStatBuf st;

#ifdef S_ISLNK
    if (test & VFS_IS_SYMLINK)
    {
        if (g_lstat (path, & st) < 0)
        {
            error = String (strerror (errno));
            passed |= VFS_NO_ACCESS;
            goto out;
        }

        if (S_ISLNK (st.st_mode))
            passed |= VFS_IS_SYMLINK;
        else
            need_stat = false;
    }
#endif

    if (test & (VFS_IS_REGULAR | VFS_IS_DIR | VFS_IS_EXECUTABLE | VFS_EXISTS | VFS_NO_ACCESS))
    {
        if (need_stat && g_stat (path, & st) < 0)
        {
            error = String (strerror (errno));
            passed |= VFS_NO_ACCESS;
            goto out;
        }

        if (S_ISREG (st.st_mode))
            passed |= VFS_IS_REGULAR;
        if (S_ISDIR (st.st_mode))
            passed |= VFS_IS_DIR;
        if (st.st_mode & S_IXUSR)
            passed |= VFS_IS_EXECUTABLE;

        passed |= VFS_EXISTS;
    }

out:
    return VFSFileTest (test & passed);
}

Index<String> LocalTransport::read_folder (const char * uri, String & error)
{
    Index<String> entries;

    StringBuf path = uri_to_filename (uri);
    if (! path)
    {
        error = String (_("Invalid file name"));
        return entries;
    }

    GError * gerr = nullptr;
    GDir * folder = g_dir_open (path, 0, & gerr);
    if (! folder)
    {
        error = String (gerr->message);
        g_error_free (gerr);
        return entries;
    }

    const char * name;
    while ((name = g_dir_read_name (folder)))
        entries.append (String (filename_to_uri (filename_build ({path, name}))));

    g_dir_close (folder);

    return entries;
}