88 lines
1.5 KiB
C++
88 lines
1.5 KiB
C++
|
|
#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
|
|
|
|
static void sort_entry(struct dirent *dirp, list<file> &files, list<file> &dirs)
|
|
{
|
|
struct file entry;
|
|
|
|
if (dirp == NULL)
|
|
return;
|
|
|
|
entry.name = dirp->d_name;
|
|
entry.d_ino = dirp->d_ino;
|
|
if (entry.name == "." || entry.name == "..")
|
|
return;
|
|
|
|
switch(dirp->d_type) {
|
|
case DT_DIR:
|
|
dirs.push_back(entry);
|
|
break;
|
|
case DT_REG:
|
|
files.push_back(entry);
|
|
default:
|
|
break;
|
|
}
|
|
}
|
|
|
|
void readdir(string dir, list<file> &files, list<file> &dirs)
|
|
{
|
|
DIR *dp;
|
|
struct dirent *dirp;
|
|
|
|
dp = opendir(dir.c_str());
|
|
if (dp == NULL)
|
|
return;
|
|
|
|
do {
|
|
dirp = readdir(dp);
|
|
sort_entry(dirp, files, dirs);
|
|
} while (dirp != NULL);
|
|
|
|
closedir(dp);
|
|
}
|
|
|
|
#ifdef DEBUG
|
|
static string SARIA_DIR = "/saria-debug";
|
|
#else /* DEBUG */
|
|
static string SARIA_DIR = "/saria";
|
|
#endif /* DEBUG */
|
|
|
|
string get_saria_dir()
|
|
{
|
|
string saria = ".config";
|
|
char *xdg = getenv("XDG_CONFIG_HOME");
|
|
if (xdg != NULL)
|
|
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);
|
|
}
|
|
|
|
bool get_inode(string filepath, ino_t &ino)
|
|
{
|
|
int err;
|
|
struct stat stat;
|
|
|
|
err = lstat(filepath.c_str(), &stat);
|
|
if (err != 0)
|
|
return false;
|
|
ino = stat.st_ino;
|
|
return true;
|
|
}
|