2016-03-02 01:08:54 +00:00
|
|
|
/* file.c - Additional file attributes
|
2008-08-03 18:50:46 +00:00
|
|
|
|
2016-03-02 01:08:54 +00:00
|
|
|
Copyright (C) 1993 Werner Almesberger <werner.almesberger@lrc.di.epfl.ch>
|
|
|
|
Copyright (C) 1998 Roman Hodek <Roman.Hodek@informatik.uni-erlangen.de>
|
|
|
|
Copyright (C) 2008-2014 Daniel Baumann <mail@daniel-baumann.ch>
|
|
|
|
|
|
|
|
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 3 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, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
The complete text of the GNU General Public License
|
|
|
|
can be found in /usr/share/common-licenses/GPL-3 file.
|
|
|
|
*/
|
2008-08-03 18:50:46 +00:00
|
|
|
|
|
|
|
/* FAT32, VFAT, Atari format support, and various fixes additions May 1998
|
|
|
|
* by Roman Hodek <Roman.Hodek@informatik.uni-erlangen.de> */
|
|
|
|
|
2008-08-04 10:00:32 +00:00
|
|
|
#include "vfatlib.h"
|
2008-08-03 18:50:46 +00:00
|
|
|
|
2008-08-04 10:00:32 +00:00
|
|
|
#define NDEBUG
|
|
|
|
#include <debug.h>
|
2008-08-03 18:50:46 +00:00
|
|
|
|
2016-03-02 01:08:54 +00:00
|
|
|
FDSC *fp_root = NULL;
|
2008-08-03 18:50:46 +00:00
|
|
|
|
2016-03-02 01:08:54 +00:00
|
|
|
static void put_char(char **p, unsigned char c)
|
2008-08-03 18:50:46 +00:00
|
|
|
{
|
2018-05-28 21:18:25 +00:00
|
|
|
#ifndef __REACTOS__
|
|
|
|
if (dos_char_to_printable(p, c))
|
|
|
|
return;
|
|
|
|
#endif
|
2016-03-02 01:08:54 +00:00
|
|
|
if ((c >= ' ' && c < 0x7f) || c >= 0xa0)
|
|
|
|
*(*p)++ = c;
|
2008-08-03 18:50:46 +00:00
|
|
|
else {
|
|
|
|
*(*p)++ = '\\';
|
2016-03-02 01:08:54 +00:00
|
|
|
*(*p)++ = '0' + (c >> 6);
|
|
|
|
*(*p)++ = '0' + ((c >> 3) & 7);
|
|
|
|
*(*p)++ = '0' + (c & 7);
|
2008-08-03 18:50:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-02 01:08:54 +00:00
|
|
|
/**
|
|
|
|
* Construct the "pretty-printed" representation of the name in a short directory entry.
|
|
|
|
*
|
|
|
|
* @param[in] fixed Pointer to name[0] of a DIR_ENT
|
|
|
|
*
|
|
|
|
* @return Pointer to static string containing pretty "8.3" equivalent of the
|
|
|
|
* name in the directory entry.
|
|
|
|
*/
|
2008-08-03 18:50:46 +00:00
|
|
|
char *file_name(unsigned char *fixed)
|
|
|
|
{
|
2016-03-02 01:08:54 +00:00
|
|
|
static char path[MSDOS_NAME * 4 + 2];
|
2008-08-03 18:50:46 +00:00
|
|
|
char *p;
|
2016-03-02 01:08:54 +00:00
|
|
|
int i, j;
|
2008-08-03 18:50:46 +00:00
|
|
|
|
|
|
|
p = path;
|
2016-03-02 01:08:54 +00:00
|
|
|
for (i = j = 0; i < 8; i++)
|
2008-08-03 18:50:46 +00:00
|
|
|
if (fixed[i] != ' ') {
|
2016-03-02 01:08:54 +00:00
|
|
|
while (j++ < i)
|
|
|
|
*p++ = ' ';
|
|
|
|
put_char(&p, fixed[i]);
|
2008-08-03 18:50:46 +00:00
|
|
|
}
|
2016-03-02 01:08:54 +00:00
|
|
|
if (strncmp((const char *)(fixed + 8), " ", 3)) {
|
2008-08-03 18:50:46 +00:00
|
|
|
*p++ = '.';
|
2016-03-02 01:08:54 +00:00
|
|
|
for (i = j = 0; i < 3; i++)
|
|
|
|
if (fixed[i + 8] != ' ') {
|
|
|
|
while (j++ < i)
|
|
|
|
*p++ = ' ';
|
|
|
|
put_char(&p, fixed[i + 8]);
|
2008-08-03 18:50:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
*p = 0;
|
|
|
|
return path;
|
|
|
|
}
|
|
|
|
|
2016-03-02 01:08:54 +00:00
|
|
|
int file_cvt(unsigned char *name, unsigned char *fixed)
|
2008-08-03 18:50:46 +00:00
|
|
|
{
|
|
|
|
unsigned char c;
|
2016-03-02 01:08:54 +00:00
|
|
|
int size, ext, cnt;
|
2008-08-03 18:50:46 +00:00
|
|
|
|
|
|
|
size = 8;
|
|
|
|
ext = 0;
|
|
|
|
while (*name) {
|
|
|
|
c = *name;
|
2016-03-02 01:08:54 +00:00
|
|
|
if (c < ' ' || c > 0x7e || strchr("*?<>|\"/", c)) {
|
|
|
|
printf("Invalid character in name. Use \\ooo for special "
|
|
|
|
"characters.\n");
|
2008-08-03 18:50:46 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
if (c == '.') {
|
|
|
|
if (ext) {
|
2016-03-02 01:08:54 +00:00
|
|
|
printf("Duplicate dots in name.\n");
|
2008-08-03 18:50:46 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2016-03-02 01:08:54 +00:00
|
|
|
while (size--)
|
|
|
|
*fixed++ = ' ';
|
2008-08-03 18:50:46 +00:00
|
|
|
size = 3;
|
|
|
|
ext = 1;
|
|
|
|
name++;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (c == '\\') {
|
|
|
|
c = 0;
|
2018-05-28 21:18:25 +00:00
|
|
|
name++;
|
2008-08-03 18:50:46 +00:00
|
|
|
for (cnt = 3; cnt; cnt--) {
|
|
|
|
if (*name < '0' || *name > '7') {
|
2018-05-28 21:18:25 +00:00
|
|
|
printf("Expected three octal digits.\n");
|
2008-08-03 18:50:46 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2016-03-02 01:08:54 +00:00
|
|
|
c = c * 8 + *name++ - '0';
|
2008-08-03 18:50:46 +00:00
|
|
|
}
|
2018-05-28 21:18:25 +00:00
|
|
|
name--;
|
2008-08-03 18:50:46 +00:00
|
|
|
}
|
2016-03-02 01:08:54 +00:00
|
|
|
if (islower(c))
|
|
|
|
c = toupper(c);
|
2008-08-03 18:50:46 +00:00
|
|
|
if (size) {
|
|
|
|
*fixed++ = c;
|
|
|
|
size--;
|
|
|
|
}
|
|
|
|
name++;
|
|
|
|
}
|
2016-03-02 01:08:54 +00:00
|
|
|
if (*name || size == 8)
|
|
|
|
return 0;
|
2008-08-03 18:50:46 +00:00
|
|
|
if (!ext) {
|
2016-03-02 01:08:54 +00:00
|
|
|
while (size--)
|
|
|
|
*fixed++ = ' ';
|
2008-08-03 18:50:46 +00:00
|
|
|
size = 3;
|
|
|
|
}
|
2016-03-02 01:08:54 +00:00
|
|
|
while (size--)
|
|
|
|
*fixed++ = ' ';
|
2008-08-03 18:50:46 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2016-03-02 01:08:54 +00:00
|
|
|
void file_add(char *path, FD_TYPE type)
|
2008-08-03 18:50:46 +00:00
|
|
|
{
|
2016-03-02 01:08:54 +00:00
|
|
|
FDSC **current, *walk;
|
2008-08-03 18:50:46 +00:00
|
|
|
char name[MSDOS_NAME];
|
|
|
|
char *here;
|
|
|
|
|
|
|
|
current = &fp_root;
|
2016-03-02 01:08:54 +00:00
|
|
|
if (*path != '/')
|
|
|
|
die("%s: Absolute path required.", path);
|
2008-08-03 18:50:46 +00:00
|
|
|
path++;
|
|
|
|
while (1) {
|
2016-03-02 01:08:54 +00:00
|
|
|
if ((here = strchr(path, '/')))
|
|
|
|
*here = 0;
|
|
|
|
if (!file_cvt((unsigned char *)path, (unsigned char *)name))
|
|
|
|
exit(2);
|
2008-08-03 18:50:46 +00:00
|
|
|
for (walk = *current; walk; walk = walk->next)
|
2016-03-02 01:08:54 +00:00
|
|
|
if (!here && (!strncmp(name, walk->name, MSDOS_NAME) || (type ==
|
2018-05-28 21:18:25 +00:00
|
|
|
fdt_undelete
|
|
|
|
&&
|
|
|
|
!strncmp
|
|
|
|
(name + 1,
|
|
|
|
walk->name
|
|
|
|
+ 1,
|
|
|
|
MSDOS_NAME
|
|
|
|
- 1))))
|
2016-03-02 01:08:54 +00:00
|
|
|
die("Ambiguous name: \"%s\"", path);
|
|
|
|
else if (here && !strncmp(name, walk->name, MSDOS_NAME))
|
|
|
|
break;
|
2008-08-03 18:50:46 +00:00
|
|
|
if (!walk) {
|
2016-03-02 01:08:54 +00:00
|
|
|
walk = alloc(sizeof(FDSC));
|
|
|
|
strncpy(walk->name, name, MSDOS_NAME);
|
2008-08-03 18:50:46 +00:00
|
|
|
walk->type = here ? fdt_none : type;
|
|
|
|
walk->first = NULL;
|
|
|
|
walk->next = *current;
|
|
|
|
*current = walk;
|
|
|
|
}
|
|
|
|
current = &walk->first;
|
2016-03-02 01:08:54 +00:00
|
|
|
if (!here)
|
|
|
|
break;
|
2008-08-03 18:50:46 +00:00
|
|
|
*here = '/';
|
2016-03-02 01:08:54 +00:00
|
|
|
path = here + 1;
|
2008-08-03 18:50:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-02 01:08:54 +00:00
|
|
|
FDSC **file_cd(FDSC ** curr, char *fixed)
|
2008-08-03 18:50:46 +00:00
|
|
|
{
|
|
|
|
FDSC **walk;
|
|
|
|
|
2016-03-02 01:08:54 +00:00
|
|
|
if (!curr || !*curr)
|
|
|
|
return NULL;
|
2008-08-03 18:50:46 +00:00
|
|
|
for (walk = curr; *walk; walk = &(*walk)->next)
|
2016-03-02 01:08:54 +00:00
|
|
|
if (!strncmp((*walk)->name, fixed, MSDOS_NAME) && (*walk)->first)
|
2008-08-03 18:50:46 +00:00
|
|
|
return &(*walk)->first;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2016-03-02 01:08:54 +00:00
|
|
|
static FDSC **file_find(FDSC ** dir, char *fixed)
|
2008-08-03 18:50:46 +00:00
|
|
|
{
|
2016-03-02 01:08:54 +00:00
|
|
|
if (!dir || !*dir)
|
|
|
|
return NULL;
|
|
|
|
if (*(unsigned char *)fixed == DELETED_FLAG) {
|
2008-08-03 18:50:46 +00:00
|
|
|
while (*dir) {
|
2016-03-02 01:08:54 +00:00
|
|
|
if (!strncmp((*dir)->name + 1, fixed + 1, MSDOS_NAME - 1)
|
|
|
|
&& !(*dir)->first)
|
2008-08-03 18:50:46 +00:00
|
|
|
return dir;
|
|
|
|
dir = &(*dir)->next;
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
while (*dir) {
|
2016-03-02 01:08:54 +00:00
|
|
|
if (!strncmp((*dir)->name, fixed, MSDOS_NAME) && !(*dir)->first)
|
2008-08-03 18:50:46 +00:00
|
|
|
return dir;
|
|
|
|
dir = &(*dir)->next;
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2016-03-02 01:08:54 +00:00
|
|
|
/* Returns the attribute of the file FIXED in directory CURR or FDT_NONE if no
|
|
|
|
such file exists or if CURR is NULL. */
|
|
|
|
FD_TYPE file_type(FDSC ** curr, char *fixed)
|
2008-08-03 18:50:46 +00:00
|
|
|
{
|
|
|
|
FDSC **this;
|
|
|
|
|
2016-03-02 01:08:54 +00:00
|
|
|
if ((this = file_find(curr, fixed)))
|
|
|
|
return (*this)->type;
|
2008-08-03 18:50:46 +00:00
|
|
|
return fdt_none;
|
|
|
|
}
|
|
|
|
|
2016-03-02 01:08:54 +00:00
|
|
|
void file_modify(FDSC ** curr, char *fixed)
|
2008-08-03 18:50:46 +00:00
|
|
|
{
|
2016-03-02 01:08:54 +00:00
|
|
|
FDSC **this, *next;
|
2008-08-03 18:50:46 +00:00
|
|
|
|
2016-03-02 01:08:54 +00:00
|
|
|
if (!(this = file_find(curr, fixed)))
|
2008-08-03 18:50:46 +00:00
|
|
|
die("Internal error: file_find failed");
|
|
|
|
switch ((*this)->type) {
|
2016-03-02 01:08:54 +00:00
|
|
|
case fdt_drop:
|
|
|
|
printf("Dropping %s\n", file_name((unsigned char *)fixed));
|
|
|
|
*(unsigned char *)fixed = DELETED_FLAG;
|
|
|
|
break;
|
|
|
|
case fdt_undelete:
|
|
|
|
*fixed = *(*this)->name;
|
|
|
|
printf("Undeleting %s\n", file_name((unsigned char *)fixed));
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
die("Internal error: file_modify");
|
2008-08-03 18:50:46 +00:00
|
|
|
}
|
|
|
|
next = (*this)->next;
|
2016-03-02 01:08:54 +00:00
|
|
|
free(*this);
|
2008-08-03 18:50:46 +00:00
|
|
|
*this = next;
|
|
|
|
}
|
|
|
|
|
2016-03-02 01:08:54 +00:00
|
|
|
static void report_unused(FDSC * this)
|
2008-08-03 18:50:46 +00:00
|
|
|
{
|
|
|
|
FDSC *next;
|
|
|
|
|
|
|
|
while (this) {
|
|
|
|
next = this->next;
|
2016-03-02 01:08:54 +00:00
|
|
|
if (this->first)
|
|
|
|
report_unused(this->first);
|
2008-08-03 18:50:46 +00:00
|
|
|
else if (this->type != fdt_none)
|
2016-03-02 01:08:54 +00:00
|
|
|
printf("Warning: did not %s file %s\n", this->type == fdt_drop ?
|
|
|
|
"drop" : "undelete", file_name((unsigned char *)this->name));
|
|
|
|
free(this);
|
2008-08-03 18:50:46 +00:00
|
|
|
this = next;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void file_unused(void)
|
|
|
|
{
|
|
|
|
report_unused(fp_root);
|
|
|
|
}
|