summaryrefslogtreecommitdiff
path: root/src/Dolphin/card/CARDRename.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/Dolphin/card/CARDRename.c')
-rw-r--r--src/Dolphin/card/CARDRename.c70
1 files changed, 70 insertions, 0 deletions
diff --git a/src/Dolphin/card/CARDRename.c b/src/Dolphin/card/CARDRename.c
new file mode 100644
index 0000000..83d1c8a
--- /dev/null
+++ b/src/Dolphin/card/CARDRename.c
@@ -0,0 +1,70 @@
+#include <dolphin/card.h>
+#include <dolphin/dsp.h>
+#include <dolphin/dvd.h>
+#include <dolphin/os.h>
+
+#include <dolphin/CARDPriv.h>
+
+s32 CARDRenameAsync(s32 chan, const char* old, const char* new, CARDCallback callback) {
+ CARDControl* card;
+ CARDDir* dir;
+ CARDDir* ent;
+ s32 result;
+ int fileNo;
+ int newNo;
+ int oldNo;
+
+ if (*old == 0xff || *new == 0xff || *old == 0x00 || *new == 0x00) {
+ return CARD_RESULT_FATAL_ERROR;
+ }
+ if (CARD_FILENAME_MAX < (u32)strlen(old) || CARD_FILENAME_MAX < (u32)strlen(new)) {
+ return CARD_RESULT_NAMETOOLONG;
+ }
+ result = __CARDGetControlBlock(chan, &card);
+ if (result < 0) {
+ return result;
+ }
+
+ newNo = oldNo = -1;
+ dir = __CARDGetDirBlock(card);
+ for (fileNo = 0; fileNo < CARD_MAX_FILE; fileNo++) {
+ ent = &dir[fileNo];
+ if (ent->gameName[0] == 0xff) {
+ continue;
+ }
+
+ if (memcmp(ent->gameName, card->diskID->gameName, sizeof(ent->gameName)) != 0 ||
+ memcmp(ent->company, card->diskID->company, sizeof(ent->company)) != 0) {
+ continue;
+ }
+
+ if (__CARDCompareFileName(ent, old)) {
+ oldNo = fileNo;
+ }
+ if (__CARDCompareFileName(ent, new)) {
+ newNo = fileNo;
+ }
+ }
+
+ if (oldNo == -1) {
+ return __CARDPutControlBlock(card, CARD_RESULT_NOFILE);
+ }
+ if (newNo != -1) {
+ return __CARDPutControlBlock(card, CARD_RESULT_EXIST);
+ }
+
+ ent = &dir[oldNo];
+ result = __CARDAccess(card, ent);
+ if (result < 0) {
+ return __CARDPutControlBlock(card, result);
+ }
+
+ strncpy((char*)ent->fileName, new, CARD_FILENAME_MAX);
+
+ ent->time = (u32)OSTicksToSeconds(OSGetTime());
+ result = __CARDUpdateDir(chan, callback);
+ if (result < 0) {
+ __CARDPutControlBlock(card, result);
+ }
+ return result;
+}