libsaria: Check if a song exists before loading it

This could lead to a segmentation fault if we don't do it...

Signed-off-by: Bryan Schumaker <bjschuma@gmail.com>
This commit is contained in:
Bryan Schumaker 2012-02-07 22:24:37 -05:00
parent 51e5887971
commit fe7bf9e445
6 changed files with 82 additions and 2 deletions

View File

@ -3,7 +3,7 @@ import sys
MAJOR = 5
MINOR = 5
BUG = 1
BUG = 2
EXTRA = ""
DEBUG = False

View File

@ -17,6 +17,7 @@ struct file
string get_saria_dir();
void make_saria_dir();
bool exists(string);
sid_t lookup_songid(string &);
#endif /* LIBSARIA_PATH_H */

View File

@ -0,0 +1,22 @@
#ifndef LIBSARIA_PATH_H
#define LIBSARIA_PATH_H
#include <libsaria/track.h>
#include <string>
#include <list>
using namespace std;
#include <sys/types.h>
struct file
{
string name;
sid_t d_sid;
};
string get_saria_dir();
void make_saria_dir();
sid_t lookup_songid(string &);
#endif /* LIBSARIA_PATH_H */

View File

@ -2,6 +2,7 @@
#include <libsaria/audio.h>
#include <libsaria/callback.h>
#include <libsaria/print.h>
#include <libsaria/path.h>
#include "audio.h"
static string cur_file;
@ -30,7 +31,7 @@ static void reset()
void load_file(GstElement *playbin, string file)
{
if (file == "")
if (file == "" || !exists(file))
return;
string uri = "file://" + file;
println("Loading uri: " + uri);

View File

@ -40,6 +40,12 @@ void make_saria_dir()
mkdir(saria.c_str(), mode);
}
bool exists(string path)
{
struct stat stat;
return lstat(path.c_str(), &stat) == 0;
}
sid_t lookup_songid(string &filepath)
{
struct stat stat;

View File

@ -0,0 +1,50 @@
#include <dirent.h>
#include <sys/stat.h>
#include <stdlib.h>
#include <libsaria/path.h>
#include <iostream>
using namespace std;
/* I want to use the d_type field in a dirent */
#ifndef __USE_BSD
#define __USE_BSD
#endif
#ifdef DEBUG
static string SARIA_DIR = "/saria-debug";
#else /* DEBUG */
static string SARIA_DIR = "/saria";
#endif /* DEBUG */
string get_saria_dir()
{
string saria = getenv("HOME");
string xdg = getenv("XDG_CONFIG_HOME");
if (xdg == "")
saria += "/.config";
else if (xdg.compare(0, saria.size(), saria) == 0)
saria = xdg + "/";
else
saria += "/" + xdg += "/";
return saria += SARIA_DIR;
}
void make_saria_dir()
{
string saria = get_saria_dir();
mode_t mode = S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH;
mkdir(saria.c_str(), mode);
}
sid_t lookup_songid(string &filepath)
{
struct stat stat;
int err = lstat(filepath.c_str(), &stat);
if (err != 0)
return err;
return stat.st_ino;
}