2002-05-03 15:36:52 +00:00
|
|
|
|
/***************************************************************************
|
|
|
|
|
* __________ __ ___.
|
|
|
|
|
* Open \______ \ ____ ____ | | _\_ |__ _______ ___
|
|
|
|
|
* Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
|
|
|
|
|
* Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
|
|
|
|
|
* Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
|
|
|
|
|
* \/ \/ \/ \/ \/
|
|
|
|
|
* $Id$
|
|
|
|
|
*
|
|
|
|
|
* Copyright (C) 2002 by Bj<EFBFBD>rn Stenberg
|
|
|
|
|
*
|
|
|
|
|
* 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>
|
2002-05-03 15:36:52 +00:00
|
|
|
|
#include "fat.h"
|
|
|
|
|
#include "dir.h"
|
|
|
|
|
#include "debug.h"
|
2004-12-28 22:16:07 +00:00
|
|
|
|
#include "atoi.h"
|
2002-05-03 15:36:52 +00:00
|
|
|
|
|
2002-05-27 09:13:24 +00:00
|
|
|
|
#define MAX_OPEN_DIRS 8
|
|
|
|
|
|
|
|
|
|
static DIR opendirs[MAX_OPEN_DIRS];
|
2002-05-03 15:36:52 +00:00
|
|
|
|
|
2004-12-28 22:16:07 +00:00
|
|
|
|
#ifdef HAVE_MULTIVOLUME
|
|
|
|
|
|
|
|
|
|
/* how to name volumes, first char must be outside of legal file names,
|
|
|
|
|
a number gets appended to enumerate, if applicable */
|
|
|
|
|
#ifdef HAVE_MMC
|
2005-01-01 14:27:28 +00:00
|
|
|
|
static const char* vol_names = "<MMC%d>";
|
|
|
|
|
#define VOL_ENUM_POS 4 /* position of %d, to avoid runtime calculation */
|
2004-12-28 22:16:07 +00:00
|
|
|
|
#else
|
2005-01-01 14:27:28 +00:00
|
|
|
|
static const char* vol_names = "<HD%d>";
|
|
|
|
|
#define VOL_ENUM_POS 3
|
2004-12-28 22:16:07 +00:00
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
/* returns on which volume this is, and copies the reduced name
|
|
|
|
|
(sortof a preprocessor for volume-decorated pathnames) */
|
|
|
|
|
static int strip_volume(const char* name, char* namecopy)
|
|
|
|
|
{
|
|
|
|
|
int volume = 0;
|
|
|
|
|
|
2005-01-01 14:27:28 +00:00
|
|
|
|
if (name[1] == vol_names[0] ) /* a '<' quickly identifies our volumes */
|
2004-12-28 22:16:07 +00:00
|
|
|
|
{
|
|
|
|
|
const char* temp;
|
2005-01-01 14:27:28 +00:00
|
|
|
|
temp = name + 1 + VOL_ENUM_POS; /* behind '/' and special name */
|
2004-12-28 22:16:07 +00:00
|
|
|
|
volume = atoi(temp); /* number is following */
|
|
|
|
|
temp = strchr(temp, '/'); /* search for slash behind */
|
|
|
|
|
if (temp != NULL)
|
|
|
|
|
name = temp; /* use the part behind the volume */
|
|
|
|
|
else
|
|
|
|
|
name = "/"; /* else this must be the root dir */
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
strncpy(namecopy, name, MAX_PATH);
|
|
|
|
|
namecopy[MAX_PATH-1] = '\0';
|
|
|
|
|
|
|
|
|
|
return volume;
|
|
|
|
|
}
|
|
|
|
|
#endif /* #ifdef HAVE_MULTIVOLUME */
|
|
|
|
|
|
|
|
|
|
|
2004-04-20 10:15:39 +00:00
|
|
|
|
DIR* opendir(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;
|
2005-01-03 20:23:42 +00:00
|
|
|
|
DIR* 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 ) {
|
2002-05-03 15:36:52 +00:00
|
|
|
|
DEBUGF("Failed opening dir '%s' (%d)\n",
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int closedir(DIR* dir)
|
|
|
|
|
{
|
2002-05-27 09:13:24 +00:00
|
|
|
|
dir->busy=false;
|
2002-05-03 15:36:52 +00:00
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
struct dirent* readdir(DIR* dir)
|
|
|
|
|
{
|
|
|
|
|
struct fat_direntry entry;
|
2002-05-27 09:13:24 +00:00
|
|
|
|
struct dirent* theent = &(dir->theent);
|
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
|
|
|
|
|
will get redirected upon opendir() */
|
|
|
|
|
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),
|
2005-01-01 14:27:28 +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
|
|
|
|
|
2004-04-20 10:15:39 +00:00
|
|
|
|
int mkdir(const char *name, int mode)
|
2004-01-15 14:30:59 +00:00
|
|
|
|
{
|
|
|
|
|
DIR *dir;
|
|
|
|
|
char namecopy[MAX_PATH];
|
|
|
|
|
char* end;
|
|
|
|
|
char *basename;
|
|
|
|
|
char *parent;
|
|
|
|
|
struct dirent *entry;
|
|
|
|
|
struct fat_dir newdir;
|
|
|
|
|
int rc;
|
2004-01-21 14:58:40 +00:00
|
|
|
|
|
|
|
|
|
(void)mode;
|
2004-02-11 14:37:16 +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);
|
|
|
|
|
|
|
|
|
|
dir = opendir(parent);
|
|
|
|
|
|
|
|
|
|
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 */
|
|
|
|
|
while ((entry = readdir(dir))) {
|
|
|
|
|
if ( !strcasecmp(basename, entry->d_name) ) {
|
|
|
|
|
DEBUGF("mkdir error: file exists\n");
|
|
|
|
|
errno = EEXIST;
|
2004-01-15 14:34:54 +00:00
|
|
|
|
closedir(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));
|
|
|
|
|
|
2004-01-15 15:56:19 +00:00
|
|
|
|
closedir(dir);
|
|
|
|
|
|
2004-01-15 14:30:59 +00:00
|
|
|
|
return rc;
|
|
|
|
|
}
|
2004-04-16 08:58:29 +00:00
|
|
|
|
|
2004-04-20 10:15:39 +00:00
|
|
|
|
int rmdir(const char* name)
|
2004-04-16 08:58:29 +00:00
|
|
|
|
{
|
|
|
|
|
int rc;
|
|
|
|
|
DIR* dir;
|
|
|
|
|
struct dirent* entry;
|
|
|
|
|
|
|
|
|
|
dir = opendir(name);
|
|
|
|
|
if (!dir)
|
|
|
|
|
{
|
|
|
|
|
errno = ENOENT; /* open error */
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* check if the directory is empty */
|
|
|
|
|
while ((entry = readdir(dir)))
|
|
|
|
|
{
|
|
|
|
|
if (strcmp(entry->d_name, ".") &&
|
|
|
|
|
strcmp(entry->d_name, ".."))
|
|
|
|
|
{
|
|
|
|
|
DEBUGF("rmdir error: not empty\n");
|
|
|
|
|
errno = ENOTEMPTY;
|
|
|
|
|
closedir(dir);
|
|
|
|
|
return -2;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
rc = fat_remove(&(dir->fatdir.file));
|
|
|
|
|
if ( rc < 0 ) {
|
|
|
|
|
DEBUGF("Failed removing dir: %d\n", rc);
|
|
|
|
|
errno = EIO;
|
|
|
|
|
rc = rc * 10 - 3;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
closedir(dir);
|
|
|
|
|
|
|
|
|
|
return rc;
|
|
|
|
|
}
|