97 lines
3.6 KiB
C
97 lines
3.6 KiB
C
// SPDX-License-Identifier: GPL-3.0-or-later
|
|
#include <stdio.h>
|
|
#include "disk-internal.h"
|
|
|
|
// Remove the name before freeing its storage. An interrupted reclaim can
|
|
// leave allocated space behind, but a live file never points into free space.
|
|
static byte eraseEntry(byte drive, byte sector, byte offset)
|
|
{
|
|
DECBDirEntry *entry = (DECBDirEntry *)(vcc_disk_buffers.directory + offset);
|
|
DECBDirEntry erased;
|
|
byte first = entry->name[0], g = entry->firstGranule;
|
|
entry->name[0] = 0;
|
|
memcpy(&erased, entry, sizeof(erased));
|
|
byte error = vcc_disk_sector(drive, DECB_DSKCON_WRITE, sector, vcc_disk_buffers.directory);
|
|
if (error) return error;
|
|
// checkOwnership has already checked every link for range and sharing.
|
|
while (g < DECB_MAX_NUM_GRANULES)
|
|
{
|
|
byte next = vcc_disk_buffers.fat[g];
|
|
vcc_disk_buffers.fat[g] = 0xff;
|
|
g = next;
|
|
}
|
|
DECBDrive *registered = decb_getDrive(drive);
|
|
if (registered) registered->fatLoaded = FALSE;
|
|
error = vcc_disk_sector(drive, DECB_DSKCON_WRITE, 2, vcc_disk_buffers.fat);
|
|
// If the reclaim was refused, restore the name only when the old FAT
|
|
// and deleted entry can both be confirmed on disk. Otherwise leave it
|
|
// detached: reattaching to an uncertain chain could damage another file.
|
|
if (error && !vcc_disk_sector(drive, DECB_DSKCON_READ, 2, vcc_disk_buffers.fat)
|
|
&& !memcmp(vcc_disk_buffers.fat, vcc_disk_buffers.previousFAT, DECB_MAX_NUM_GRANULES)
|
|
&& !vcc_disk_sector(drive, DECB_DSKCON_READ, sector, vcc_disk_buffers.directory)
|
|
&& !memcmp(entry, &erased, sizeof(erased)))
|
|
{
|
|
entry->name[0] = first;
|
|
vcc_disk_sector(drive, DECB_DSKCON_WRITE, sector, vcc_disk_buffers.directory);
|
|
}
|
|
return error;
|
|
}
|
|
|
|
static byte closedPath(const char *path, char *name, byte *drive)
|
|
{
|
|
char raw[13];
|
|
byte error = vcc_disk_parse_path(path, raw, drive);
|
|
if (error) return error;
|
|
decb_normalizeFilename(name, raw);
|
|
return vcc_disk_path_open(*drive, name) ? DECB_ERR_ALREADY_EXISTS : DECB_OK;
|
|
}
|
|
|
|
static int changeName(const char *path, const char *destination)
|
|
{
|
|
byte drive, newDrive, sector, offset;
|
|
char name[12], newName[12];
|
|
byte error = closedPath(path, name, &drive);
|
|
if (error) return vcc_disk_result(error);
|
|
if (destination)
|
|
{
|
|
error = closedPath(destination, newName, &newDrive);
|
|
if (error) return vcc_disk_result(error);
|
|
if (drive != newDrive) return vcc_disk_result(DECB_ERR_INVALID_ARGUMENT);
|
|
}
|
|
struct VccDiskBackend saved;
|
|
error = vcc_disk_use_rom(&saved);
|
|
if (error) return vcc_disk_result(error);
|
|
error = vcc_disk_check_volume(drive);
|
|
if (!error && destination)
|
|
{
|
|
error = decb_findDirEntry(FALSE, drive, newName, vcc_disk_buffers.directory, §or, &offset);
|
|
if (error == DECB_OK) error = DECB_ERR_ALREADY_EXISTS;
|
|
else if (error == DECB_ERR_NOT_FOUND) error = DECB_OK;
|
|
}
|
|
if (!error)
|
|
error = decb_findDirEntry(FALSE, drive, name, vcc_disk_buffers.directory, §or, &offset);
|
|
if (!error)
|
|
{
|
|
if (destination)
|
|
{
|
|
memcpy(vcc_disk_buffers.directory + offset, newName, 11);
|
|
error = vcc_disk_sector(drive, DECB_DSKCON_WRITE, sector, vcc_disk_buffers.directory);
|
|
}
|
|
else
|
|
error = eraseEntry(drive, sector, offset);
|
|
}
|
|
vcc_disk_restore(&saved);
|
|
return vcc_disk_result(error);
|
|
}
|
|
|
|
int rename(const char *oldPath, const char *newPath)
|
|
{
|
|
if (!newPath) return vcc_disk_result(DECB_ERR_INVALID_ARGUMENT);
|
|
return changeName(oldPath, newPath);
|
|
}
|
|
|
|
int remove(const char *path)
|
|
{
|
|
return changeName(path, NULL);
|
|
}
|