summaryrefslogtreecommitdiff
path: root/src/Dolphin/card/CARDDelete.c
blob: f715751c7ee744db185c20a7d205b9f97050a770 (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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#include <dolphin/card.h>
#include <dolphin/dsp.h>
#include <dolphin/dvd.h>
#include <dolphin/os.h>

#include <dolphin/CARDPriv.h>

static void DeleteCallback(s32 chan, s32 result) {
  CARDControl* card;
  CARDCallback callback;

  card = &__CARDBlock[chan];
  callback = card->apiCallback;
  card->apiCallback = 0;

  if (result < 0) {
    goto error;
  }

  result = __CARDFreeBlock(chan, card->startBlock, callback);
  if (result < 0) {
    goto error;
  }
  return;

error:
  __CARDPutControlBlock(card, result);
  if (callback) {
    callback(chan, result);
  }
}

s32 CARDFastDeleteAsync(s32 chan, s32 fileNo, CARDCallback callback) {
  CARDControl* card;
  CARDDir* dir;
  CARDDir* ent;
  s32 result;

  if (fileNo < 0 || CARD_MAX_FILE <= fileNo) {
    return CARD_RESULT_FATAL_ERROR;
  }
  result = __CARDGetControlBlock(chan, &card);
  if (result < 0) {
    return result;
  }

  dir = __CARDGetDirBlock(card);
  ent = &dir[fileNo];
  result = __CARDAccess(card, ent);
  if (result < 0) {
    return __CARDPutControlBlock(card, result);
  }
  if (__CARDIsOpened(card, fileNo)) {
    return __CARDPutControlBlock(card, CARD_RESULT_BUSY);
  }
  card->startBlock = ent->startBlock;
  memset(ent, 0xff, sizeof(CARDDir));

  card->apiCallback = callback ? callback : __CARDDefaultApiCallback;
  result = __CARDUpdateDir(chan, DeleteCallback);
  if (result < 0) {
    __CARDPutControlBlock(card, result);
  }
  return result;
}

s32 CARDDeleteAsync(s32 chan, const char* fileName, CARDCallback callback) {
  CARDControl* card;
  s32 fileNo;
  s32 result;
  CARDDir* dir;
  CARDDir* ent;

  result = __CARDGetControlBlock(chan, &card);
  if (result < 0) {
    return result;
  }
  result = __CARDGetFileNo(card, fileName, &fileNo);
  if (result < 0) {
    return __CARDPutControlBlock(card, result);
  }
  if (__CARDIsOpened(card, fileNo)) {
    return __CARDPutControlBlock(card, CARD_RESULT_BUSY);
  }

  dir = __CARDGetDirBlock(card);
  ent = &dir[fileNo];
  card->startBlock = ent->startBlock;
  memset(ent, 0xff, sizeof(CARDDir));

  card->apiCallback = callback ? callback : __CARDDefaultApiCallback;
  result = __CARDUpdateDir(chan, DeleteCallback);
  if (result < 0) {
    __CARDPutControlBlock(card, result);
  }
  return result;
}