mpd/src/ArchiveLookup.cxx

99 lines
2.3 KiB
C++
Raw Normal View History

/*
2013-01-24 19:18:58 +01:00
* Copyright (C) 2003-2013 The Music Player Daemon Project
* http://www.musicpd.org
2008-12-16 21:42:34 +01:00
*
* 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.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
2008-12-16 21:42:34 +01:00
*/
#include "config.h" /* must be first for large file support */
2013-01-24 19:18:58 +01:00
#include "ArchiveLookup.hxx"
#include "ArchiveDomain.hxx"
#include "Log.hxx"
#include <glib.h>
2008-12-16 21:42:34 +01:00
#include <string.h>
#include <sys/stat.h>
#include <unistd.h>
#include <errno.h>
2013-10-17 00:35:58 +02:00
bool
archive_lookup(char *pathname, const char **archive,
const char **inpath, const char **suffix)
2008-12-16 21:42:34 +01:00
{
char *pathdupe;
int len, idx;
struct stat st_info;
bool ret = false;
*archive = NULL;
*inpath = NULL;
*suffix = NULL;
pathdupe = g_strdup(pathname);
len = idx = strlen(pathname);
while (idx > 0) {
//try to stat if its real directory
if (stat(pathdupe, &st_info) == -1) {
if (errno != ENOTDIR) {
FormatErrno(archive_domain,
"Failed to stat %s", pathdupe);
2008-12-16 21:42:34 +01:00
break;
}
} else {
//is something found ins original path (is not an archive)
if (idx == len) {
break;
}
//its a file ?
if (S_ISREG(st_info.st_mode)) {
//so the upper should be file
pathname[idx] = 0;
ret = true;
*archive = pathname;
*inpath = pathname + idx+1;
//try to get suffix
*suffix = NULL;
while (idx > 0) {
if (pathname[idx] == '.') {
*suffix = pathname + idx + 1;
break;
}
idx--;
}
break;
} else {
FormatError(archive_domain,
"Not a regular file: %s",
pathdupe);
2008-12-16 21:42:34 +01:00
break;
}
}
//find one dir up
while (idx > 0) {
if (pathdupe[idx] == '/') {
pathdupe[idx] = 0;
break;
}
idx--;
}
}
g_free(pathdupe);
return ret;
}