From 53dee9b300a0d153bf2b97c1fb05609b4612cfb8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kjetil=20=C3=98rbekk?= Date: Thu, 14 Aug 2008 12:03:55 +0200 Subject: - Added license information (GPLv2) --- COPYING | 339 ++++++++++++++++++++++++ README | 20 +- include/debug.h | 20 ++ include/musicfs.h | 20 ++ src/mfs_subr.c | 757 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ src/mfs_vnops.c | 235 +++++++++++++++++ src/mp3_subr.c | 736 ---------------------------------------------------- src/mp3_vnops.c | 214 --------------- src/mp3fs.c | 21 ++ 9 files changed, 1411 insertions(+), 951 deletions(-) create mode 100644 COPYING create mode 100644 src/mfs_subr.c create mode 100755 src/mfs_vnops.c delete mode 100644 src/mp3_subr.c delete mode 100755 src/mp3_vnops.c diff --git a/COPYING b/COPYING new file mode 100644 index 0000000..d511905 --- /dev/null +++ b/COPYING @@ -0,0 +1,339 @@ + GNU GENERAL PUBLIC LICENSE + Version 2, June 1991 + + Copyright (C) 1989, 1991 Free Software Foundation, Inc., + 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + Everyone is permitted to copy and distribute verbatim copies + of this license document, but changing it is not allowed. + + Preamble + + The licenses for most software are designed to take away your +freedom to share and change it. By contrast, the GNU General Public +License is intended to guarantee your freedom to share and change free +software--to make sure the software is free for all its users. This +General Public License applies to most of the Free Software +Foundation's software and to any other program whose authors commit to +using it. (Some other Free Software Foundation software is covered by +the GNU Lesser General Public License instead.) You can apply it to +your programs, too. + + When we speak of free software, we are referring to freedom, not +price. Our General Public Licenses are designed to make sure that you +have the freedom to distribute copies of free software (and charge for +this service if you wish), that you receive source code or can get it +if you want it, that you can change the software or use pieces of it +in new free programs; and that you know you can do these things. + + To protect your rights, we need to make restrictions that forbid +anyone to deny you these rights or to ask you to surrender the rights. +These restrictions translate to certain responsibilities for you if you +distribute copies of the software, or if you modify it. + + For example, if you distribute copies of such a program, whether +gratis or for a fee, you must give the recipients all the rights that +you have. You must make sure that they, too, receive or can get the +source code. And you must show them these terms so they know their +rights. + + We protect your rights with two steps: (1) copyright the software, and +(2) offer you this license which gives you legal permission to copy, +distribute and/or modify the software. + + Also, for each author's protection and ours, we want to make certain +that everyone understands that there is no warranty for this free +software. If the software is modified by someone else and passed on, we +want its recipients to know that what they have is not the original, so +that any problems introduced by others will not reflect on the original +authors' reputations. + + Finally, any free program is threatened constantly by software +patents. We wish to avoid the danger that redistributors of a free +program will individually obtain patent licenses, in effect making the +program proprietary. To prevent this, we have made it clear that any +patent must be licensed for everyone's free use or not licensed at all. + + The precise terms and conditions for copying, distribution and +modification follow. + + GNU GENERAL PUBLIC LICENSE + TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION + + 0. This License applies to any program or other work which contains +a notice placed by the copyright holder saying it may be distributed +under the terms of this General Public License. The "Program", below, +refers to any such program or work, and a "work based on the Program" +means either the Program or any derivative work under copyright law: +that is to say, a work containing the Program or a portion of it, +either verbatim or with modifications and/or translated into another +language. (Hereinafter, translation is included without limitation in +the term "modification".) Each licensee is addressed as "you". + +Activities other than copying, distribution and modification are not +covered by this License; they are outside its scope. The act of +running the Program is not restricted, and the output from the Program +is covered only if its contents constitute a work based on the +Program (independent of having been made by running the Program). +Whether that is true depends on what the Program does. + + 1. You may copy and distribute verbatim copies of the Program's +source code as you receive it, in any medium, provided that you +conspicuously and appropriately publish on each copy an appropriate +copyright notice and disclaimer of warranty; keep intact all the +notices that refer to this License and to the absence of any warranty; +and give any other recipients of the Program a copy of this License +along with the Program. + +You may charge a fee for the physical act of transferring a copy, and +you may at your option offer warranty protection in exchange for a fee. + + 2. You may modify your copy or copies of the Program or any portion +of it, thus forming a work based on the Program, and copy and +distribute such modifications or work under the terms of Section 1 +above, provided that you also meet all of these conditions: + + a) You must cause the modified files to carry prominent notices + stating that you changed the files and the date of any change. + + b) You must cause any work that you distribute or publish, that in + whole or in part contains or is derived from the Program or any + part thereof, to be licensed as a whole at no charge to all third + parties under the terms of this License. + + c) If the modified program normally reads commands interactively + when run, you must cause it, when started running for such + interactive use in the most ordinary way, to print or display an + announcement including an appropriate copyright notice and a + notice that there is no warranty (or else, saying that you provide + a warranty) and that users may redistribute the program under + these conditions, and telling the user how to view a copy of this + License. (Exception: if the Program itself is interactive but + does not normally print such an announcement, your work based on + the Program is not required to print an announcement.) + +These requirements apply to the modified work as a whole. If +identifiable sections of that work are not derived from the Program, +and can be reasonably considered independent and separate works in +themselves, then this License, and its terms, do not apply to those +sections when you distribute them as separate works. But when you +distribute the same sections as part of a whole which is a work based +on the Program, the distribution of the whole must be on the terms of +this License, whose permissions for other licensees extend to the +entire whole, and thus to each and every part regardless of who wrote it. + +Thus, it is not the intent of this section to claim rights or contest +your rights to work written entirely by you; rather, the intent is to +exercise the right to control the distribution of derivative or +collective works based on the Program. + +In addition, mere aggregation of another work not based on the Program +with the Program (or with a work based on the Program) on a volume of +a storage or distribution medium does not bring the other work under +the scope of this License. + + 3. You may copy and distribute the Program (or a work based on it, +under Section 2) in object code or executable form under the terms of +Sections 1 and 2 above provided that you also do one of the following: + + a) Accompany it with the complete corresponding machine-readable + source code, which must be distributed under the terms of Sections + 1 and 2 above on a medium customarily used for software interchange; or, + + b) Accompany it with a written offer, valid for at least three + years, to give any third party, for a charge no more than your + cost of physically performing source distribution, a complete + machine-readable copy of the corresponding source code, to be + distributed under the terms of Sections 1 and 2 above on a medium + customarily used for software interchange; or, + + c) Accompany it with the information you received as to the offer + to distribute corresponding source code. (This alternative is + allowed only for noncommercial distribution and only if you + received the program in object code or executable form with such + an offer, in accord with Subsection b above.) + +The source code for a work means the preferred form of the work for +making modifications to it. For an executable work, complete source +code means all the source code for all modules it contains, plus any +associated interface definition files, plus the scripts used to +control compilation and installation of the executable. However, as a +special exception, the source code distributed need not include +anything that is normally distributed (in either source or binary +form) with the major components (compiler, kernel, and so on) of the +operating system on which the executable runs, unless that component +itself accompanies the executable. + +If distribution of executable or object code is made by offering +access to copy from a designated place, then offering equivalent +access to copy the source code from the same place counts as +distribution of the source code, even though third parties are not +compelled to copy the source along with the object code. + + 4. You may not copy, modify, sublicense, or distribute the Program +except as expressly provided under this License. Any attempt +otherwise to copy, modify, sublicense or distribute the Program is +void, and will automatically terminate your rights under this License. +However, parties who have received copies, or rights, from you under +this License will not have their licenses terminated so long as such +parties remain in full compliance. + + 5. You are not required to accept this License, since you have not +signed it. However, nothing else grants you permission to modify or +distribute the Program or its derivative works. These actions are +prohibited by law if you do not accept this License. Therefore, by +modifying or distributing the Program (or any work based on the +Program), you indicate your acceptance of this License to do so, and +all its terms and conditions for copying, distributing or modifying +the Program or works based on it. + + 6. Each time you redistribute the Program (or any work based on the +Program), the recipient automatically receives a license from the +original licensor to copy, distribute or modify the Program subject to +these terms and conditions. You may not impose any further +restrictions on the recipients' exercise of the rights granted herein. +You are not responsible for enforcing compliance by third parties to +this License. + + 7. If, as a consequence of a court judgment or allegation of patent +infringement or for any other reason (not limited to patent issues), +conditions are imposed on you (whether by court order, agreement or +otherwise) that contradict the conditions of this License, they do not +excuse you from the conditions of this License. If you cannot +distribute so as to satisfy simultaneously your obligations under this +License and any other pertinent obligations, then as a consequence you +may not distribute the Program at all. For example, if a patent +license would not permit royalty-free redistribution of the Program by +all those who receive copies directly or indirectly through you, then +the only way you could satisfy both it and this License would be to +refrain entirely from distribution of the Program. + +If any portion of this section is held invalid or unenforceable under +any particular circumstance, the balance of the section is intended to +apply and the section as a whole is intended to apply in other +circumstances. + +It is not the purpose of this section to induce you to infringe any +patents or other property right claims or to contest validity of any +such claims; this section has the sole purpose of protecting the +integrity of the free software distribution system, which is +implemented by public license practices. Many people have made +generous contributions to the wide range of software distributed +through that system in reliance on consistent application of that +system; it is up to the author/donor to decide if he or she is willing +to distribute software through any other system and a licensee cannot +impose that choice. + +This section is intended to make thoroughly clear what is believed to +be a consequence of the rest of this License. + + 8. If the distribution and/or use of the Program is restricted in +certain countries either by patents or by copyrighted interfaces, the +original copyright holder who places the Program under this License +may add an explicit geographical distribution limitation excluding +those countries, so that distribution is permitted only in or among +countries not thus excluded. In such case, this License incorporates +the limitation as if written in the body of this License. + + 9. The Free Software Foundation may publish revised and/or new versions +of the General Public License from time to time. Such new versions will +be similar in spirit to the present version, but may differ in detail to +address new problems or concerns. + +Each version is given a distinguishing version number. If the Program +specifies a version number of this License which applies to it and "any +later version", you have the option of following the terms and conditions +either of that version or of any later version published by the Free +Software Foundation. If the Program does not specify a version number of +this License, you may choose any version ever published by the Free Software +Foundation. + + 10. If you wish to incorporate parts of the Program into other free +programs whose distribution conditions are different, write to the author +to ask for permission. For software which is copyrighted by the Free +Software Foundation, write to the Free Software Foundation; we sometimes +make exceptions for this. Our decision will be guided by the two goals +of preserving the free status of all derivatives of our free software and +of promoting the sharing and reuse of software generally. + + NO WARRANTY + + 11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY +FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN +OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES +PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED +OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF +MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS +TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE +PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, +REPAIR OR CORRECTION. + + 12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING +WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR +REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, +INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING +OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED +TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY +YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER +PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE +POSSIBILITY OF SUCH DAMAGES. + + END OF TERMS AND CONDITIONS + + How to Apply These Terms to Your New Programs + + If you develop a new program, and you want it to be of the greatest +possible use to the public, the best way to achieve this is to make it +free software which everyone can redistribute and change under these terms. + + To do so, attach the following notices to the program. It is safest +to attach them to the start of each source file to most effectively +convey the exclusion of warranty; and each file should have at least +the "copyright" line and a pointer to where the full notice is found. + + + Copyright (C) + + 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. + +Also add information on how to contact you by electronic and paper mail. + +If the program is interactive, make it output a short notice like this +when it starts in an interactive mode: + + Gnomovision version 69, Copyright (C) year name of author + Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type `show w'. + This is free software, and you are welcome to redistribute it + under certain conditions; type `show c' for details. + +The hypothetical commands `show w' and `show c' should show the appropriate +parts of the General Public License. Of course, the commands you use may +be called something other than `show w' and `show c'; they could even be +mouse-clicks or menu items--whatever suits your program. + +You should also get your employer (if you work as a programmer) or your +school, if any, to sign a "copyright disclaimer" for the program, if +necessary. Here is a sample; alter the names: + + Yoyodyne, Inc., hereby disclaims all copyright interest in the program + `Gnomovision' (which makes passes at compilers) written by James Hacker. + + , 1 April 1989 + Ty Coon, President of Vice + +This General Public License does not permit incorporating your program into +proprietary programs. If your program is a subroutine library, you may +consider it more useful to permit linking proprietary applications with the +library. If this is what you want to do, use the GNU Lesser General +Public License instead of this License. diff --git a/README b/README index f17e811..0010ca7 100755 --- a/README +++ b/README @@ -1,4 +1,4 @@ -musicfs is a FUSE module implementing a mp3 filesystem in +musicfs is a FUSE module implementing a media filesystem in userland. The purpose of musicfs is to provide filesystem hierarchy based on id3 tags of mp3-files. @@ -6,3 +6,21 @@ Dependencies ~~~~~~~~~~~~ - taglib 1.5 - FUSE v.2.6? + +License +~~~~~~~ +This program is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License version 2, as +published by the Free Software Foundation. + +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. + +A copy of the license can typically be found in COPYING diff --git a/include/debug.h b/include/debug.h index a14900a..a30af63 100644 --- a/include/debug.h +++ b/include/debug.h @@ -1,3 +1,23 @@ +/* + * Musicfs is a FUSE module implementing a media filesystem in userland. + * Copyright (C) 2008 Ulf Lilleengen, Kjetil Ørbekk + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License version + * 2, as published by the Free Software Foundation. + * + * 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. + * + * A copy of the license can typically be found in COPYING + */ + #ifndef _DEBUG_H_ #define _DEBUG_H_ diff --git a/include/musicfs.h b/include/musicfs.h index 5f92946..3da10da 100644 --- a/include/musicfs.h +++ b/include/musicfs.h @@ -1,3 +1,23 @@ +/* + * Musicfs is a FUSE module implementing a media filesystem in userland. + * Copyright (C) 2008 Ulf Lilleengen, Kjetil Ørbekk + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License version + * 2, as published by the Free Software Foundation. + * + * 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. + * + * A copy of the license can typically be found in COPYING + */ + #ifndef _MP3FS_H_ #define _MP3FS_H_ struct fuse_args; diff --git a/src/mfs_subr.c b/src/mfs_subr.c new file mode 100644 index 0000000..26daa52 --- /dev/null +++ b/src/mfs_subr.c @@ -0,0 +1,757 @@ +/* -*- Mode: C; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*- */ + +/* + * Musicfs is a FUSE module implementing a media filesystem in userland. + * Copyright (C) 2008 Ulf Lilleengen, Kjetil Ørbekk + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License version + * 2, as published by the Free Software Foundation. + * + * 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. + * + * A copy of the license can typically be found in COPYING + */ + +/* Miscellaneous subroutines for musicfs. */ +#define FUSE_USE_VERSION 26 +#include +#include +#include +#include +#include + +#include +#include +#include +#include + +#include +#include +#include +#include + +struct lookuphandle { + sqlite3 *handle; + sqlite3_stmt *st; + const char *query; + int field; + int count; + void *priv; + lookup_fn_t *lookup; +}; + +sqlite3 *handle; + +int +mfs_initscan(char *musicpath) +{ + int error; + + /* Open database. */ + error = sqlite3_open(DBNAME, &handle); + if (error) { + warnx("Can't open database: %s\n", sqlite3_errmsg(handle)); + sqlite3_close(handle); + return (-1); + } + traverse_hierarchy(musicpath, mfs_scan); + sqlite3_close(handle); + return (0); +} + +/* + * Traverse a hierarchy given a directory path. Perform fileoperations on the + * files in the directory giving data as arguments, as well as recursing on + * sub-directories. + */ +void +traverse_hierarchy(char *dirpath, traverse_fn_t fileop) +{ + DIR *dirp; + struct dirent *dp; + char filepath[MAXPATHLEN]; + struct stat st; + + dirp = opendir(dirpath); + if (dirp == NULL) + return; + + /* Loop through all files. */ + while ((dp = readdir(dirp)) != NULL) { + if (!strcmp(dp->d_name, ".") || + !strcmp(dp->d_name, "..")) + continue; + + snprintf(filepath, sizeof(filepath), "%s/%s", + dirpath, dp->d_name); + if (stat(filepath, &st) < 0) + err(1, "error doing stat on %s", filepath); + /* Recurse if it's a directory. */ + if (st.st_mode & S_IFDIR) { + traverse_hierarchy(filepath, fileop); + continue; + } + /* If it's a regular file, perform the operation. */ + if (st.st_mode & S_IFREG) { + fileop(filepath); + } + } + closedir(dirp); +} + +/* Scan the music initially. */ +void +mfs_scan(char *filepath) +{ + TagLib_File *file; + TagLib_Tag *tag; + char *artist, *album, *genre, *title, *trackno; + const char *extension; + int ret; + unsigned int track, year; + sqlite3_stmt *st; + struct stat fstat; + + file = taglib_file_new(filepath); + /* XXX: errmsg. */ + if (file == NULL) + return; + tag = taglib_file_tag(file); + if (tag == NULL) { + printf("error!\n"); + return; + } + + if (stat(filepath, &fstat) < 0) { + perror("stat"); + return; + } + + /* XXX: The main query code should perhaps be a bit generalized. */ + + /* First insert artist if we have it. */ + do { + artist = taglib_tag_artist(tag); + if (artist == NULL) + break; + /* First find out if it exists. */ + ret = sqlite3_prepare_v2(handle, "SELECT * FROM artist WHERE " + "name=?", -1, &st, NULL); + if (ret != SQLITE_OK) { + warnx("Error preparing statement: %s\n", + sqlite3_errmsg(handle)); + break; + } + sqlite3_bind_text(st, 1, artist, -1, SQLITE_STATIC); + ret = sqlite3_step(st); + sqlite3_finalize(st); + /* Already exists or generic error. */ + if (ret != SQLITE_DONE) + break; + /* Doesn't exist, so we can insert it. */ + ret = sqlite3_prepare_v2(handle, "INSERT INTO artist(name) " + "VALUES(?)", -1, &st, NULL); + if (ret != SQLITE_OK) { + warnx("Error preparing statement: %s\n", + sqlite3_errmsg(handle)); + break; + } + sqlite3_bind_text(st, 1, artist, -1, SQLITE_STATIC); + ret = sqlite3_step(st); + sqlite3_finalize(st); + if (ret != SQLITE_DONE) { + warnx("Error inserting into database: %s\n", + sqlite3_errmsg(handle)); + break; + } + } while (0); + + /* Insert genre if it doesn't exist. */ + do { + genre = taglib_tag_genre(tag); + if (genre == NULL) + break; + /* First find out if it exists. */ + ret = sqlite3_prepare_v2(handle, "SELECT * FROM genre WHERE " + "name=?", -1, &st, NULL); + if (ret != SQLITE_OK) { + warnx("Error preparing statement: %s\n", + sqlite3_errmsg(handle)); + break; + } + sqlite3_bind_text(st, 1, genre, -1, SQLITE_STATIC); + ret = sqlite3_step(st); + sqlite3_finalize(st); + /* Already exists or generic error. */ + if (ret != SQLITE_DONE) + break; + /* Doesn't exist, so we can insert it. */ + ret = sqlite3_prepare_v2(handle, "INSERT INTO genre(name) " + "VALUES(?)", -1, &st, NULL); + if (ret != SQLITE_OK) { + warnx("Error preparing statement: %s\n", + sqlite3_errmsg(handle)); + break; + } + sqlite3_bind_text(st, 1, genre, -1, SQLITE_STATIC); + ret = sqlite3_step(st); + sqlite3_finalize(st); + if (ret != SQLITE_DONE) { + warnx("Error inserting into database: %s\n", + sqlite3_errmsg(handle)); + break; + } + } while (0); + + /* Finally, insert song. */ + do { + title = taglib_tag_title(tag); + album = taglib_tag_album(tag); + track = taglib_tag_track(tag); + year = taglib_tag_year(tag); + extension = strrchr(filepath, (int)'.'); + if (extension == NULL) + extension = ""; + else + /* Drop the '.' */ + extension++; + + if (title == NULL || genre == NULL || artist == NULL || + album == NULL) + break; + + /* First find out if it exists. */ + ret = sqlite3_prepare_v2(handle, "SELECT * FROM song, artist " + " WHERE artist.name=song.artistname AND title=? AND year=?" + " AND song.album=?", + -1, &st, NULL); + if (ret != SQLITE_OK) { + warnx("Error preparing statement: %s\n", + sqlite3_errmsg(handle)); + break; + } + sqlite3_bind_text(st, 1, title, -1, SQLITE_STATIC); + sqlite3_bind_int(st, 2, year); + sqlite3_bind_text(st, 3, album, -1, SQLITE_STATIC); + ret = sqlite3_step(st); + sqlite3_finalize(st); + if (ret == SQLITE_ROW) + /* Already exists. XXX: Update it if mtime mismatches */ + break; + if (ret != SQLITE_DONE) + /* SQL Error. */ + break; + /* Now, finally insert it. */ + ret = sqlite3_prepare_v2(handle, "INSERT INTO song(title, " + "artistname, album, genrename, year, track, filepath, " + "mtime, extension) VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?)", + -1, &st, NULL); + if (ret != SQLITE_OK) { + warnx("Error preparing insert statement: %s\n", + sqlite3_errmsg(handle)); + break; + } + sqlite3_bind_text(st, 1, title, -1, SQLITE_STATIC); + sqlite3_bind_text(st, 2, artist, -1, SQLITE_STATIC); + sqlite3_bind_text(st, 3, album, -1, SQLITE_STATIC); + sqlite3_bind_text(st, 4, genre, -1, SQLITE_STATIC); + sqlite3_bind_int(st, 5, year); + + if (track) { + trackno = malloc(sizeof(char) * 9); + sprintf(trackno, "%02d", track); + sqlite3_bind_text(st, 6, trackno, -1, SQLITE_TRANSIENT); + free(trackno); + } else { + sqlite3_bind_text(st, 6, "", -1, SQLITE_STATIC); + } + + sqlite3_bind_text(st, 7, filepath, -1, SQLITE_STATIC); + sqlite3_bind_int(st, 8, fstat.st_mtime); + sqlite3_bind_text(st, 9, extension, -1, SQLITE_STATIC); + ret = sqlite3_step(st); + sqlite3_finalize(st); + if (ret != SQLITE_DONE) { + warnx("Error inserting into database: %s\n", + sqlite3_errmsg(handle)); + break; + } + } while (0); + taglib_tag_free_strings(); + taglib_file_free(file); +} + +/* + * Create a handle for listing music with a certain query. Allocate the + * resources and return the handle. + */ +struct lookuphandle * +mfs_lookup_start(int field, void *data, lookup_fn_t *fn, const char *query) +{ + struct lookuphandle *lh; + int ret, error; + + lh = malloc(sizeof(*lh)); + if (lh == NULL) + return (NULL); + lh->field = field; + lh->lookup = fn; + lh->priv = data; + /* Open database. */ + error = sqlite3_open(DBNAME, &lh->handle); + if (error) { + warnx("Can't open database: %s\n", sqlite3_errmsg(lh->handle)); + sqlite3_close(lh->handle); + free(lh); + return (NULL); + } + ret = sqlite3_prepare_v2(lh->handle, query, -1, &lh->st, NULL); + if (ret != SQLITE_OK) { + free(lh); + return (NULL); + } + lh->query = query; + lh->count = 1; + return (lh); +} + +/* + * Insert data that should be searched for in the list. The data is assumed to + * be dynamically allocated, and will be free'd when mfs_lookup_finish is called! + */ +void +mfs_lookup_insert(struct lookuphandle *lh, void *data, int type) +{ + char *str; + int val; + + switch (type) { + case LIST_DATATYPE_STRING: + str = (char *)data; + sqlite3_bind_text(lh->st, lh->count++, str, -1, free); + break; + case LIST_DATATYPE_INT: + val = *((int *)data); + sqlite3_bind_int(lh->st, lh->count++, val); + break; + } +} + +/* + * Finish a statement buildup and use the lookup function to operate on the + * returned data. Free the handle when done. + */ +void +mfs_lookup_finish(struct lookuphandle *lh) +{ + char buf[1024]; + const unsigned char *value; + int ret, type, val, finished; + + if (lh == NULL) + return; + ret = sqlite3_step(lh->st); + while (ret == SQLITE_ROW) { + type = sqlite3_column_type(lh->st, lh->field); + switch (type) { + case SQLITE_INTEGER: + val = sqlite3_column_int(lh->st, lh->field); + snprintf(buf, sizeof(buf), "%d", val); + finished = lh->lookup(lh->priv, (const char *)buf); + break; + case SQLITE_TEXT: + value = sqlite3_column_text(lh->st, lh->field); + finished = lh->lookup(lh->priv, (const char *)value); + break; + default: + finished = 0; + break; +// default: +// lh->filler(lh->buf, "UNKNOWN TYPE", NULL, 0); + } + if (finished) + break; + ret = sqlite3_step(lh->st); + } + // XXX: Check for errors too. + sqlite3_finalize(lh->st); + sqlite3_close(lh->handle); + free(lh); +} + +/* + * Build a file_data struct for a path. + * + * data should be a pointer to a file handle + * returns 0 on success + */ +int +mfs_file_data_for_path(const char *path, void *data) { + DEBUG("getting file data for %s\n", path); + struct file_data *fd; + fd = (struct file_data *)data; + struct lookuphandle *lh; + char *artist, *album, *title; + + lh = NULL; + fd->fd = -1; + fd->found = 0; + + /* Open a specific track. */ + if (strncmp(path, "/Tracks", 7) == 0) { + switch (mfs_numtoken(path)) { + case 2: + title = mfs_gettoken(path, 2); + if (title == NULL) + break; + lh = mfs_lookup_start(0, fd, mfs_lookup_open, + "SELECT filepath FROM song " + "WHERE (artistname||' - '||title||'.'||extension) LIKE ?"); + if (lh == NULL) + return (-EIO); + mfs_lookup_insert(lh, title, LIST_DATATYPE_STRING); + break; + default: + return (-ENOENT); + } + } else if (strncmp(path, "/Albums", 7) == 0) { + switch (mfs_numtoken(path)) { + case 3: + album = mfs_gettoken(path, 2); + if (album == NULL) + break; + title = mfs_gettoken(path, 3); + if (title == NULL) + break; + lh = mfs_lookup_start(0, fd, mfs_lookup_open, + "SELECT filepath FROM song " + "WHERE (title||'.'||extension) LIKE ? AND " + "album LIKE ?"); + if (lh == NULL) + return (-EIO); + mfs_lookup_insert(lh, title, LIST_DATATYPE_STRING); + mfs_lookup_insert(lh, album, LIST_DATATYPE_STRING); + break; + default: + return (-ENOENT); + } + } else if (strncmp(path, "/Artists", 8) == 0) { + switch (mfs_numtoken(path)) { + case 4: + artist = mfs_gettoken(path, 2); + album = mfs_gettoken(path, 3); + title = mfs_gettoken(path, 4); + DEBUG("artist(%s) album(%s) title(%s)\n", artist, album, title); + if (!(artist && album && title)) { + break; + } + lh = mfs_lookup_start(0, fd, mfs_lookup_open, + "SELECT filepath FROM song WHERE artistname LIKE ? AND " + "album LIKE ? AND " + "(LTRIM(track||' ')||title||'.'||extension) LIKE ?"); + if (lh == NULL) + return (-EIO); + mfs_lookup_insert(lh, artist, LIST_DATATYPE_STRING); + mfs_lookup_insert(lh, album, LIST_DATATYPE_STRING); + mfs_lookup_insert(lh, title, LIST_DATATYPE_STRING); + break; + default: + return (-ENOENT); + } + } + + if (lh) { + mfs_lookup_finish(lh); + } + + return 0; +} + +/* + * Count number of tokens in pathname. + * XXX: should we strip the first and last? + */ +int +mfs_numtoken(const char *str) +{ + const char *ptr; + int num; + + num = 0; + ptr = str; + if (*ptr == '/') + ptr++; + while ((*ptr != '\0')) { + if (*ptr++ == '/') + num++; + } + if (*(ptr - 1) == '/') + num--; + return (num + 1); +} + +/* + * Extract token number toknum and return it in escaped form. + */ +char * +mfs_gettoken(const char *str, int toknum) +{ + const char *ptr, *start, *end; + char *ret, *dest; + int numescape; + int i; + size_t size; + + start = str; + /* Find the token start. */ + for (i = 0; i < toknum; i++) { + start = strchr(start, '/'); + if (start == NULL) + return (NULL); + start++; + } + /* Find the end pointer. */ + end = strchr(start, '/'); + if (end == NULL) + end = strchr(start, '\0'); + /* + * Count how many bytes extra to take into account because of escaping. + */ + numescape = 0; + ptr = start; + while ((ptr = strchr(ptr, '\'')) != NULL && ptr < end) { + ptr++; + numescape++; + } + size = (end - start) + numescape; + ret = malloc(size + 1); + if (ret == NULL) + return (ret); + ptr = start; + dest = ret; + while (ptr < end) { + if (*ptr == '\'') + *dest++ = '\\'; + *dest++ = *ptr++; + } + ret[size] = '\0'; + return (ret); +} + +/* + * List album given a path. + */ +void +mfs_lookup_album(const char *path, struct filler_data *fd) +{ + struct lookuphandle *lh; + char *album; + + switch (mfs_numtoken(path)) { + case 1: + lh = mfs_lookup_start(0, fd, mfs_lookup_list, + "SELECT DISTINCT album FROM song"); + break; + case 2: + /* So, now we got to find out the artist and list its albums. */ + album = mfs_gettoken(path, 2); + if (album == NULL) + break; + lh = mfs_lookup_start(0, fd, mfs_lookup_list, + "SELECT DISTINCT title||'.'||extension FROM song " + "WHERE album LIKE ?"); + mfs_lookup_insert(lh, album, LIST_DATATYPE_STRING); + break; + } + mfs_lookup_finish(lh); +} + +/* + * List artist given a path. + */ +void +mfs_lookup_artist(const char *path, struct filler_data *fd) +{ + struct lookuphandle *lh; + char *name, *album; + + switch (mfs_numtoken(path)) { + case 1: + lh = mfs_lookup_start(0, fd, mfs_lookup_list, + "SELECT name FROM artist"); + break; + case 2: + /* So, now we got to find out the artist and list its albums. */ + name = mfs_gettoken(path, 2); + if (name == NULL) + break; + lh = mfs_lookup_start(0, fd, mfs_lookup_list, + "SELECT DISTINCT album FROM song, " + "artist WHERE song.artistname = artist.name AND artist.name" + " LIKE ?"); + mfs_lookup_insert(lh, name, LIST_DATATYPE_STRING); + break; + case 3: + /* List songs in an album. */ + name = mfs_gettoken(path, 2); + if (name == NULL) + break; + album = mfs_gettoken(path, 3); + if (album == NULL) + break; + lh = mfs_lookup_start(0, fd, mfs_lookup_list, + "SELECT LTRIM(track||' ')||title||'.'||extension " + "FROM song, artist " + "WHERE song.artistname = artist.name AND artist.name " + "LIKE ? AND song.album LIKE ?"); + mfs_lookup_insert(lh, name, LIST_DATATYPE_STRING); + mfs_lookup_insert(lh, album, LIST_DATATYPE_STRING); + break; + } + mfs_lookup_finish(lh); +} + +/* + * Looks up tracks given a genre, or all genres. + */ +void +mfs_lookup_genre(const char *path, struct filler_data *fd) +{ + struct lookuphandle *lh; + char *genre, *album; + + switch (mfs_numtoken(path)) { + case 1: + lh = mfs_lookup_start(0, fd, mfs_lookup_list, + "SELECT name FROM genre"); + break; + case 2: + genre = mfs_gettoken(path, 2); + if (genre == NULL) + break; + lh = mfs_lookup_start(0, fd, mfs_lookup_list, + "SELECT DISTINCT album FROM song, " + "genre WHERE song.genrename = genre.name AND genre.name " + "LIKE ?"); + mfs_lookup_insert(lh, genre, LIST_DATATYPE_STRING); + break; + case 3: + genre = mfs_gettoken(path, 2); + if (genre == NULL) + break; + album = mfs_gettoken(path, 3); + if (album == NULL) + break; + lh = mfs_lookup_start(0, fd, mfs_lookup_list, + "SELECT title||'.'||extension FROM song, genre WHERE" + " song.genrename = genre.name AND genre.name LIKE ? " + " AND song.album LIKE ?"); + mfs_lookup_insert(lh, genre, LIST_DATATYPE_STRING); + mfs_lookup_insert(lh, album, LIST_DATATYPE_STRING); + break; + } + mfs_lookup_finish(lh); +} + +/* + * Lookup function for filling given data into a filler. + */ +int +mfs_lookup_list(void *data, const char *str) +{ + struct filler_data *fd; + + fd = (struct filler_data *)data; + fd->filler(fd->buf, str, NULL, 0); + return (0); +} + +/* + * Lookup a file and open it if found. + */ +int +mfs_lookup_open(void *data, const char *str) +{ + struct file_data *fd; + + fd = (struct file_data *)data; + fd->fd = open(str, O_RDONLY); + fd->found = 1; + return (1); +} + +/* + * Stat a file. + * XXX: watch out for duplicates, we might stat more than once. + */ +int +mfs_lookup_stat(void *data, const char *str) +{ + struct stat *st; + + st = (struct stat *)data; + if (stat(str, st) < 0) + return (0); + return (1); +} + +/* + * Guess on a filetype for a path. + * + * Examples: + * + * - /Artists/Whoever should be a directory, since it's an + * Album. + * + * - /Tracks/Whatever should be a file, since Whatever is a song + * + * Note: This should only be used for paths inside the musicfs + * directories (Artists, Tracks, ...) + */ +enum mfs_filetype +mfs_get_filetype(const char *path) { + int tokens = mfs_numtoken(path); + + if (strncmp(path, "/Genres", 7) == 0 || + strncmp(path, "/Artists", 8) == 0) { + switch (tokens) { + case 1: + case 2: + case 3: + return (MFS_DIRECTORY); + case 4: + return (MFS_FILE); + default: + return (MFS_NOTFOUND); + } + } else if (strncmp(path, "/Albums", 7) == 0) { + switch (tokens) { + case 1: + case 2: + return (MFS_DIRECTORY); + case 3: + return (MFS_FILE); + default: + return (MFS_NOTFOUND); + } + } else if (strncmp(path, "/Tracks", 7) == 0) { + switch (tokens) { + case 1: + return (MFS_DIRECTORY); + case 2: + return (MFS_FILE); + default: + return (MFS_NOTFOUND); + } + } else { + return (MFS_NOTFOUND); + } +} diff --git a/src/mfs_vnops.c b/src/mfs_vnops.c new file mode 100755 index 0000000..60c9ad6 --- /dev/null +++ b/src/mfs_vnops.c @@ -0,0 +1,235 @@ +/* -*- Mode: C; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*- */ + +/* + * Musicfs is a FUSE module implementing a media filesystem in userland. + * Copyright (C) 2008 Ulf Lilleengen, Kjetil Ørbekk + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License version + * 2, as published by the Free Software Foundation. + * + * 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. + * + * A copy of the license can typically be found in COPYING + */ + +#define FUSE_USE_VERSION 26 +#include +#include +#include +#include +#include + +#include +#include +#include +#include +#include +#include + +#include +#include +#include + +char musicpath[MAXPATHLEN]; // = "/home/lulf/dev/musicfs/music"; +char *logpath = "/home/lulf/dev/musicfs/musicfs.log"; + +static int mfs_getattr (const char *path, struct stat *stbuf) +{ + struct file_data fd; + fd.fd = -1; + fd.found = 0; + + int status = 0; + memset (stbuf, 0, sizeof (struct stat)); + + if (strcmp (path, "/") == 0) { + stbuf->st_mode = S_IFDIR | 0755; + stbuf->st_nlink = 2; + return 0; + } + + enum mfs_filetype type = mfs_get_filetype(path); + switch (type) { + case MFS_DIRECTORY: + stbuf->st_mode = S_IFDIR | 0444; + stbuf->st_nlink = 1; + stbuf->st_size = 12; + return 0; + + case MFS_FILE: + status = mfs_file_data_for_path(path, &fd); + if (status != 0) + return (status); + + if (!fd.found) + return (-ENOENT); + if (fd.fd < 0) + return (-EIO); + + if (fstat(fd.fd, stbuf) == 0) + return (0); + else + return (-ENOENT); + + case MFS_NOTFOUND: + default: + return -ENOENT; + } +} + + +static int mfs_readdir (const char *path, void *buf, fuse_fill_dir_t filler, + off_t offset, struct fuse_file_info *fi) +{ + struct filler_data fd; + struct lookuphandle *lh; + + filler (buf, ".", NULL, 0); + filler (buf, "..", NULL, 0); + fd.buf = buf; + fd.filler = filler; + + if (!strcmp(path, "/")) { + filler(buf, "Artists", NULL, 0); + filler(buf, "Genres", NULL, 0); + filler(buf, "Tracks", NULL, 0); + filler(buf, "Albums", NULL, 0); + return (0); + } + + /* + * 1. Parse the path. + * 2. Find the mp3s that matches the tags given from the path. + * 3. Return the list of those mp3s. + */ + if (strncmp(path, "/Artists", 8) == 0) { + mfs_lookup_artist(path, &fd); + return (0); + } else if (strncmp(path, "/Genres", 7) == 0) { + mfs_lookup_genre(path, &fd); + return (0); + } else if (strcmp(path, "/Tracks") == 0) { + lh = mfs_lookup_start(0, &fd, mfs_lookup_list, + "SELECT DISTINCT artistname||' - '||title||'.'||extension " + "FROM song"); + mfs_lookup_finish(lh); + return (0); + } else if (strncmp(path, "/Albums", 7) == 0) { + mfs_lookup_album(path, &fd); + return (0); + } + + return (-ENOENT); +} + +static int mfs_open (const char *path, struct fuse_file_info *fi) +{ + struct file_data fd; + fd.fd = -1; + fd.found = 0; + + int status = mfs_file_data_for_path(path, &fd); + if (status != 0) + return (status); + + if (!fd.found) + return (-ENOENT); + if (fd.fd < 0) + return (-EIO); + close(fd.fd); + + return (0); + + /* + * 1. Have a lookup cache for names?. + * Parse Genre, Album, Artist etc. + * 2. Find MP3s that match. XXX what do we do with duplicates? just + * return the first match? + * 3. Put the mnode of the mp3 in our cache. + * 4. Signal in mnode that the mp3 is being read? + */ +} + +static int mfs_read (const char *path, char *buf, size_t size, off_t offset, + struct fuse_file_info *fi) +{ + struct file_data fd; + fd.fd = -1; + fd.found = 0; + size_t bytes; + + int status = mfs_file_data_for_path(path, &fd); + if (status != 0) + return (status); + + if (!fd.found) + return (-ENOENT); + if (fd.fd < 0) + return (-EIO); + lseek(fd.fd, offset, SEEK_CUR); + bytes = read(fd.fd, buf, size); + close(fd.fd); + return (bytes); + + /* + * 1. Find the mnode given the path. If not in cache, read through mp3 + * list to find it. + * 2. Read the offset of the mp3 and return the data. + */ +} + +static struct fuse_operations mfs_ops = { + .getattr = mfs_getattr, + .readdir = mfs_readdir, + .open = mfs_open, + .read = mfs_read, +}; + +static int musicfs_opt_proc (void *data, const char *arg, int key, + struct fuse_args *outargs) +{ + static int musicpath_set = 0; + + if (key == FUSE_OPT_KEY_NONOPT && !musicpath_set) { + /* The source directory isn't already set, let's do it */ + strcpy(musicpath, arg); + musicpath_set = 1; + return (0); + } + return (1); +} + +int +mfs_run(int argc, char **argv) +{ + int ret; + /* + * XXX: Build index of mp3's. + */ + + /* Update tables. */ + if (argc < 2) { + fprintf(stderr, "Usage: %s \n", argv[0]); + return (-1); + } + + struct fuse_args args = FUSE_ARGS_INIT (argc, argv); + + if (fuse_opt_parse(&args, NULL, NULL, musicfs_opt_proc) != 0) + exit (1); + + DEBUG("musicpath: %s\n", musicpath); + mfs_initscan(musicpath); + + ret = 0; + ret = fuse_main(args.argc, args.argv, &mfs_ops, NULL); + return (ret); +} diff --git a/src/mp3_subr.c b/src/mp3_subr.c deleted file mode 100644 index 22e533a..0000000 --- a/src/mp3_subr.c +++ /dev/null @@ -1,736 +0,0 @@ -/* -*- Mode: C; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*- */ -/* Miscellaneous subroutines for musicfs. */ -#define FUSE_USE_VERSION 26 -#include -#include -#include -#include -#include - -#include -#include -#include -#include - -#include -#include -#include -#include - -struct lookuphandle { - sqlite3 *handle; - sqlite3_stmt *st; - const char *query; - int field; - int count; - void *priv; - lookup_fn_t *lookup; -}; - -sqlite3 *handle; - -int -mfs_initscan(char *musicpath) -{ - int error; - - /* Open database. */ - error = sqlite3_open(DBNAME, &handle); - if (error) { - warnx("Can't open database: %s\n", sqlite3_errmsg(handle)); - sqlite3_close(handle); - return (-1); - } - traverse_hierarchy(musicpath, mfs_scan); - sqlite3_close(handle); - return (0); -} - -/* - * Traverse a hierarchy given a directory path. Perform fileoperations on the - * files in the directory giving data as arguments, as well as recursing on - * sub-directories. - */ -void -traverse_hierarchy(char *dirpath, traverse_fn_t fileop) -{ - DIR *dirp; - struct dirent *dp; - char filepath[MAXPATHLEN]; - struct stat st; - - dirp = opendir(dirpath); - if (dirp == NULL) - return; - - /* Loop through all files. */ - while ((dp = readdir(dirp)) != NULL) { - if (!strcmp(dp->d_name, ".") || - !strcmp(dp->d_name, "..")) - continue; - - snprintf(filepath, sizeof(filepath), "%s/%s", - dirpath, dp->d_name); - if (stat(filepath, &st) < 0) - err(1, "error doing stat on %s", filepath); - /* Recurse if it's a directory. */ - if (st.st_mode & S_IFDIR) { - traverse_hierarchy(filepath, fileop); - continue; - } - /* If it's a regular file, perform the operation. */ - if (st.st_mode & S_IFREG) { - fileop(filepath); - } - } - closedir(dirp); -} - -/* Scan the music initially. */ -void -mfs_scan(char *filepath) -{ - TagLib_File *file; - TagLib_Tag *tag; - char *artist, *album, *genre, *title, *trackno; - const char *extension; - int ret; - unsigned int track, year; - sqlite3_stmt *st; - struct stat fstat; - - file = taglib_file_new(filepath); - /* XXX: errmsg. */ - if (file == NULL) - return; - tag = taglib_file_tag(file); - if (tag == NULL) { - printf("error!\n"); - return; - } - - if (stat(filepath, &fstat) < 0) { - perror("stat"); - return; - } - - /* XXX: The main query code should perhaps be a bit generalized. */ - - /* First insert artist if we have it. */ - do { - artist = taglib_tag_artist(tag); - if (artist == NULL) - break; - /* First find out if it exists. */ - ret = sqlite3_prepare_v2(handle, "SELECT * FROM artist WHERE " - "name=?", -1, &st, NULL); - if (ret != SQLITE_OK) { - warnx("Error preparing statement: %s\n", - sqlite3_errmsg(handle)); - break; - } - sqlite3_bind_text(st, 1, artist, -1, SQLITE_STATIC); - ret = sqlite3_step(st); - sqlite3_finalize(st); - /* Already exists or generic error. */ - if (ret != SQLITE_DONE) - break; - /* Doesn't exist, so we can insert it. */ - ret = sqlite3_prepare_v2(handle, "INSERT INTO artist(name) " - "VALUES(?)", -1, &st, NULL); - if (ret != SQLITE_OK) { - warnx("Error preparing statement: %s\n", - sqlite3_errmsg(handle)); - break; - } - sqlite3_bind_text(st, 1, artist, -1, SQLITE_STATIC); - ret = sqlite3_step(st); - sqlite3_finalize(st); - if (ret != SQLITE_DONE) { - warnx("Error inserting into database: %s\n", - sqlite3_errmsg(handle)); - break; - } - } while (0); - - /* Insert genre if it doesn't exist. */ - do { - genre = taglib_tag_genre(tag); - if (genre == NULL) - break; - /* First find out if it exists. */ - ret = sqlite3_prepare_v2(handle, "SELECT * FROM genre WHERE " - "name=?", -1, &st, NULL); - if (ret != SQLITE_OK) { - warnx("Error preparing statement: %s\n", - sqlite3_errmsg(handle)); - break; - } - sqlite3_bind_text(st, 1, genre, -1, SQLITE_STATIC); - ret = sqlite3_step(st); - sqlite3_finalize(st); - /* Already exists or generic error. */ - if (ret != SQLITE_DONE) - break; - /* Doesn't exist, so we can insert it. */ - ret = sqlite3_prepare_v2(handle, "INSERT INTO genre(name) " - "VALUES(?)", -1, &st, NULL); - if (ret != SQLITE_OK) { - warnx("Error preparing statement: %s\n", - sqlite3_errmsg(handle)); - break; - } - sqlite3_bind_text(st, 1, genre, -1, SQLITE_STATIC); - ret = sqlite3_step(st); - sqlite3_finalize(st); - if (ret != SQLITE_DONE) { - warnx("Error inserting into database: %s\n", - sqlite3_errmsg(handle)); - break; - } - } while (0); - - /* Finally, insert song. */ - do { - title = taglib_tag_title(tag); - album = taglib_tag_album(tag); - track = taglib_tag_track(tag); - year = taglib_tag_year(tag); - extension = strrchr(filepath, (int)'.'); - if (extension == NULL) - extension = ""; - else - /* Drop the '.' */ - extension++; - - if (title == NULL || genre == NULL || artist == NULL || - album == NULL) - break; - - /* First find out if it exists. */ - ret = sqlite3_prepare_v2(handle, "SELECT * FROM song, artist " - " WHERE artist.name=song.artistname AND title=? AND year=?" - " AND song.album=?", - -1, &st, NULL); - if (ret != SQLITE_OK) { - warnx("Error preparing statement: %s\n", - sqlite3_errmsg(handle)); - break; - } - sqlite3_bind_text(st, 1, title, -1, SQLITE_STATIC); - sqlite3_bind_int(st, 2, year); - sqlite3_bind_text(st, 3, album, -1, SQLITE_STATIC); - ret = sqlite3_step(st); - sqlite3_finalize(st); - if (ret == SQLITE_ROW) - /* Already exists. XXX: Update it if mtime mismatches */ - break; - if (ret != SQLITE_DONE) - /* SQL Error. */ - break; - /* Now, finally insert it. */ - ret = sqlite3_prepare_v2(handle, "INSERT INTO song(title, " - "artistname, album, genrename, year, track, filepath, " - "mtime, extension) VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?)", - -1, &st, NULL); - if (ret != SQLITE_OK) { - warnx("Error preparing insert statement: %s\n", - sqlite3_errmsg(handle)); - break; - } - sqlite3_bind_text(st, 1, title, -1, SQLITE_STATIC); - sqlite3_bind_text(st, 2, artist, -1, SQLITE_STATIC); - sqlite3_bind_text(st, 3, album, -1, SQLITE_STATIC); - sqlite3_bind_text(st, 4, genre, -1, SQLITE_STATIC); - sqlite3_bind_int(st, 5, year); - - if (track) { - trackno = malloc(sizeof(char) * 9); - sprintf(trackno, "%02d", track); - sqlite3_bind_text(st, 6, trackno, -1, SQLITE_TRANSIENT); - free(trackno); - } else { - sqlite3_bind_text(st, 6, "", -1, SQLITE_STATIC); - } - - sqlite3_bind_text(st, 7, filepath, -1, SQLITE_STATIC); - sqlite3_bind_int(st, 8, fstat.st_mtime); - sqlite3_bind_text(st, 9, extension, -1, SQLITE_STATIC); - ret = sqlite3_step(st); - sqlite3_finalize(st); - if (ret != SQLITE_DONE) { - warnx("Error inserting into database: %s\n", - sqlite3_errmsg(handle)); - break; - } - } while (0); - taglib_tag_free_strings(); - taglib_file_free(file); -} - -/* - * Create a handle for listing music with a certain query. Allocate the - * resources and return the handle. - */ -struct lookuphandle * -mfs_lookup_start(int field, void *data, lookup_fn_t *fn, const char *query) -{ - struct lookuphandle *lh; - int ret, error; - - lh = malloc(sizeof(*lh)); - if (lh == NULL) - return (NULL); - lh->field = field; - lh->lookup = fn; - lh->priv = data; - /* Open database. */ - error = sqlite3_open(DBNAME, &lh->handle); - if (error) { - warnx("Can't open database: %s\n", sqlite3_errmsg(lh->handle)); - sqlite3_close(lh->handle); - free(lh); - return (NULL); - } - ret = sqlite3_prepare_v2(lh->handle, query, -1, &lh->st, NULL); - if (ret != SQLITE_OK) { - free(lh); - return (NULL); - } - lh->query = query; - lh->count = 1; - return (lh); -} - -/* - * Insert data that should be searched for in the list. The data is assumed to - * be dynamically allocated, and will be free'd when mfs_lookup_finish is called! - */ -void -mfs_lookup_insert(struct lookuphandle *lh, void *data, int type) -{ - char *str; - int val; - - switch (type) { - case LIST_DATATYPE_STRING: - str = (char *)data; - sqlite3_bind_text(lh->st, lh->count++, str, -1, free); - break; - case LIST_DATATYPE_INT: - val = *((int *)data); - sqlite3_bind_int(lh->st, lh->count++, val); - break; - } -} - -/* - * Finish a statement buildup and use the lookup function to operate on the - * returned data. Free the handle when done. - */ -void -mfs_lookup_finish(struct lookuphandle *lh) -{ - char buf[1024]; - const unsigned char *value; - int ret, type, val, finished; - - if (lh == NULL) - return; - ret = sqlite3_step(lh->st); - while (ret == SQLITE_ROW) { - type = sqlite3_column_type(lh->st, lh->field); - switch (type) { - case SQLITE_INTEGER: - val = sqlite3_column_int(lh->st, lh->field); - snprintf(buf, sizeof(buf), "%d", val); - finished = lh->lookup(lh->priv, (const char *)buf); - break; - case SQLITE_TEXT: - value = sqlite3_column_text(lh->st, lh->field); - finished = lh->lookup(lh->priv, (const char *)value); - break; - default: - finished = 0; - break; -// default: -// lh->filler(lh->buf, "UNKNOWN TYPE", NULL, 0); - } - if (finished) - break; - ret = sqlite3_step(lh->st); - } - // XXX: Check for errors too. - sqlite3_finalize(lh->st); - sqlite3_close(lh->handle); - free(lh); -} - -/* - * Build a file_data struct for a path. - * - * data should be a pointer to a file handle - * returns 0 on success - */ -int -mfs_file_data_for_path(const char *path, void *data) { - DEBUG("getting file data for %s\n", path); - struct file_data *fd; - fd = (struct file_data *)data; - struct lookuphandle *lh; - char *artist, *album, *title; - - lh = NULL; - fd->fd = -1; - fd->found = 0; - - /* Open a specific track. */ - if (strncmp(path, "/Tracks", 7) == 0) { - switch (mfs_numtoken(path)) { - case 2: - title = mfs_gettoken(path, 2); - if (title == NULL) - break; - lh = mfs_lookup_start(0, fd, mfs_lookup_open, - "SELECT filepath FROM song " - "WHERE (artistname||' - '||title||'.'||extension) LIKE ?"); - if (lh == NULL) - return (-EIO); - mfs_lookup_insert(lh, title, LIST_DATATYPE_STRING); - break; - default: - return (-ENOENT); - } - } else if (strncmp(path, "/Albums", 7) == 0) { - switch (mfs_numtoken(path)) { - case 3: - album = mfs_gettoken(path, 2); - if (album == NULL) - break; - title = mfs_gettoken(path, 3); - if (title == NULL) - break; - lh = mfs_lookup_start(0, fd, mfs_lookup_open, - "SELECT filepath FROM song " - "WHERE (title||'.'||extension) LIKE ? AND " - "album LIKE ?"); - if (lh == NULL) - return (-EIO); - mfs_lookup_insert(lh, title, LIST_DATATYPE_STRING); - mfs_lookup_insert(lh, album, LIST_DATATYPE_STRING); - break; - default: - return (-ENOENT); - } - } else if (strncmp(path, "/Artists", 8) == 0) { - switch (mfs_numtoken(path)) { - case 4: - artist = mfs_gettoken(path, 2); - album = mfs_gettoken(path, 3); - title = mfs_gettoken(path, 4); - DEBUG("artist(%s) album(%s) title(%s)\n", artist, album, title); - if (!(artist && album && title)) { - break; - } - lh = mfs_lookup_start(0, fd, mfs_lookup_open, - "SELECT filepath FROM song WHERE artistname LIKE ? AND " - "album LIKE ? AND " - "(LTRIM(track||' ')||title||'.'||extension) LIKE ?"); - if (lh == NULL) - return (-EIO); - mfs_lookup_insert(lh, artist, LIST_DATATYPE_STRING); - mfs_lookup_insert(lh, album, LIST_DATATYPE_STRING); - mfs_lookup_insert(lh, title, LIST_DATATYPE_STRING); - break; - default: - return (-ENOENT); - } - } - - if (lh) { - mfs_lookup_finish(lh); - } - - return 0; -} - -/* - * Count number of tokens in pathname. - * XXX: should we strip the first and last? - */ -int -mfs_numtoken(const char *str) -{ - const char *ptr; - int num; - - num = 0; - ptr = str; - if (*ptr == '/') - ptr++; - while ((*ptr != '\0')) { - if (*ptr++ == '/') - num++; - } - if (*(ptr - 1) == '/') - num--; - return (num + 1); -} - -/* - * Extract token number toknum and return it in escaped form. - */ -char * -mfs_gettoken(const char *str, int toknum) -{ - const char *ptr, *start, *end; - char *ret, *dest; - int numescape; - int i; - size_t size; - - start = str; - /* Find the token start. */ - for (i = 0; i < toknum; i++) { - start = strchr(start, '/'); - if (start == NULL) - return (NULL); - start++; - } - /* Find the end pointer. */ - end = strchr(start, '/'); - if (end == NULL) - end = strchr(start, '\0'); - /* - * Count how many bytes extra to take into account because of escaping. - */ - numescape = 0; - ptr = start; - while ((ptr = strchr(ptr, '\'')) != NULL && ptr < end) { - ptr++; - numescape++; - } - size = (end - start) + numescape; - ret = malloc(size + 1); - if (ret == NULL) - return (ret); - ptr = start; - dest = ret; - while (ptr < end) { - if (*ptr == '\'') - *dest++ = '\\'; - *dest++ = *ptr++; - } - ret[size] = '\0'; - return (ret); -} - -/* - * List album given a path. - */ -void -mfs_lookup_album(const char *path, struct filler_data *fd) -{ - struct lookuphandle *lh; - char *album; - - switch (mfs_numtoken(path)) { - case 1: - lh = mfs_lookup_start(0, fd, mfs_lookup_list, - "SELECT DISTINCT album FROM song"); - break; - case 2: - /* So, now we got to find out the artist and list its albums. */ - album = mfs_gettoken(path, 2); - if (album == NULL) - break; - lh = mfs_lookup_start(0, fd, mfs_lookup_list, - "SELECT DISTINCT title||'.'||extension FROM song " - "WHERE album LIKE ?"); - mfs_lookup_insert(lh, album, LIST_DATATYPE_STRING); - break; - } - mfs_lookup_finish(lh); -} - -/* - * List artist given a path. - */ -void -mfs_lookup_artist(const char *path, struct filler_data *fd) -{ - struct lookuphandle *lh; - char *name, *album; - - switch (mfs_numtoken(path)) { - case 1: - lh = mfs_lookup_start(0, fd, mfs_lookup_list, - "SELECT name FROM artist"); - break; - case 2: - /* So, now we got to find out the artist and list its albums. */ - name = mfs_gettoken(path, 2); - if (name == NULL) - break; - lh = mfs_lookup_start(0, fd, mfs_lookup_list, - "SELECT DISTINCT album FROM song, " - "artist WHERE song.artistname = artist.name AND artist.name" - " LIKE ?"); - mfs_lookup_insert(lh, name, LIST_DATATYPE_STRING); - break; - case 3: - /* List songs in an album. */ - name = mfs_gettoken(path, 2); - if (name == NULL) - break; - album = mfs_gettoken(path, 3); - if (album == NULL) - break; - lh = mfs_lookup_start(0, fd, mfs_lookup_list, - "SELECT LTRIM(track||' ')||title||'.'||extension " - "FROM song, artist " - "WHERE song.artistname = artist.name AND artist.name " - "LIKE ? AND song.album LIKE ?"); - mfs_lookup_insert(lh, name, LIST_DATATYPE_STRING); - mfs_lookup_insert(lh, album, LIST_DATATYPE_STRING); - break; - } - mfs_lookup_finish(lh); -} - -/* - * Looks up tracks given a genre, or all genres. - */ -void -mfs_lookup_genre(const char *path, struct filler_data *fd) -{ - struct lookuphandle *lh; - char *genre, *album; - - switch (mfs_numtoken(path)) { - case 1: - lh = mfs_lookup_start(0, fd, mfs_lookup_list, - "SELECT name FROM genre"); - break; - case 2: - genre = mfs_gettoken(path, 2); - if (genre == NULL) - break; - lh = mfs_lookup_start(0, fd, mfs_lookup_list, - "SELECT DISTINCT album FROM song, " - "genre WHERE song.genrename = genre.name AND genre.name " - "LIKE ?"); - mfs_lookup_insert(lh, genre, LIST_DATATYPE_STRING); - break; - case 3: - genre = mfs_gettoken(path, 2); - if (genre == NULL) - break; - album = mfs_gettoken(path, 3); - if (album == NULL) - break; - lh = mfs_lookup_start(0, fd, mfs_lookup_list, - "SELECT title||'.'||extension FROM song, genre WHERE" - " song.genrename = genre.name AND genre.name LIKE ? " - " AND song.album LIKE ?"); - mfs_lookup_insert(lh, genre, LIST_DATATYPE_STRING); - mfs_lookup_insert(lh, album, LIST_DATATYPE_STRING); - break; - } - mfs_lookup_finish(lh); -} - -/* - * Lookup function for filling given data into a filler. - */ -int -mfs_lookup_list(void *data, const char *str) -{ - struct filler_data *fd; - - fd = (struct filler_data *)data; - fd->filler(fd->buf, str, NULL, 0); - return (0); -} - -/* - * Lookup a file and open it if found. - */ -int -mfs_lookup_open(void *data, const char *str) -{ - struct file_data *fd; - - fd = (struct file_data *)data; - fd->fd = open(str, O_RDONLY); - fd->found = 1; - return (1); -} - -/* - * Stat a file. - * XXX: watch out for duplicates, we might stat more than once. - */ -int -mfs_lookup_stat(void *data, const char *str) -{ - struct stat *st; - - st = (struct stat *)data; - if (stat(str, st) < 0) - return (0); - return (1); -} - -/* - * Guess on a filetype for a path. - * - * Examples: - * - * - /Artists/Whoever should be a directory, since it's an - * Album. - * - * - /Tracks/Whatever should be a file, since Whatever is a song - * - * Note: This should only be used for paths inside the musicfs - * directories (Artists, Tracks, ...) - */ -enum mfs_filetype -mfs_get_filetype(const char *path) { - int tokens = mfs_numtoken(path); - - if (strncmp(path, "/Genres", 7) == 0 || - strncmp(path, "/Artists", 8) == 0) { - switch (tokens) { - case 1: - case 2: - case 3: - return (MFS_DIRECTORY); - case 4: - return (MFS_FILE); - default: - return (MFS_NOTFOUND); - } - } else if (strncmp(path, "/Albums", 7) == 0) { - switch (tokens) { - case 1: - case 2: - return (MFS_DIRECTORY); - case 3: - return (MFS_FILE); - default: - return (MFS_NOTFOUND); - } - } else if (strncmp(path, "/Tracks", 7) == 0) { - switch (tokens) { - case 1: - return (MFS_DIRECTORY); - case 2: - return (MFS_FILE); - default: - return (MFS_NOTFOUND); - } - } else { - return (MFS_NOTFOUND); - } -} diff --git a/src/mp3_vnops.c b/src/mp3_vnops.c deleted file mode 100755 index 92a314c..0000000 --- a/src/mp3_vnops.c +++ /dev/null @@ -1,214 +0,0 @@ -/* -*- Mode: C; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*- */ -#define FUSE_USE_VERSION 26 -#include -#include -#include -#include -#include - -#include -#include -#include -#include -#include -#include - -#include -#include -#include - -char musicpath[MAXPATHLEN]; // = "/home/lulf/dev/musicfs/music"; -char *logpath = "/home/lulf/dev/musicfs/musicfs.log"; - -static int mfs_getattr (const char *path, struct stat *stbuf) -{ - struct file_data fd; - fd.fd = -1; - fd.found = 0; - - int status = 0; - memset (stbuf, 0, sizeof (struct stat)); - - if (strcmp (path, "/") == 0) { - stbuf->st_mode = S_IFDIR | 0755; - stbuf->st_nlink = 2; - return 0; - } - - enum mfs_filetype type = mfs_get_filetype(path); - switch (type) { - case MFS_DIRECTORY: - stbuf->st_mode = S_IFDIR | 0444; - stbuf->st_nlink = 1; - stbuf->st_size = 12; - return 0; - - case MFS_FILE: - status = mfs_file_data_for_path(path, &fd); - if (status != 0) - return (status); - - if (!fd.found) - return (-ENOENT); - if (fd.fd < 0) - return (-EIO); - - if (fstat(fd.fd, stbuf) == 0) - return (0); - else - return (-ENOENT); - - case MFS_NOTFOUND: - default: - return -ENOENT; - } -} - - -static int mfs_readdir (const char *path, void *buf, fuse_fill_dir_t filler, - off_t offset, struct fuse_file_info *fi) -{ - struct filler_data fd; - struct lookuphandle *lh; - - filler (buf, ".", NULL, 0); - filler (buf, "..", NULL, 0); - fd.buf = buf; - fd.filler = filler; - - if (!strcmp(path, "/")) { - filler(buf, "Artists", NULL, 0); - filler(buf, "Genres", NULL, 0); - filler(buf, "Tracks", NULL, 0); - filler(buf, "Albums", NULL, 0); - return (0); - } - - /* - * 1. Parse the path. - * 2. Find the mp3s that matches the tags given from the path. - * 3. Return the list of those mp3s. - */ - if (strncmp(path, "/Artists", 8) == 0) { - mfs_lookup_artist(path, &fd); - return (0); - } else if (strncmp(path, "/Genres", 7) == 0) { - mfs_lookup_genre(path, &fd); - return (0); - } else if (strcmp(path, "/Tracks") == 0) { - lh = mfs_lookup_start(0, &fd, mfs_lookup_list, - "SELECT DISTINCT artistname||' - '||title||'.'||extension " - "FROM song"); - mfs_lookup_finish(lh); - return (0); - } else if (strncmp(path, "/Albums", 7) == 0) { - mfs_lookup_album(path, &fd); - return (0); - } - - return (-ENOENT); -} - -static int mfs_open (const char *path, struct fuse_file_info *fi) -{ - struct file_data fd; - fd.fd = -1; - fd.found = 0; - - int status = mfs_file_data_for_path(path, &fd); - if (status != 0) - return (status); - - if (!fd.found) - return (-ENOENT); - if (fd.fd < 0) - return (-EIO); - close(fd.fd); - - return (0); - - /* - * 1. Have a lookup cache for names?. - * Parse Genre, Album, Artist etc. - * 2. Find MP3s that match. XXX what do we do with duplicates? just - * return the first match? - * 3. Put the mnode of the mp3 in our cache. - * 4. Signal in mnode that the mp3 is being read? - */ -} - -static int mfs_read (const char *path, char *buf, size_t size, off_t offset, - struct fuse_file_info *fi) -{ - struct file_data fd; - fd.fd = -1; - fd.found = 0; - size_t bytes; - - int status = mfs_file_data_for_path(path, &fd); - if (status != 0) - return (status); - - if (!fd.found) - return (-ENOENT); - if (fd.fd < 0) - return (-EIO); - lseek(fd.fd, offset, SEEK_CUR); - bytes = read(fd.fd, buf, size); - close(fd.fd); - return (bytes); - - /* - * 1. Find the mnode given the path. If not in cache, read through mp3 - * list to find it. - * 2. Read the offset of the mp3 and return the data. - */ -} - -static struct fuse_operations mfs_ops = { - .getattr = mfs_getattr, - .readdir = mfs_readdir, - .open = mfs_open, - .read = mfs_read, -}; - -static int musicfs_opt_proc (void *data, const char *arg, int key, - struct fuse_args *outargs) -{ - static int musicpath_set = 0; - - if (key == FUSE_OPT_KEY_NONOPT && !musicpath_set) { - /* The source directory isn't already set, let's do it */ - strcpy(musicpath, arg); - musicpath_set = 1; - return (0); - } - return (1); -} - -int -mfs_run(int argc, char **argv) -{ - int ret; - /* - * XXX: Build index of mp3's. - */ - - /* Update tables. */ - if (argc < 2) { - fprintf(stderr, "Usage: %s \n", argv[0]); - return (-1); - } - - struct fuse_args args = FUSE_ARGS_INIT (argc, argv); - - if (fuse_opt_parse(&args, NULL, NULL, musicfs_opt_proc) != 0) - exit (1); - - DEBUG("musicpath: %s\n", musicpath); - mfs_initscan(musicpath); - - ret = 0; - ret = fuse_main(args.argc, args.argv, &mfs_ops, NULL); - return (ret); -} diff --git a/src/mp3fs.c b/src/mp3fs.c index 8690346..4811cb9 100755 --- a/src/mp3fs.c +++ b/src/mp3fs.c @@ -1,4 +1,25 @@ /* -*- Mode: C; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*- */ + +/* + * Musicfs is a FUSE module implementing a media filesystem in userland. + * Copyright (C) 2008 Ulf Lilleengen, Kjetil Ørbekk + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License version + * 2, as published by the Free Software Foundation. + * + * 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. + * + * A copy of the license can typically be found in COPYING + */ + #define FUSE_USE_VERSION 26 #include -- cgit v1.2.3