Repository navigation
Expand file tree
/
Copy pathdiskmount.c
More file actions
174 lines (140 loc) · 4.66 KB
/
Copy pathdiskmount.c
File metadata and controls
174 lines (140 loc) · 4.66 KB
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
/*********************************************************************
* Mounting disks
*********************************************************************/
#ifdef __ORCAC__
#pragma noroot
#endif
#include <finder.h>
#include <list.h>
#include <locator.h>
#include <misctool.h>
#include <quickdraw.h>
#include <qdaux.h>
#include <stdio.h>
#include <string.h>
#include "mounturl.h"
#include "asprintf.h"
#include "json.h"
#include "jsonutil.h"
#include "strcasecmp.h"
#include "diskbrowser.h"
#include "diskmount.h"
#include "browserutil.h"
#include "disksearch.h"
/* Item ID and file extension for the disk image we want to open */
static char *wantedExt;
static char *wantedItemID;
/* Device name of mounted to disk (to give to Finder) */
static GSString32 devName;
static boolean processFile(json_value *fileObj);
static void MountFile(char *itemID, char *fileName, char *fileExt);
void DoMount(void) {
unsigned int itemNumber = NextMember2(0, (Handle)disksListHandle);
char *filesURL = NULL;
enum NetDiskError result = 0;
json_value *filesJSON = NULL;
if (itemNumber == 0) {
// shouldn't happen
return;
}
itemNumber--;
if (diskList[itemNumber].memPtr == noResultsString) {
return;
}
WaitCursor();
if (diskList[itemNumber].memPtr == moreResultsString) {
DoSearch(true);
return;
}
if (diskList[itemNumber].idPtr == NULL) {
result = 2;
goto errorReturn;
}
asprintf(&filesURL,
"http://archive.org/metadata/%s/files",
diskList[itemNumber].idPtr);
if (filesURL == NULL) {
result = 3;
goto errorReturn;
}
result = ReadJSONFromURL(filesURL, &filesJSON);
if (result != OPERATION_SUCCESSFUL)
goto errorReturn;
json_value *resultJSON = findEntryInObject(filesJSON, "result", json_array);
if (resultJSON == NULL) {
result = NOT_EXPECTED_CONTENTS;
goto errorReturn;
}
wantedExt = diskList[itemNumber].extPtr;
wantedItemID = diskList[itemNumber].idPtr;
processArray(resultJSON, json_object, processFile);
errorReturn:
if (filesJSON != NULL)
json_value_free(filesJSON);
free(filesURL);
InitCursor();
if (result != 0)
ShowErrorAlert(result, mountErrorAlert);
}
static boolean processFile(json_value *fileObj) {
if (fileObj == NULL || fileObj->type != json_object)
return false;
json_value *name = findEntryInObject(fileObj, "name", json_string);
if (name == NULL)
return true;
char *nameExt = strrchr(name->u.string.ptr, '.');
if (nameExt == NULL)
return true;
if (strcmp(nameExt + 1, wantedExt) == 0) {
MountFile(wantedItemID, name->u.string.ptr, nameExt + 1);
return false;
}
return true;
}
static void MountFile(char *itemID, char *fileName, char *fileExt) {
static struct MountURLRec mountURLRec = {sizeof(struct MountURLRec)};
if (strcasecmp(fileExt, "woz") == 0 || strcasecmp(fileExt, "nib") == 0) {
ShowErrorAlert(UNSUPPORTED_IMAGE_TYPE, mountErrorAlert);
return;
}
char *fileURL = NULL;
asprintf(&fileURL, "http://archive.org/download/%s/%s", itemID, fileName);
if (fileURL == NULL) {
SysBeep();
return;
}
mountURLRec.result = NETDISK_NOT_PRESENT;
mountURLRec.url = fileURL;
mountURLRec.flags = flgUseCache;
if (strcasecmp(fileExt, "do") == 0) {
mountURLRec.format = formatDOSOrder;
} else if (strcasecmp(fileExt, "po") == 0) {
mountURLRec.format = formatRaw;
} else if (strcasecmp(fileExt, "2mg") == 0) {
mountURLRec.format = format2mg;
} else {
mountURLRec.format = formatAutoDetect;
}
SendRequest(MountURL, sendToName|stopAfterOne, (Long)NETDISK_REQUEST_NAME,
(Long)&mountURLRec, NULL);
if (mountURLRec.result == OPERATION_SUCCESSFUL) {
devName.length = snprintf(devName.text, sizeof(devName.text), ".D%u",
mountURLRec.devNum);
wantToOpenWindow = 2;
} else {
ShowErrorAlert(mountURLRec.result, mountErrorAlert);
}
free(fileURL);
}
/* Show Finder window for newly-mounted disk (once Finder is ready) */
void ShowDiskWindow(void) {
static struct tellFinderOpenWindowOut tfowOut;
/*
* Wait till the Finder has had time to recognize the new disk
* before opening the window for it. Otherwise, it may crash.
*/
if (wantToOpenWindow && --wantToOpenWindow == 0) {
SendRequest(tellFinderOpenWindow, sendToName|stopAfterOne,
(Long)NAME_OF_FINDER, (Long)&devName, (Ptr)&tfowOut);
}
}