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
|
|
|
*
|
2008-06-28 18:10:04 +00:00
|
|
|
* 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.
|
2002-05-03 15:36:52 +00:00
|
|
|
*
|
|
|
|
* 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"
|
2009-11-26 22:34:08 +00:00
|
|
|
#include "filefuncs.h"
|
2002-05-03 15:36:52 +00:00
|
|
|
|
2011-02-02 17:43:32 +00:00
|
|
|
#if (MEMORYSIZE > 8)
|
2008-08-10 13:22:31 +00:00
|
|
|
#define MAX_OPEN_DIRS 12
|
2008-08-10 12:46:15 +00:00
|
|
|
#else
|
2002-05-27 09:13:24 +00:00
|
|
|
#define MAX_OPEN_DIRS 8
|
2008-08-10 12:46:15 +00:00
|
|
|
#endif
|
2002-05-27 09:13:24 +00:00
|
|
|
|
2007-07-20 17:06:55 +00:00
|
|
|
static DIR_UNCACHED opendirs[MAX_OPEN_DIRS];
|
2002-05-03 15:36:52 +00:00
|
|
|
|
2005-01-28 21:32:16 +00:00
|
|
|
// 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++)
|
|
|
|
{
|
2009-11-26 16:57:30 +00:00
|
|
|
#ifdef HAVE_MULTIVOLUME
|
2005-01-28 21:32:16 +00:00
|
|
|
if (pdir->fatdir.file.volume == volume)
|
2009-11-26 16:57:30 +00:00
|
|
|
#else
|
|
|
|
(void)volume;
|
|
|
|
#endif
|
2005-01-28 21:32:16 +00:00
|
|
|
{
|
|
|
|
pdir->busy = false; /* mark as available, no further action */
|
|
|
|
closed++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return closed; /* return how many we did */
|
|
|
|
}
|
|
|
|
|
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
|
2009-07-14 13:57:45 +00:00
|
|
|
strlcpy(namecopy, name, sizeof(namecopy)); /* just copy */
|
2004-12-28 22:16:07 +00:00
|
|
|
#endif
|
|
|
|
|
2013-08-17 16:18:22 +00:00
|
|
|
if ( fat_opendir(IF_MV(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)) ) {
|
2010-03-15 12:32:04 +00:00
|
|
|
/* In reality, the parent_dir parameter of fat_opendir seems
|
2010-03-10 08:56:27 +00:00
|
|
|
* useless because it's sole purpose it to have a way to
|
|
|
|
* update the file metadata, but here we are only reading
|
|
|
|
* a directory so there's no need for that kind of stuff.
|
2010-03-15 12:32:04 +00:00
|
|
|
* However, the rmdir_uncached function uses a ugly hack to
|
|
|
|
* avoid opening a directory twice when deleting it and thus
|
|
|
|
* needs those information. That's why we pass pdir->fatdir both
|
|
|
|
* as the parent directory and the resulting one (this is safe,
|
|
|
|
* in doubt, check fat_open(dir) code) which will allow this kind of
|
|
|
|
* (ugly) things */
|
2013-08-17 16:18:22 +00:00
|
|
|
if ( fat_opendir(IF_MV(volume,)
|
2005-01-03 20:23:42 +00:00
|
|
|
&pdir->fatdir,
|
2004-04-16 08:58:29 +00:00
|
|
|
entry.firstcluster,
|
2010-03-15 12:32:04 +00:00
|
|
|
&pdir->fatdir) < 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));
|
2010-09-01 21:29:34 +00:00
|
|
|
theent->info.attribute = FAT_ATTR_DIRECTORY | FAT_ATTR_VOLUME;
|
2004-12-28 22:16:07 +00:00
|
|
|
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] )
|
2010-01-03 11:31:14 +00:00
|
|
|
return NULL;
|
2002-05-03 15:36:52 +00:00
|
|
|
|
2009-07-14 13:57:45 +00:00
|
|
|
strlcpy(theent->d_name, entry.name, sizeof(theent->d_name));
|
2010-09-01 21:29:34 +00:00
|
|
|
theent->info.attribute = entry.attr;
|
|
|
|
theent->info.wrtdate = entry.wrtdate;
|
|
|
|
theent->info.wrttime = entry.wrttime;
|
|
|
|
theent->info.size = entry.filesize;
|
2002-05-27 09:13:24 +00:00
|
|
|
theent->startcluster = entry.firstcluster;
|
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;
|
2010-11-13 15:41:18 +00:00
|
|
|
int dd;
|
|
|
|
DIR_UNCACHED* pdir = opendirs;
|
|
|
|
struct fat_dir *newdir;
|
2004-01-15 14:30:59 +00:00
|
|
|
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;
|
|
|
|
}
|
2010-11-13 15:41:18 +00:00
|
|
|
/* find a free dir descriptor */
|
|
|
|
for ( dd=0; dd<MAX_OPEN_DIRS; dd++, pdir++)
|
|
|
|
if ( !pdir->busy )
|
|
|
|
break;
|
|
|
|
|
|
|
|
if ( dd == MAX_OPEN_DIRS ) {
|
|
|
|
DEBUGF("Too many dirs open\n");
|
|
|
|
errno = EMFILE;
|
|
|
|
return -5;
|
|
|
|
}
|
|
|
|
|
|
|
|
pdir->busy = true;
|
|
|
|
newdir = &pdir->fatdir;
|
2004-01-15 14:30:59 +00:00
|
|
|
|
2009-07-14 13:57:45 +00:00
|
|
|
strlcpy(namecopy, name, sizeof(namecopy));
|
2004-01-15 14:30:59 +00:00
|
|
|
|
|
|
|
/* 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");
|
2010-11-13 15:41:18 +00:00
|
|
|
pdir->busy = false;
|
2004-01-15 14:30:59 +00:00
|
|
|
return -2;
|
|
|
|
}
|
|
|
|
|
2004-02-11 14:37:16 +00:00
|
|
|
if(basename[0] == 0) {
|
|
|
|
DEBUGF("mkdir: Empty dir name\n");
|
2010-11-13 15:41:18 +00:00
|
|
|
pdir->busy = false;
|
2004-02-11 14:37:16 +00:00
|
|
|
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);
|
2010-11-13 15:41:18 +00:00
|
|
|
pdir->busy = false;
|
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
|
|
|
|
2010-11-13 15:41:18 +00:00
|
|
|
memset(newdir, 0, sizeof(struct fat_dir));
|
2004-01-15 14:30:59 +00:00
|
|
|
|
2010-11-13 15:41:18 +00:00
|
|
|
rc = fat_create_dir(basename, newdir, &(dir->fatdir));
|
2007-07-20 17:06:55 +00:00
|
|
|
closedir_uncached(dir);
|
2010-11-13 15:41:18 +00:00
|
|
|
pdir->busy = false;
|
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;
|
|
|
|
}
|