2002-05-03 15:36:52 +00:00
|
|
|
/***************************************************************************
|
|
|
|
* __________ __ ___.
|
|
|
|
* Open \______ \ ____ ____ | | _\_ |__ _______ ___
|
|
|
|
* Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
|
|
|
|
* Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
|
|
|
|
* Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
|
|
|
|
* \/ \/ \/ \/ \/
|
2007-07-20 17:06:55 +00:00
|
|
|
* $Id: dir.c 13741 2007-06-30 02:08:27Z jethead71 $
|
2002-05-03 15:36:52 +00:00
|
|
|
*
|
2008-05-05 10:32:46 +00:00
|
|
|
* Copyright (C) 2002 by Björn Stenberg
|
2002-05-03 15:36:52 +00:00
|
|
|
*
|
|
|
|
* All files in this archive are subject to the GNU General Public License.
|
|
|
|
* See the file COPYING in the source tree root for full license agreement.
|
|
|
|
*
|
|
|
|
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
|
|
|
|
* KIND, either express or implied.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
#include <stdio.h>
|
2002-05-27 09:13:24 +00:00
|
|
|
#include <errno.h>
|
2002-05-03 15:36:52 +00:00
|
|
|
#include <string.h>
|
2002-05-13 12:29:34 +00:00
|
|
|
#include <stdbool.h>
|
2008-04-28 16:18:04 +00:00
|
|
|
#include <stdlib.h>
|
2002-05-03 15:36:52 +00:00
|
|
|
#include "fat.h"
|
|
|
|
#include "dir.h"
|
|
|
|
#include "debug.h"
|
|
|
|
|
2002-05-27 09:13:24 +00:00
|
|
|
#define MAX_OPEN_DIRS 8
|
|
|
|
|
2007-07-20 17:06:55 +00:00
|
|
|
static DIR_UNCACHED opendirs[MAX_OPEN_DIRS];
|
2002-05-03 15:36:52 +00:00
|
|
|
|
2004-12-28 22:16:07 +00:00
|
|
|
#ifdef HAVE_MULTIVOLUME
|
|
|
|
|
|
|
|
/* returns on which volume this is, and copies the reduced name
|
|
|
|
(sortof a preprocessor for volume-decorated pathnames) */
|
2008-03-11 19:39:26 +00:00
|
|
|
int strip_volume(const char* name, char* namecopy)
|
2004-12-28 22:16:07 +00:00
|
|
|
{
|
|
|
|
int volume = 0;
|
2006-06-30 05:35:11 +00:00
|
|
|
const char *temp = name;
|
|
|
|
|
2006-06-30 05:43:08 +00:00
|
|
|
while (*temp == '/') /* skip all leading slashes */
|
2006-06-30 05:35:11 +00:00
|
|
|
++temp;
|
|
|
|
|
2008-03-11 19:39:26 +00:00
|
|
|
if (*temp && !strncmp(temp, VOL_NAMES, VOL_ENUM_POS))
|
2004-12-28 22:16:07 +00:00
|
|
|
{
|
2006-06-30 05:35:11 +00:00
|
|
|
temp += VOL_ENUM_POS; /* behind special name */
|
|
|
|
volume = atoi(temp); /* number is following */
|
2004-12-28 22:16:07 +00:00
|
|
|
temp = strchr(temp, '/'); /* search for slash behind */
|
|
|
|
if (temp != NULL)
|
2006-06-30 05:35:11 +00:00
|
|
|
name = temp; /* use the part behind the volume */
|
2004-12-28 22:16:07 +00:00
|
|
|
else
|
2006-06-30 05:35:11 +00:00
|
|
|
name = "/"; /* else this must be the root dir */
|
2004-12-28 22:16:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
strncpy(namecopy, name, MAX_PATH);
|
|
|
|
namecopy[MAX_PATH-1] = '\0';
|
|
|
|
|
|
|
|
return volume;
|
|
|
|
}
|
|
|
|
#endif /* #ifdef HAVE_MULTIVOLUME */
|
|
|
|
|
|
|
|
|
2005-01-28 21:32:16 +00:00
|
|
|
#ifdef HAVE_HOTSWAP
|
|
|
|
// release all dir handles on a given volume "by force", to avoid leaks
|
|
|
|
int release_dirs(int volume)
|
|
|
|
{
|
2007-07-20 17:06:55 +00:00
|
|
|
DIR_UNCACHED* pdir = opendirs;
|
2005-01-28 21:32:16 +00:00
|
|
|
int dd;
|
|
|
|
int closed = 0;
|
|
|
|
for ( dd=0; dd<MAX_OPEN_DIRS; dd++, pdir++)
|
|
|
|
{
|
|
|
|
if (pdir->fatdir.file.volume == volume)
|
|
|
|
{
|
|
|
|
pdir->busy = false; /* mark as available, no further action */
|
|
|
|
closed++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return closed; /* return how many we did */
|
|
|
|
}
|
|
|
|
#endif /* #ifdef HAVE_HOTSWAP */
|
|
|
|
|
2007-07-20 17:06:55 +00:00
|
|
|
DIR_UNCACHED* opendir_uncached(const char* name)
|
2002-05-03 15:36:52 +00:00
|
|
|
{
|
2002-07-15 22:58:28 +00:00
|
|
|
char namecopy[MAX_PATH];
|
2002-05-03 15:36:52 +00:00
|
|
|
char* part;
|
2002-05-24 09:57:33 +00:00
|
|
|
char* end;
|
2002-05-03 15:36:52 +00:00
|
|
|
struct fat_direntry entry;
|
2002-05-27 09:13:24 +00:00
|
|
|
int dd;
|
2007-07-20 17:06:55 +00:00
|
|
|
DIR_UNCACHED* pdir = opendirs;
|
2004-12-28 22:16:07 +00:00
|
|
|
#ifdef HAVE_MULTIVOLUME
|
|
|
|
int volume;
|
|
|
|
#endif
|
2002-05-27 09:13:24 +00:00
|
|
|
|
2005-01-03 20:23:42 +00:00
|
|
|
if ( name[0] != '/' ) {
|
|
|
|
DEBUGF("Only absolute paths supported right now\n");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2002-05-27 09:13:24 +00:00
|
|
|
/* find a free dir descriptor */
|
2005-01-03 20:23:42 +00:00
|
|
|
for ( dd=0; dd<MAX_OPEN_DIRS; dd++, pdir++)
|
|
|
|
if ( !pdir->busy )
|
2002-05-27 09:13:24 +00:00
|
|
|
break;
|
2002-05-03 15:36:52 +00:00
|
|
|
|
2002-05-27 09:13:24 +00:00
|
|
|
if ( dd == MAX_OPEN_DIRS ) {
|
|
|
|
DEBUGF("Too many dirs open\n");
|
|
|
|
errno = EMFILE;
|
2002-05-03 15:36:52 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2005-01-03 20:23:42 +00:00
|
|
|
pdir->busy = true;
|
2002-05-03 15:36:52 +00:00
|
|
|
|
2004-12-28 22:16:07 +00:00
|
|
|
#ifdef HAVE_MULTIVOLUME
|
|
|
|
/* try to extract a heading volume name, if present */
|
|
|
|
volume = strip_volume(name, namecopy);
|
2005-01-03 20:23:42 +00:00
|
|
|
pdir->volumecounter = 0;
|
2004-12-28 22:16:07 +00:00
|
|
|
#else
|
|
|
|
strncpy(namecopy,name,sizeof(namecopy)); /* just copy */
|
|
|
|
namecopy[sizeof(namecopy)-1] = '\0';
|
|
|
|
#endif
|
|
|
|
|
2005-01-03 20:23:42 +00:00
|
|
|
if ( fat_opendir(IF_MV2(volume,) &pdir->fatdir, 0, NULL) < 0 ) {
|
2002-05-03 15:36:52 +00:00
|
|
|
DEBUGF("Failed opening root dir\n");
|
2005-01-03 20:23:42 +00:00
|
|
|
pdir->busy = false;
|
2002-05-03 15:36:52 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2002-05-24 09:57:33 +00:00
|
|
|
for ( part = strtok_r(namecopy, "/", &end); part;
|
|
|
|
part = strtok_r(NULL, "/", &end)) {
|
2002-05-03 15:36:52 +00:00
|
|
|
/* scan dir for name */
|
|
|
|
while (1) {
|
2005-01-03 20:23:42 +00:00
|
|
|
if ((fat_getnext(&pdir->fatdir,&entry) < 0) ||
|
2002-05-27 09:13:24 +00:00
|
|
|
(!entry.name[0])) {
|
2005-01-03 20:23:42 +00:00
|
|
|
pdir->busy = false;
|
2002-05-03 15:36:52 +00:00
|
|
|
return NULL;
|
2002-05-27 09:13:24 +00:00
|
|
|
}
|
2002-05-03 15:36:52 +00:00
|
|
|
if ( (entry.attr & FAT_ATTR_DIRECTORY) &&
|
2002-08-15 11:26:12 +00:00
|
|
|
(!strcasecmp(part, entry.name)) ) {
|
2005-01-03 20:23:42 +00:00
|
|
|
pdir->parent_dir = pdir->fatdir;
|
2004-12-28 22:16:07 +00:00
|
|
|
if ( fat_opendir(IF_MV2(volume,)
|
2005-01-03 20:23:42 +00:00
|
|
|
&pdir->fatdir,
|
2004-04-16 08:58:29 +00:00
|
|
|
entry.firstcluster,
|
2005-01-03 20:23:42 +00:00
|
|
|
&pdir->parent_dir) < 0 ) {
|
2005-01-23 23:29:35 +00:00
|
|
|
DEBUGF("Failed opening dir '%s' (%ld)\n",
|
2002-05-03 15:36:52 +00:00
|
|
|
part, entry.firstcluster);
|
2005-01-03 20:23:42 +00:00
|
|
|
pdir->busy = false;
|
2002-05-03 15:36:52 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
2004-12-28 22:16:07 +00:00
|
|
|
#ifdef HAVE_MULTIVOLUME
|
2005-01-03 20:23:42 +00:00
|
|
|
pdir->volumecounter = -1; /* n.a. to subdirs */
|
2004-12-28 22:16:07 +00:00
|
|
|
#endif
|
2002-05-03 15:36:52 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-01-03 20:23:42 +00:00
|
|
|
return pdir;
|
2002-05-03 15:36:52 +00:00
|
|
|
}
|
|
|
|
|
2007-07-20 17:06:55 +00:00
|
|
|
int closedir_uncached(DIR_UNCACHED* dir)
|
2002-05-03 15:36:52 +00:00
|
|
|
{
|
2002-05-27 09:13:24 +00:00
|
|
|
dir->busy=false;
|
2002-05-03 15:36:52 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2007-07-20 17:06:55 +00:00
|
|
|
struct dirent_uncached* readdir_uncached(DIR_UNCACHED* dir)
|
2002-05-03 15:36:52 +00:00
|
|
|
{
|
|
|
|
struct fat_direntry entry;
|
2007-07-20 17:06:55 +00:00
|
|
|
struct dirent_uncached* theent = &(dir->theent);
|
2005-01-28 21:32:16 +00:00
|
|
|
|
|
|
|
if (!dir->busy)
|
|
|
|
return NULL;
|
|
|
|
|
2004-12-28 22:16:07 +00:00
|
|
|
#ifdef HAVE_MULTIVOLUME
|
|
|
|
/* Volumes (secondary file systems) get inserted into the root directory
|
|
|
|
of the first volume, since we have no separate top level. */
|
|
|
|
if (dir->volumecounter >= 0 /* on a root dir */
|
|
|
|
&& dir->volumecounter < NUM_VOLUMES /* in range */
|
|
|
|
&& dir->fatdir.file.volume == 0) /* at volume 0 */
|
|
|
|
{ /* fake special directories, which don't really exist, but
|
2007-07-20 17:06:55 +00:00
|
|
|
will get redirected upon opendir_uncached() */
|
2004-12-28 22:16:07 +00:00
|
|
|
while (++dir->volumecounter < NUM_VOLUMES)
|
|
|
|
{
|
|
|
|
if (fat_ismounted(dir->volumecounter))
|
|
|
|
{
|
|
|
|
memset(theent, 0, sizeof(*theent));
|
|
|
|
theent->attribute = FAT_ATTR_DIRECTORY | FAT_ATTR_VOLUME;
|
|
|
|
snprintf(theent->d_name, sizeof(theent->d_name),
|
2008-03-11 19:39:26 +00:00
|
|
|
VOL_NAMES, dir->volumecounter);
|
2004-12-28 22:16:07 +00:00
|
|
|
return theent;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
/* normal directory entry fetching follows here */
|
2002-05-03 15:36:52 +00:00
|
|
|
if (fat_getnext(&(dir->fatdir),&entry) < 0)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
if ( !entry.name[0] )
|
2002-10-20 22:50:58 +00:00
|
|
|
return NULL;
|
2002-05-03 15:36:52 +00:00
|
|
|
|
2002-05-27 09:13:24 +00:00
|
|
|
strncpy(theent->d_name, entry.name, sizeof( theent->d_name ) );
|
|
|
|
theent->attribute = entry.attr;
|
|
|
|
theent->size = entry.filesize;
|
|
|
|
theent->startcluster = entry.firstcluster;
|
2004-06-20 16:34:29 +00:00
|
|
|
theent->wrtdate = entry.wrtdate;
|
|
|
|
theent->wrttime = entry.wrttime;
|
2002-05-03 15:36:52 +00:00
|
|
|
|
2002-05-27 09:13:24 +00:00
|
|
|
return theent;
|
2002-05-03 15:36:52 +00:00
|
|
|
}
|
2004-01-15 14:30:59 +00:00
|
|
|
|
2007-07-20 17:06:55 +00:00
|
|
|
int mkdir_uncached(const char *name)
|
2004-01-15 14:30:59 +00:00
|
|
|
{
|
2007-07-20 17:06:55 +00:00
|
|
|
DIR_UNCACHED *dir;
|
2004-01-15 14:30:59 +00:00
|
|
|
char namecopy[MAX_PATH];
|
|
|
|
char* end;
|
|
|
|
char *basename;
|
|
|
|
char *parent;
|
2007-07-20 17:06:55 +00:00
|
|
|
struct dirent_uncached *entry;
|
2004-01-15 14:30:59 +00:00
|
|
|
struct fat_dir newdir;
|
|
|
|
int rc;
|
2004-01-21 14:58:40 +00:00
|
|
|
|
2004-01-15 14:30:59 +00:00
|
|
|
if ( name[0] != '/' ) {
|
2004-02-11 14:37:16 +00:00
|
|
|
DEBUGF("mkdir: Only absolute paths supported right now\n");
|
2004-01-15 14:30:59 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
strncpy(namecopy,name,sizeof(namecopy));
|
|
|
|
namecopy[sizeof(namecopy)-1] = 0;
|
|
|
|
|
|
|
|
/* Split the base name and the path */
|
|
|
|
end = strrchr(namecopy, '/');
|
|
|
|
*end = 0;
|
|
|
|
basename = end+1;
|
|
|
|
|
|
|
|
if(namecopy == end) /* Root dir? */
|
|
|
|
parent = "/";
|
|
|
|
else
|
|
|
|
parent = namecopy;
|
|
|
|
|
|
|
|
DEBUGF("mkdir: parent: %s, name: %s\n", parent, basename);
|
|
|
|
|
2007-07-20 17:06:55 +00:00
|
|
|
dir = opendir_uncached(parent);
|
2004-01-15 14:30:59 +00:00
|
|
|
|
|
|
|
if(!dir) {
|
|
|
|
DEBUGF("mkdir: can't open parent dir\n");
|
|
|
|
return -2;
|
|
|
|
}
|
|
|
|
|
2004-02-11 14:37:16 +00:00
|
|
|
if(basename[0] == 0) {
|
|
|
|
DEBUGF("mkdir: Empty dir name\n");
|
|
|
|
errno = EINVAL;
|
|
|
|
return -3;
|
|
|
|
}
|
|
|
|
|
2004-01-15 14:30:59 +00:00
|
|
|
/* Now check if the name already exists */
|
2007-07-20 17:06:55 +00:00
|
|
|
while ((entry = readdir_uncached(dir))) {
|
2004-01-15 14:30:59 +00:00
|
|
|
if ( !strcasecmp(basename, entry->d_name) ) {
|
|
|
|
DEBUGF("mkdir error: file exists\n");
|
|
|
|
errno = EEXIST;
|
2007-07-20 17:06:55 +00:00
|
|
|
closedir_uncached(dir);
|
2004-02-11 14:37:16 +00:00
|
|
|
return - 4;
|
2004-01-15 14:30:59 +00:00
|
|
|
}
|
|
|
|
}
|
2004-01-15 14:34:54 +00:00
|
|
|
|
2004-01-15 15:56:19 +00:00
|
|
|
memset(&newdir, sizeof(struct fat_dir), 0);
|
2004-01-15 14:30:59 +00:00
|
|
|
|
|
|
|
rc = fat_create_dir(basename, &newdir, &(dir->fatdir));
|
2007-07-20 17:06:55 +00:00
|
|
|
closedir_uncached(dir);
|
2004-01-15 15:56:19 +00:00
|
|
|
|
2004-01-15 14:30:59 +00:00
|
|
|
return rc;
|
|
|
|
}
|
2004-04-16 08:58:29 +00:00
|
|
|
|
2007-07-20 17:06:55 +00:00
|
|
|
int rmdir_uncached(const char* name)
|
2004-04-16 08:58:29 +00:00
|
|
|
{
|
|
|
|
int rc;
|
2007-07-20 17:06:55 +00:00
|
|
|
DIR_UNCACHED* dir;
|
|
|
|
struct dirent_uncached* entry;
|
2004-04-16 08:58:29 +00:00
|
|
|
|
2007-07-20 17:06:55 +00:00
|
|
|
dir = opendir_uncached(name);
|
2004-04-16 08:58:29 +00:00
|
|
|
if (!dir)
|
|
|
|
{
|
|
|
|
errno = ENOENT; /* open error */
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* check if the directory is empty */
|
2007-07-20 17:06:55 +00:00
|
|
|
while ((entry = readdir_uncached(dir)))
|
2004-04-16 08:58:29 +00:00
|
|
|
{
|
|
|
|
if (strcmp(entry->d_name, ".") &&
|
|
|
|
strcmp(entry->d_name, ".."))
|
|
|
|
{
|
|
|
|
DEBUGF("rmdir error: not empty\n");
|
|
|
|
errno = ENOTEMPTY;
|
2007-07-20 17:06:55 +00:00
|
|
|
closedir_uncached(dir);
|
2004-04-16 08:58:29 +00:00
|
|
|
return -2;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
rc = fat_remove(&(dir->fatdir.file));
|
|
|
|
if ( rc < 0 ) {
|
|
|
|
DEBUGF("Failed removing dir: %d\n", rc);
|
|
|
|
errno = EIO;
|
|
|
|
rc = rc * 10 - 3;
|
|
|
|
}
|
|
|
|
|
2007-07-20 17:06:55 +00:00
|
|
|
closedir_uncached(dir);
|
2004-04-16 08:58:29 +00:00
|
|
|
return rc;
|
|
|
|
}
|