~ubuntu-branches/ubuntu/trusty/mpd/trusty

« back to all changes in this revision

Viewing changes to src/mapper.c

  • Committer: Bazaar Package Importer
  • Author(s): Angel Abad
  • Date: 2011-02-02 12:26:30 UTC
  • mfrom: (1.5.11 upstream)
  • Revision ID: james.westby@ubuntu.com-20110202122630-bdyx8w4k94doz4fs
Tags: 0.16.1-1ubuntu1
* Merge from debian unstable. Remaining changes:
  - debian/control:
    + Don't build-depend 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.
  - debian/control, debian/rules:
    + Add libmp3lame-dev to the build dependencies and enable lame.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/*
2
 
 * Copyright (C) 2003-2009 The Music Player Daemon Project
 
2
 * Copyright (C) 2003-2010 The Music Player Daemon Project
3
3
 * http://www.musicpd.org
4
4
 *
5
5
 * This program is free software; you can redistribute it and/or modify
21
21
 * Maps directory and song objects to file system paths.
22
22
 */
23
23
 
 
24
#include "config.h"
24
25
#include "mapper.h"
25
26
#include "directory.h"
26
27
#include "song.h"
27
28
#include "path.h"
28
 
#include "conf.h"
29
29
 
30
30
#include <glib.h>
31
31
 
32
32
#include <assert.h>
33
 
#include <sys/types.h>
34
 
#include <sys/stat.h>
35
 
#include <unistd.h>
36
33
#include <string.h>
37
 
#include <errno.h>
38
34
 
39
35
static char *music_dir;
40
36
static size_t music_dir_length;
58
54
static void
59
55
mapper_set_music_dir(const char *path)
60
56
{
61
 
        int ret;
62
 
        struct stat st;
63
 
 
64
57
        music_dir = strdup_chop_slash(path);
65
58
        music_dir_length = strlen(music_dir);
66
59
 
67
 
        ret = stat(music_dir, &st);
68
 
        if (ret < 0)
69
 
                g_warning("failed to stat music directory \"%s\": %s",
70
 
                          music_dir, g_strerror(errno));
71
 
        else if (!S_ISDIR(st.st_mode))
 
60
        if (!g_file_test(music_dir, G_FILE_TEST_IS_DIR))
72
61
                g_warning("music directory is not a directory: \"%s\"",
73
62
                          music_dir);
74
63
}
76
65
static void
77
66
mapper_set_playlist_dir(const char *path)
78
67
{
79
 
        int ret;
80
 
        struct stat st;
81
 
 
82
68
        playlist_dir = g_strdup(path);
83
69
 
84
 
        ret = stat(playlist_dir, &st);
85
 
        if (ret < 0)
86
 
                g_warning("failed to stat playlist directory \"%s\": %s",
87
 
                          playlist_dir, g_strerror(errno));
88
 
        else if (!S_ISDIR(st.st_mode))
 
70
        if (!g_file_test(playlist_dir, G_FILE_TEST_IS_DIR))
89
71
                g_warning("playlist directory is not a directory: \"%s\"",
90
72
                          playlist_dir);
91
73
}
92
74
 
93
 
void mapper_init(void)
 
75
void mapper_init(const char *_music_dir, const char *_playlist_dir)
94
76
{
95
 
        const char *path;
96
 
 
97
 
        path = config_get_path(CONF_MUSIC_DIR);
98
 
        if (path != NULL)
99
 
                mapper_set_music_dir(path);
100
 
#if GLIB_CHECK_VERSION(2,14,0)
101
 
        else {
102
 
                path = g_get_user_special_dir(G_USER_DIRECTORY_MUSIC);
103
 
                if (path != NULL)
104
 
                        mapper_set_music_dir(path);
105
 
        }
106
 
#endif
107
 
 
108
 
        path = config_get_path(CONF_PLAYLIST_DIR);
109
 
        if (path != NULL)
110
 
                mapper_set_playlist_dir(path);
 
77
        if (_music_dir != NULL)
 
78
                mapper_set_music_dir(_music_dir);
 
79
 
 
80
        if (_playlist_dir != NULL)
 
81
                mapper_set_playlist_dir(_playlist_dir);
111
82
}
112
83
 
113
84
void mapper_finish(void)
122
93
        return music_dir != NULL;
123
94
}
124
95
 
 
96
const char *
 
97
map_to_relative_path(const char *path_utf8)
 
98
{
 
99
        return music_dir != NULL &&
 
100
                memcmp(path_utf8, music_dir, music_dir_length) == 0 &&
 
101
                G_IS_DIR_SEPARATOR(path_utf8[music_dir_length])
 
102
                ? path_utf8 + music_dir_length + 1
 
103
                : path_utf8;
 
104
}
 
105
 
125
106
char *
126
107
map_uri_fs(const char *uri)
127
108
{
189
170
        assert(song_is_file(song));
190
171
 
191
172
        if (song_in_database(song))
192
 
                return map_directory_child_fs(song->parent, song->url);
 
173
                return map_directory_child_fs(song->parent, song->uri);
193
174
        else
194
 
                return utf8_to_fs_charset(song->url);
 
175
                return utf8_to_fs_charset(song->uri);
195
176
}
196
177
 
197
178
char *
199
180
{
200
181
        if (music_dir != NULL &&
201
182
            strncmp(path_fs, music_dir, music_dir_length) == 0 &&
202
 
            path_fs[music_dir_length] == '/')
 
183
            G_IS_DIR_SEPARATOR(path_fs[music_dir_length]))
203
184
                /* remove musicDir prefix */
204
185
                path_fs += music_dir_length + 1;
205
 
        else if (path_fs[0] == '/')
 
186
        else if (G_IS_DIR_SEPARATOR(path_fs[0]))
206
187
                /* not within musicDir */
207
188
                return NULL;
208
189