summaryrefslogtreecommitdiff
path: root/src/Dolphin/card/CARDRename.c
blob: 83d1c8a55ba3358aea6e7cc3ec46675463660252 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
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;
}