~ubuntu-dev/ubuntu/lucid/mpd/lucid-201002101854

« back to all changes in this revision

Viewing changes to src/input_file.c

  • Committer: Bazaar Package Importer
  • Author(s): Alessio Treglia
  • Date: 2009-10-23 12:38:20 UTC
  • mfrom: (1.1.12 upstream) (2.2.6 sid)
  • Revision ID: james.westby@ubuntu.com-20091023123820-rxt21lmekscxbkbt
Tags: 0.15.4-1ubuntu1
* Merge from debian unstable, Ubuntu remaining changes:
  - debian/control:
    + Don't build-depends on libmikmod2-dev (Debian bug #510675).
    + Move avahi-daemon from Suggests field to Recommends field.
  - debian/mpd.init.d:
    + Read mpd user from mpd.conf.
* Also fixes LP: #332332.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* the Music Player Daemon (MPD)
2
 
 * Copyright (C) 2003-2007 by Warren Dukes (warren.dukes@gmail.com)
3
 
 * This project's homepage is: http://www.musicpd.org
4
 
 *
5
 
 * This program is free software; you can redistribute it and/or modify
6
 
 * it under the terms of the GNU General Public License as published by
7
 
 * the Free Software Foundation; either version 2 of the License, or
8
 
 * (at your option) any later version.
9
 
 *
10
 
 * This program is distributed in the hope that it will be useful,
11
 
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 
 * GNU General Public License for more details.
14
 
 * You should have received a copy of the GNU General Public License
15
 
 * along with this program; if not, write to the Free Software
16
 
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
17
 
 */
18
 
 
19
 
#include "input_file.h"
20
 
 
21
 
#include <sys/stat.h>
22
 
#include <fcntl.h>
23
 
#include <unistd.h>
24
 
#include <errno.h>
25
 
#include <string.h>
26
 
#include <glib.h>
27
 
 
28
 
static bool
29
 
input_file_open(struct input_stream *is, const char *filename)
30
 
{
31
 
        int fd, ret;
32
 
        struct stat st;
33
 
 
34
 
        if (filename[0] != '/')
35
 
                return false;
36
 
 
37
 
        fd = open(filename, O_RDONLY);
38
 
        if (fd < 0) {
39
 
                is->error = errno;
40
 
                return false;
41
 
        }
42
 
 
43
 
        is->seekable = true;
44
 
 
45
 
        ret = fstat(fd, &st);
46
 
        if (ret < 0) {
47
 
                is->error = errno;
48
 
                close(fd);
49
 
                return false;
50
 
        }
51
 
 
52
 
        if (!S_ISREG(st.st_mode)) {
53
 
                g_debug("Not a regular file: %s\n", filename);
54
 
                is->error = EINVAL;
55
 
                close(fd);
56
 
                return false;
57
 
        }
58
 
 
59
 
        is->size = st.st_size;
60
 
 
61
 
#ifdef POSIX_FADV_SEQUENTIAL
62
 
        posix_fadvise(fd, (off_t)0, is->size, POSIX_FADV_SEQUENTIAL);
63
 
#endif
64
 
 
65
 
        is->data = GINT_TO_POINTER(fd);
66
 
        is->ready = true;
67
 
 
68
 
        return true;
69
 
}
70
 
 
71
 
static bool
72
 
input_file_seek(struct input_stream *is, off_t offset, int whence)
73
 
{
74
 
        int fd = GPOINTER_TO_INT(is->data);
75
 
 
76
 
        offset = lseek(fd, offset, whence);
77
 
        if (offset < 0) {
78
 
                is->error = errno;
79
 
                return false;
80
 
        }
81
 
 
82
 
        is->offset = offset;
83
 
        return true;
84
 
}
85
 
 
86
 
static size_t
87
 
input_file_read(struct input_stream *is, void *ptr, size_t size)
88
 
{
89
 
        int fd = GPOINTER_TO_INT(is->data);
90
 
        ssize_t nbytes;
91
 
 
92
 
        nbytes = read(fd, ptr, size);
93
 
        if (nbytes < 0) {
94
 
                is->error = errno;
95
 
                g_debug("input_file_read: error reading: %s\n",
96
 
                        strerror(is->error));
97
 
                return 0;
98
 
        }
99
 
 
100
 
        is->offset += nbytes;
101
 
        return (size_t)nbytes;
102
 
}
103
 
 
104
 
static void
105
 
input_file_close(struct input_stream *is)
106
 
{
107
 
        int fd = GPOINTER_TO_INT(is->data);
108
 
 
109
 
        close(fd);
110
 
}
111
 
 
112
 
static bool
113
 
input_file_eof(struct input_stream *is)
114
 
{
115
 
        return is->offset >= is->size;
116
 
}
117
 
 
118
 
static int
119
 
input_file_buffer(G_GNUC_UNUSED struct input_stream *is)
120
 
{
121
 
        return 0;
122
 
}
123
 
 
124
 
const struct input_plugin input_plugin_file = {
125
 
        .open = input_file_open,
126
 
        .close = input_file_close,
127
 
        .buffer = input_file_buffer,
128
 
        .read = input_file_read,
129
 
        .eof = input_file_eof,
130
 
        .seek = input_file_seek,
131
 
};