libsaria: Remove files that no longer exist
I scan a library path, do an lstat() and remove the files that can't be found. Signed-off-by: Bryan Schumaker <bjschuma@gmail.com>
This commit is contained in:
parent
a1819dd5f9
commit
659a2208b8
|
@ -12,6 +12,7 @@ enum DataState {
|
|||
|
||||
namespace libsaria
|
||||
{
|
||||
bool exists(string);
|
||||
void list_dir(string &, list<string> &);
|
||||
|
||||
namespace app
|
||||
|
|
|
@ -42,6 +42,12 @@ static void handle_entry(string &dir, list<string> &file_list, struct dirent *di
|
|||
namespace libsaria
|
||||
{
|
||||
|
||||
bool exists(string filepath)
|
||||
{
|
||||
struct stat stat;
|
||||
return lstat(filepath.c_str(), &stat) == 0;
|
||||
}
|
||||
|
||||
void list_dir(string &dir, list<string> &file_list)
|
||||
{
|
||||
DIR *dp;
|
||||
|
|
|
@ -2,11 +2,24 @@
|
|||
#include <libsaria/library.h>
|
||||
#include <libsaria/track.h>
|
||||
#include <libsaria/print.h>
|
||||
#include <libsaria/deck.h>
|
||||
#include <libsaria/idle.h>
|
||||
#include <libsaria/fs.h>
|
||||
#include "library.h"
|
||||
|
||||
static unsigned int MAX_SCAN = 20;
|
||||
|
||||
class ValidateTask : public IdleTask
|
||||
{
|
||||
private:
|
||||
struct libsaria::library::Path *path;
|
||||
|
||||
public:
|
||||
ValidateTask(struct libsaria::library::Path *);
|
||||
~ValidateTask();
|
||||
void run_task();
|
||||
};
|
||||
|
||||
class ScanTask : public IdleTask
|
||||
{
|
||||
private:
|
||||
|
@ -30,6 +43,36 @@ class ReaddirTask : public IdleTask
|
|||
void run_task();
|
||||
};
|
||||
|
||||
ValidateTask::ValidateTask(struct libsaria::library::Path *p)
|
||||
{
|
||||
path = p;
|
||||
}
|
||||
|
||||
ValidateTask::~ValidateTask()
|
||||
{
|
||||
}
|
||||
|
||||
void ValidateTask::run_task()
|
||||
{
|
||||
list<libsaria::Track> to_rm;
|
||||
list<libsaria::Track>::iterator it, it_mv;
|
||||
|
||||
for (it = path->tracks.begin(); it != path->tracks.end(); it++) {
|
||||
if (!libsaria::exists(it->get_filepath())) {
|
||||
println("Reaping: " + it->get_filepath());
|
||||
/* Iterators are no longer valid after splice */
|
||||
it_mv = it;
|
||||
it--;
|
||||
to_rm.splice(to_rm.begin(), path->tracks, it_mv);
|
||||
path->data_state = DIRTY;
|
||||
}
|
||||
};
|
||||
|
||||
libsaria::deck::tracks_removed(to_rm);
|
||||
notify_path_updated(path);
|
||||
save_path(path);
|
||||
}
|
||||
|
||||
ScanTask::ScanTask(struct libsaria::library::Path *p, list<string> &files)
|
||||
{
|
||||
list<string>::iterator it;
|
||||
|
@ -114,6 +157,14 @@ void ReaddirTask::run_task()
|
|||
void do_update_path(struct libsaria::library::Path *path)
|
||||
{
|
||||
println("Updating path: " + path->path);
|
||||
|
||||
if (!libsaria::exists(path->path)) {
|
||||
libsaria::library::delete_path(path);
|
||||
return;
|
||||
}
|
||||
|
||||
ValidateTask *validate = new ValidateTask(path);
|
||||
ReaddirTask *task = new ReaddirTask(path);
|
||||
validate->queue();
|
||||
task->queue();
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue