directory: converted typedef Directory to struct directory

The struct can be forward-declared by other headers, which relaxes the
header dependencies.
This commit is contained in:
Max Kellermann 2008-10-08 10:49:05 +02:00
parent 3c1142cbcb
commit 25f67da58c
11 changed files with 102 additions and 87 deletions

View File

@ -46,8 +46,8 @@ typedef struct _SearchStats {
unsigned long playTime; unsigned long playTime;
} SearchStats; } SearchStats;
static int countSongsInDirectory(Directory * directory, static int
void *data) countSongsInDirectory(struct directory *directory, void *data)
{ {
int *count = (int *)data; int *count = (int *)data;
@ -56,7 +56,8 @@ static int countSongsInDirectory(Directory * directory,
return 0; return 0;
} }
static int printDirectoryInDirectory(Directory * directory, void *data) static int
printDirectoryInDirectory(struct directory *directory, void *data)
{ {
struct client *client = data; struct client *client = data;
if (directory->path) { if (directory->path) {
@ -356,15 +357,16 @@ int listAllUniqueTags(struct client *client, int type, int numConditionals,
return ret; return ret;
} }
static int sumSavedFilenameMemoryInDirectory(Directory * dir, void *data) static int
sumSavedFilenameMemoryInDirectory(struct directory *dir, void *data)
{ {
int *sum = data; int *sum = data;
if (!dir->path) if (!dir->path)
return 0; return 0;
*sum += (strlen(getDirectoryPath(dir)) + 1 - sizeof(Directory *)) * *sum += (strlen(getDirectoryPath(dir)) + 1
dir->songs.nr; - sizeof(struct directory *)) * dir->songs.nr;
return 0; return 0;
} }

View File

@ -40,11 +40,11 @@
#define DIRECTORY_MPD_VERSION "mpd_version: " #define DIRECTORY_MPD_VERSION "mpd_version: "
#define DIRECTORY_FS_CHARSET "fs_charset: " #define DIRECTORY_FS_CHARSET "fs_charset: "
static Directory *music_root; static struct directory *music_root;
static time_t directory_dbModTime; static time_t directory_dbModTime;
static void deleteEmptyDirectoriesInDirectory(Directory * directory); static void deleteEmptyDirectoriesInDirectory(struct directory * directory);
static char *getDbFile(void) static char *getDbFile(void)
{ {
@ -56,12 +56,12 @@ static char *getDbFile(void)
return param->value; return param->value;
} }
Directory * struct directory *
newDirectory(const char *dirname, Directory * parent) newDirectory(const char *dirname, struct directory * parent)
{ {
Directory *directory; struct directory *directory;
directory = xcalloc(1, sizeof(Directory)); directory = xcalloc(1, sizeof(*directory));
if (dirname && strlen(dirname)) if (dirname && strlen(dirname))
directory->path = xstrdup(dirname); directory->path = xstrdup(dirname);
@ -71,7 +71,7 @@ newDirectory(const char *dirname, Directory * parent)
} }
void void
freeDirectory(Directory * directory) freeDirectory(struct directory * directory)
{ {
dirvec_destroy(&directory->children); dirvec_destroy(&directory->children);
songvec_destroy(&directory->songs); songvec_destroy(&directory->songs);
@ -82,7 +82,7 @@ freeDirectory(Directory * directory)
/*getDirectoryPath(NULL); */ /*getDirectoryPath(NULL); */
} }
static void deleteEmptyDirectoriesInDirectory(Directory * directory) static void deleteEmptyDirectoriesInDirectory(struct directory * directory)
{ {
int i; int i;
struct dirvec *dv = &directory->children; struct dirvec *dv = &directory->children;
@ -106,7 +106,7 @@ int isRootDirectory(const char *name)
return (!name || name[0] == '\0' || !strcmp(name, "/")); return (!name || name[0] == '\0' || !strcmp(name, "/"));
} }
Directory * struct directory *
directory_get_root(void) directory_get_root(void)
{ {
assert(music_root != NULL); assert(music_root != NULL);
@ -114,10 +114,11 @@ directory_get_root(void)
return music_root; return music_root;
} }
static Directory *getSubDirectory(Directory * directory, const char *name) static struct directory *
getSubDirectory(struct directory * directory, const char *name)
{ {
Directory *cur = directory; struct directory *cur = directory;
Directory *found = NULL; struct directory *found = NULL;
char *duplicated; char *duplicated;
char *locate; char *locate;
@ -144,7 +145,7 @@ static Directory *getSubDirectory(Directory * directory, const char *name)
return found; return found;
} }
Directory * struct directory *
getDirectory(const char *name) getDirectory(const char *name)
{ {
return getSubDirectory(music_root, name); return getSubDirectory(music_root, name);
@ -164,7 +165,7 @@ static int printDirectoryList(struct client *client, struct dirvec *dv)
int printDirectoryInfo(struct client *client, const char *name) int printDirectoryInfo(struct client *client, const char *name)
{ {
Directory *directory; struct directory *directory;
if ((directory = getDirectory(name)) == NULL) if ((directory = getDirectory(name)) == NULL)
return -1; return -1;
@ -176,7 +177,8 @@ int printDirectoryInfo(struct client *client, const char *name)
} }
/* TODO error checking */ /* TODO error checking */
static int writeDirectoryInfo(FILE * fp, Directory * directory) static int
writeDirectoryInfo(FILE * fp, struct directory * directory)
{ {
struct dirvec *children = &directory->children; struct dirvec *children = &directory->children;
size_t i; size_t i;
@ -190,7 +192,7 @@ static int writeDirectoryInfo(FILE * fp, Directory * directory)
} }
for (i = 0; i < children->nr; ++i) { for (i = 0; i < children->nr; ++i) {
Directory *cur = children->base[i]; struct directory *cur = children->base[i];
const char *base = mpd_basename(cur->path); const char *base = mpd_basename(cur->path);
retv = fprintf(fp, DIRECTORY_DIR "%s\n", base); retv = fprintf(fp, DIRECTORY_DIR "%s\n", base);
@ -209,7 +211,8 @@ static int writeDirectoryInfo(FILE * fp, Directory * directory)
return 0; return 0;
} }
static void readDirectoryInfo(FILE * fp, Directory * directory) static void
readDirectoryInfo(FILE * fp, struct directory * directory)
{ {
char buffer[MPD_PATH_MAX * 2]; char buffer[MPD_PATH_MAX * 2];
int bufferSize = MPD_PATH_MAX * 2; int bufferSize = MPD_PATH_MAX * 2;
@ -219,7 +222,7 @@ static void readDirectoryInfo(FILE * fp, Directory * directory)
while (myFgets(buffer, bufferSize, fp) while (myFgets(buffer, bufferSize, fp)
&& prefixcmp(buffer, DIRECTORY_END)) { && prefixcmp(buffer, DIRECTORY_END)) {
if (!prefixcmp(buffer, DIRECTORY_DIR)) { if (!prefixcmp(buffer, DIRECTORY_DIR)) {
Directory *subdir; struct directory *subdir;
strcpy(key, &(buffer[strlen(DIRECTORY_DIR)])); strcpy(key, &(buffer[strlen(DIRECTORY_DIR)]));
if (!myFgets(buffer, bufferSize, fp)) if (!myFgets(buffer, bufferSize, fp))
@ -248,7 +251,7 @@ static void readDirectoryInfo(FILE * fp, Directory * directory)
} }
void void
sortDirectory(Directory * directory) sortDirectory(struct directory * directory)
{ {
int i; int i;
struct dirvec *dv = &directory->children; struct dirvec *dv = &directory->children;
@ -443,10 +446,11 @@ int readDirectoryDB(void)
return 0; return 0;
} }
static int traverseAllInSubDirectory(Directory * directory, static int
int (*forEachSong) (Song *, void *), traverseAllInSubDirectory(struct directory * directory,
int (*forEachDir) (Directory *, void *), int (*forEachSong) (Song *, void *),
void *data) int (*forEachDir) (struct directory *, void *),
void *data)
{ {
struct dirvec *dv = &directory->children; struct dirvec *dv = &directory->children;
int err = 0; int err = 0;
@ -468,11 +472,12 @@ static int traverseAllInSubDirectory(Directory * directory,
return err; return err;
} }
int traverseAllIn(const char *name, int
int (*forEachSong) (Song *, void *), traverseAllIn(const char *name,
int (*forEachDir) (Directory *, void *), void *data) int (*forEachSong) (Song *, void *),
int (*forEachDir) (struct directory *, void *), void *data)
{ {
Directory *directory; struct directory *directory;
if ((directory = getDirectory(name)) == NULL) { if ((directory = getDirectory(name)) == NULL) {
Song *song; Song *song;
@ -497,7 +502,7 @@ void directory_init(void)
Song *getSongFromDB(const char *file) Song *getSongFromDB(const char *file)
{ {
Song *song = NULL; Song *song = NULL;
Directory *directory; struct directory *directory;
char *dir = NULL; char *dir = NULL;
char *duplicated = xstrdup(file); char *duplicated = xstrdup(file);
char *shortname = strrchr(duplicated, '/'); char *shortname = strrchr(duplicated, '/');

View File

@ -26,19 +26,19 @@
#include <stdbool.h> #include <stdbool.h>
struct dirvec { struct dirvec {
struct _Directory **base; struct directory **base;
size_t nr; size_t nr;
}; };
typedef struct _Directory { struct directory {
char *path; char *path;
struct dirvec children; struct dirvec children;
struct songvec songs; struct songvec songs;
struct _Directory *parent; struct directory *parent;
ino_t inode; ino_t inode;
dev_t device; dev_t device;
unsigned stat; /* not needed if ino_t == dev_t == 0 is impossible */ unsigned stat; /* not needed if ino_t == dev_t == 0 is impossible */
} Directory; };
void directory_init(void); void directory_init(void);
@ -46,26 +46,26 @@ void directory_finish(void);
int isRootDirectory(const char *name); int isRootDirectory(const char *name);
Directory * struct directory *
directory_get_root(void); directory_get_root(void);
Directory * struct directory *
newDirectory(const char *dirname, Directory * parent); newDirectory(const char *dirname, struct directory * parent);
void void
freeDirectory(Directory * directory); freeDirectory(struct directory * directory);
static inline bool static inline bool
directory_is_empty(Directory *directory) directory_is_empty(struct directory *directory)
{ {
return directory->children.nr == 0 && directory->songs.nr == 0; return directory->children.nr == 0 && directory->songs.nr == 0;
} }
Directory * struct directory *
getDirectory(const char *name); getDirectory(const char *name);
void void
sortDirectory(Directory * directory); sortDirectory(struct directory * directory);
int printDirectoryInfo(struct client *client, const char *dirname); int printDirectoryInfo(struct client *client, const char *dirname);
@ -81,7 +81,7 @@ time_t getDbModTime(void);
int traverseAllIn(const char *name, int traverseAllIn(const char *name,
int (*forEachSong) (Song *, void *), int (*forEachSong) (Song *, void *),
int (*forEachDir) (Directory *, void *), void *data); int (*forEachDir) (struct directory *, void *), void *data);
#define getDirectoryPath(dir) ((dir && dir->path) ? dir->path : "") #define getDirectoryPath(dir) ((dir && dir->path) ? dir->path : "")

View File

@ -1,26 +1,27 @@
#include "dirvec.h" #include "dirvec.h"
#include "directory.h"
#include "os_compat.h" #include "os_compat.h"
#include "utils.h" #include "utils.h"
static size_t dv_size(struct dirvec *dv) static size_t dv_size(struct dirvec *dv)
{ {
return dv->nr * sizeof(Directory *); return dv->nr * sizeof(struct directory *);
} }
/* Only used for sorting/searching a dirvec, not general purpose compares */ /* Only used for sorting/searching a dirvec, not general purpose compares */
static int dirvec_cmp(const void *d1, const void *d2) static int dirvec_cmp(const void *d1, const void *d2)
{ {
const Directory *a = ((const Directory * const *)d1)[0]; const struct directory *a = ((const struct directory * const *)d1)[0];
const Directory *b = ((const Directory * const *)d2)[0]; const struct directory *b = ((const struct directory * const *)d2)[0];
return strcmp(a->path, b->path); return strcmp(a->path, b->path);
} }
void dirvec_sort(struct dirvec *dv) void dirvec_sort(struct dirvec *dv)
{ {
qsort(dv->base, dv->nr, sizeof(Directory *), dirvec_cmp); qsort(dv->base, dv->nr, sizeof(struct directory *), dirvec_cmp);
} }
Directory *dirvec_find(struct dirvec *dv, const char *path) struct directory *dirvec_find(struct dirvec *dv, const char *path)
{ {
int i; int i;
@ -30,7 +31,7 @@ Directory *dirvec_find(struct dirvec *dv, const char *path)
return NULL; return NULL;
} }
int dirvec_delete(struct dirvec *dv, Directory *del) int dirvec_delete(struct dirvec *dv, struct directory *del)
{ {
int i; int i;
@ -43,7 +44,7 @@ int dirvec_delete(struct dirvec *dv, Directory *del)
dv->base = NULL; dv->base = NULL;
} else { } else {
memmove(&dv->base[i], &dv->base[i + 1], memmove(&dv->base[i], &dv->base[i + 1],
(dv->nr - i + 1) * sizeof(Directory *)); (dv->nr - i + 1) * sizeof(struct directory *));
dv->base = xrealloc(dv->base, dv_size(dv)); dv->base = xrealloc(dv->base, dv_size(dv));
} }
return i; return i;
@ -52,7 +53,7 @@ int dirvec_delete(struct dirvec *dv, Directory *del)
return -1; /* not found */ return -1; /* not found */
} }
void dirvec_add(struct dirvec *dv, Directory *add) void dirvec_add(struct dirvec *dv, struct directory *add)
{ {
++dv->nr; ++dv->nr;
dv->base = xrealloc(dv->base, dv_size(dv)); dv->base = xrealloc(dv->base, dv_size(dv));

View File

@ -1,15 +1,15 @@
#ifndef DIRVEC_H #ifndef DIRVEC_H
#define DIRVEC_H #define DIRVEC_H
#include "directory.h" struct dirvec;
void dirvec_sort(struct dirvec *dv); void dirvec_sort(struct dirvec *dv);
Directory *dirvec_find(struct dirvec *dv, const char *path); struct directory *dirvec_find(struct dirvec *dv, const char *path);
int dirvec_delete(struct dirvec *dv, Directory *del); int dirvec_delete(struct dirvec *dv, struct directory *del);
void dirvec_add(struct dirvec *dv, Directory *add); void dirvec_add(struct dirvec *dv, struct directory *add);
void dirvec_destroy(struct dirvec *dv); void dirvec_destroy(struct dirvec *dv);

View File

@ -29,7 +29,7 @@
#include "os_compat.h" #include "os_compat.h"
Song * Song *
song_alloc(const char *url, struct _Directory *parent) song_alloc(const char *url, struct directory *parent)
{ {
size_t urllen; size_t urllen;
Song *song; Song *song;
@ -46,7 +46,7 @@ song_alloc(const char *url, struct _Directory *parent)
return song; return song;
} }
Song *newSong(const char *url, Directory * parentDir) Song *newSong(const char *url, struct directory *parentDir)
{ {
Song *song; Song *song;
assert(*url); assert(*url);

View File

@ -32,15 +32,15 @@ struct client;
typedef struct _Song { typedef struct _Song {
struct tag *tag; struct tag *tag;
struct _Directory *parentDir; struct directory *parentDir;
time_t mtime; time_t mtime;
char url[sizeof(size_t)]; char url[sizeof(size_t)];
} Song; } Song;
Song * Song *
song_alloc(const char *url, struct _Directory *parent); song_alloc(const char *url, struct directory *parent);
Song *newSong(const char *url, struct _Directory *parentDir); Song *newSong(const char *url, struct directory *parentDir);
void freeJustSong(Song *); void freeJustSong(Song *);

View File

@ -98,7 +98,7 @@ static int matchesAnMpdTagItemKey(char *buffer, int *itemType)
} }
void readSongInfoIntoList(FILE *fp, struct songvec *sv, void readSongInfoIntoList(FILE *fp, struct songvec *sv,
Directory *parentDir) struct directory *parentDir)
{ {
char buffer[MPD_PATH_MAX + 1024]; char buffer[MPD_PATH_MAX + 1024];
int bufferSize = MPD_PATH_MAX + 1024; int bufferSize = MPD_PATH_MAX + 1024;

View File

@ -26,6 +26,6 @@ struct songvec;
void songvec_save(FILE *fp, struct songvec *sv); void songvec_save(FILE *fp, struct songvec *sv);
void readSongInfoIntoList(FILE * fp, struct songvec *sv, void readSongInfoIntoList(FILE * fp, struct songvec *sv,
struct _Directory *parent); struct directory *parent);
#endif #endif

View File

@ -18,6 +18,7 @@
*/ */
#include "update.h" #include "update.h"
#include "directory.h"
#include "log.h" #include "log.h"
#include "ls.h" #include "ls.h"
#include "path.h" #include "path.h"
@ -53,14 +54,16 @@ int isUpdatingDB(void)
return (progress != UPDATE_PROGRESS_IDLE) ? update_task_id : 0; return (progress != UPDATE_PROGRESS_IDLE) ? update_task_id : 0;
} }
static void directory_set_stat(Directory * dir, const struct stat *st) static void
directory_set_stat(struct directory *dir, const struct stat *st)
{ {
dir->inode = st->st_ino; dir->inode = st->st_ino;
dir->device = st->st_dev; dir->device = st->st_dev;
dir->stat = 1; dir->stat = 1;
} }
static void delete_song(Directory *dir, Song *del) static void
delete_song(struct directory *dir, Song *del)
{ {
/* first, prevent traversers in main task from getting this */ /* first, prevent traversers in main task from getting this */
songvec_delete(&dir->songs, del); songvec_delete(&dir->songs, del);
@ -79,7 +82,7 @@ static void delete_song(Directory *dir, Song *del)
struct delete_data { struct delete_data {
char *tmp; char *tmp;
Directory *dir; struct directory *dir;
enum update_return ret; enum update_return ret;
}; };
@ -99,7 +102,7 @@ static int delete_song_if_removed(Song *song, void *_data)
} }
static enum update_return static enum update_return
removeDeletedFromDirectory(char *path_max_tmp, Directory * directory) removeDeletedFromDirectory(char *path_max_tmp, struct directory *directory)
{ {
enum update_return ret = UPDATE_RETURN_NOUPDATE; enum update_return ret = UPDATE_RETURN_NOUPDATE;
int i; int i;
@ -130,7 +133,8 @@ static const char *opendir_path(char *path_max_tmp, const char *dirname)
return musicDir; return musicDir;
} }
static int statDirectory(Directory * dir) static int
statDirectory(struct directory *dir)
{ {
struct stat st; struct stat st;
@ -142,7 +146,8 @@ static int statDirectory(Directory * dir)
return 0; return 0;
} }
static int inodeFoundInParent(Directory * parent, ino_t inode, dev_t device) static int
inodeFoundInParent(struct directory *parent, ino_t inode, dev_t device)
{ {
while (parent) { while (parent) {
if (!parent->stat && statDirectory(parent) < 0) if (!parent->stat && statDirectory(parent) < 0)
@ -158,10 +163,10 @@ static int inodeFoundInParent(Directory * parent, ino_t inode, dev_t device)
} }
static enum update_return static enum update_return
addSubDirectoryToDirectory(Directory * directory, addSubDirectoryToDirectory(struct directory *directory,
const char *name, struct stat *st) const char *name, struct stat *st)
{ {
Directory *subDirectory; struct directory *subDirectory;
if (inodeFoundInParent(directory, st->st_ino, st->st_dev)) if (inodeFoundInParent(directory, st->st_ino, st->st_dev))
return UPDATE_RETURN_NOUPDATE; return UPDATE_RETURN_NOUPDATE;
@ -180,7 +185,7 @@ addSubDirectoryToDirectory(Directory * directory,
} }
static enum update_return static enum update_return
addToDirectory(Directory * directory, const char *name) addToDirectory(struct directory *directory, const char *name)
{ {
struct stat st; struct stat st;
@ -209,7 +214,7 @@ addToDirectory(Directory * directory, const char *name)
} }
static enum update_return static enum update_return
updateInDirectory(Directory * directory, const char *name) updateInDirectory(struct directory *directory, const char *name)
{ {
Song *song; Song *song;
struct stat st; struct stat st;
@ -230,7 +235,7 @@ updateInDirectory(Directory * directory, const char *name)
return UPDATE_RETURN_UPDATED; return UPDATE_RETURN_UPDATED;
} }
} else if (S_ISDIR(st.st_mode)) { } else if (S_ISDIR(st.st_mode)) {
Directory *subdir = dirvec_find(&directory->children, name); struct directory *subdir = dirvec_find(&directory->children, name);
if (subdir) { if (subdir) {
assert(directory == subdir->parent); assert(directory == subdir->parent);
directory_set_stat(subdir, &st); directory_set_stat(subdir, &st);
@ -250,7 +255,7 @@ static int skip_path(const char *path)
} }
enum update_return enum update_return
updateDirectory(Directory * directory) updateDirectory(struct directory *directory)
{ {
bool was_empty = directory_is_empty(directory); bool was_empty = directory_is_empty(directory);
DIR *dir; DIR *dir;
@ -301,12 +306,13 @@ updateDirectory(Directory * directory)
return ret; return ret;
} }
static Directory *addDirectoryPathToDB(const char *utf8path) static struct directory *
addDirectoryPathToDB(const char *utf8path)
{ {
char path_max_tmp[MPD_PATH_MAX]; char path_max_tmp[MPD_PATH_MAX];
char *parent; char *parent;
Directory *parentDirectory; struct directory *parentDirectory;
Directory *directory; struct directory *directory;
Song *conflicting; Song *conflicting;
parent = parent_path(path_max_tmp, utf8path); parent = parent_path(path_max_tmp, utf8path);
@ -342,11 +348,12 @@ static Directory *addDirectoryPathToDB(const char *utf8path)
return directory; return directory;
} }
static Directory *addParentPathToDB(const char *utf8path) static struct directory *
addParentPathToDB(const char *utf8path)
{ {
char *parent; char *parent;
char path_max_tmp[MPD_PATH_MAX]; char path_max_tmp[MPD_PATH_MAX];
Directory *parentDirectory; struct directory *parentDirectory;
parent = parent_path(path_max_tmp, utf8path); parent = parent_path(path_max_tmp, utf8path);
@ -358,13 +365,13 @@ static Directory *addParentPathToDB(const char *utf8path)
if (!parentDirectory) if (!parentDirectory)
return NULL; return NULL;
return (Directory *) parentDirectory; return (struct directory *) parentDirectory;
} }
static enum update_return updatePath(const char *utf8path) static enum update_return updatePath(const char *utf8path)
{ {
Directory *directory; struct directory *directory;
Directory *parentDirectory; struct directory *parentDirectory;
Song *song; Song *song;
char *path = sanitizePathDup(utf8path); char *path = sanitizePathDup(utf8path);
time_t mtime; time_t mtime;

View File

@ -20,7 +20,7 @@
#ifndef UPDATE_H #ifndef UPDATE_H
#define UPDATE_H #define UPDATE_H
#include "directory.h" struct directory;
enum update_return { enum update_return {
UPDATE_RETURN_ERROR = -1, UPDATE_RETURN_ERROR = -1,
@ -31,7 +31,7 @@ enum update_return {
int isUpdatingDB(void); int isUpdatingDB(void);
enum update_return enum update_return
updateDirectory(Directory * directory); updateDirectory(struct directory *directory);
/* /*
* returns the non-negative update job ID on success, * returns the non-negative update job ID on success,