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"
|
|
|
|
|
|
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-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;
|
|
|
|
|
|
|
|
|
|
/* find a free dir descriptor */
|
|
|
|
|
for ( dd=0; dd<MAX_OPEN_DIRS; dd++ )
|
|
|
|
|
if ( !opendirs[dd].busy )
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
2002-05-27 09:13:24 +00:00
|
|
|
|
opendirs[dd].busy = true;
|
|
|
|
|
|
2002-05-03 15:36:52 +00:00
|
|
|
|
if ( name[0] != '/' ) {
|
|
|
|
|
DEBUGF("Only absolute paths supported right now\n");
|
2002-05-27 09:13:24 +00:00
|
|
|
|
opendirs[dd].busy = false;
|
2002-05-03 15:36:52 +00:00
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
2004-04-16 08:58:29 +00:00
|
|
|
|
if ( fat_opendir(&(opendirs[dd].fatdir), 0, NULL) < 0 ) {
|
2002-05-03 15:36:52 +00:00
|
|
|
|
DEBUGF("Failed opening root dir\n");
|
2002-05-27 09:13:24 +00:00
|
|
|
|
opendirs[dd].busy = false;
|
2002-05-03 15:36:52 +00:00
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
2002-05-24 09:57:33 +00:00
|
|
|
|
strncpy(namecopy,name,sizeof(namecopy));
|
|
|
|
|
namecopy[sizeof(namecopy)-1] = 0;
|
|
|
|
|
|
|
|
|
|
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) {
|
2002-05-27 09:13:24 +00:00
|
|
|
|
if ((fat_getnext(&(opendirs[dd].fatdir),&entry) < 0) ||
|
|
|
|
|
(!entry.name[0])) {
|
|
|
|
|
opendirs[dd].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)) ) {
|
2004-04-16 08:58:29 +00:00
|
|
|
|
opendirs[dd].parent_dir = opendirs[dd].fatdir;
|
2002-05-27 09:13:24 +00:00
|
|
|
|
if ( fat_opendir(&(opendirs[dd].fatdir),
|
2004-04-16 08:58:29 +00:00
|
|
|
|
entry.firstcluster,
|
|
|
|
|
&(opendirs[dd].parent_dir)) < 0 ) {
|
2002-05-03 15:36:52 +00:00
|
|
|
|
DEBUGF("Failed opening dir '%s' (%d)\n",
|
|
|
|
|
part, entry.firstcluster);
|
2002-05-27 09:13:24 +00:00
|
|
|
|
opendirs[dd].busy = false;
|
2002-05-03 15:36:52 +00:00
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2002-05-27 09:13:24 +00:00
|
|
|
|
return &opendirs[dd];
|
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);
|
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;
|
|
|
|
|
}
|