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
|
*/
struct xvfs_tclfs_channel_id {
Tcl_Channel channel;
struct xvfs_tclfs_instance_info *fsInstanceInfo;
Tcl_Obj *path;
Tcl_WideInt currentOffset;
Tcl_WideInt fileSize;
};
static Tcl_ChannelType xvfs_tclfs_channelType;
static Tcl_Channel xvfs_tclfs_openChannel(Tcl_Obj *path, struct xvfs_tclfs_instance_info *instanceInfo) {
struct xvfs_tclfs_channel_id *channelInstanceData;
Tcl_Channel channel;
Tcl_StatBuf fileInfo;
int statRet;
statRet = instanceInfo->fsInfo->getStatProc(Tcl_GetString(path), &fileInfo);
if (statRet < 0) {
return(NULL);
}
channelInstanceData = (struct xvfs_tclfs_channel_id *) Tcl_Alloc(sizeof(*channelInstanceData));
channelInstanceData->currentOffset = 0;
channelInstanceData->channel = NULL;
channelInstanceData->fsInstanceInfo = instanceInfo;
channelInstanceData->path = path;
channelInstanceData->fileSize = fileInfo.st_size;
Tcl_IncrRefCount(path);
channel = Tcl_CreateChannel(&xvfs_tclfs_channelType, Tcl_GetString(path), channelInstanceData, TCL_READABLE);
if (!channel) {
Tcl_DecrRefCount(path);
Tcl_Free((char *) channelInstanceData);
return(NULL);
}
|
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
|
|
|
>
|
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
|
*/
struct xvfs_tclfs_channel_id {
Tcl_Channel channel;
struct xvfs_tclfs_instance_info *fsInstanceInfo;
Tcl_Obj *path;
Tcl_WideInt currentOffset;
Tcl_WideInt fileSize;
int eofMarked;
};
struct xvfs_tclfs_channel_event {
Tcl_Event tcl;
struct xvfs_tclfs_channel_id *channelInstanceData;
};
static Tcl_ChannelType xvfs_tclfs_channelType;
static Tcl_Channel xvfs_tclfs_openChannel(Tcl_Obj *path, struct xvfs_tclfs_instance_info *instanceInfo) {
struct xvfs_tclfs_channel_id *channelInstanceData;
Tcl_Channel channel;
Tcl_StatBuf fileInfo;
Tcl_Obj *channelName;
int statRet;
statRet = instanceInfo->fsInfo->getStatProc(Tcl_GetString(path), &fileInfo);
if (statRet < 0) {
return(NULL);
}
channelInstanceData = (struct xvfs_tclfs_channel_id *) Tcl_Alloc(sizeof(*channelInstanceData));
channelInstanceData->currentOffset = 0;
channelInstanceData->eofMarked = 0;
channelInstanceData->channel = NULL;
channelName = Tcl_ObjPrintf("xvfs0x%llx", (unsigned long long) channelInstanceData);
if (!channelName) {
Tcl_Free((char *) channelInstanceData);
return(NULL);
}
Tcl_IncrRefCount(channelName);
channelInstanceData->fsInstanceInfo = instanceInfo;
channelInstanceData->fileSize = fileInfo.st_size;
channelInstanceData->path = path;
Tcl_IncrRefCount(path);
channel = Tcl_CreateChannel(&xvfs_tclfs_channelType, Tcl_GetString(channelName), channelInstanceData, TCL_READABLE);
Tcl_DecrRefCount(channelName);
if (!channel) {
Tcl_DecrRefCount(path);
Tcl_Free((char *) channelInstanceData);
return(NULL);
}
|
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
|
static int xvfs_tclfs_readChannel(ClientData channelInstanceData_p, char *buf, int bufSize, int *errorCodePtr) {
struct xvfs_tclfs_channel_id *channelInstanceData;
const unsigned char *data;
Tcl_WideInt offset, length;
char *path;
channelInstanceData = (struct xvfs_tclfs_channel_id *) channelInstanceData_p;
path = Tcl_GetString(channelInstanceData->path);
offset = channelInstanceData->currentOffset;
length = bufSize;
data = channelInstanceData->fsInstanceInfo->fsInfo->getDataProc(path, offset, &length);
if (length < 0) {
*errorCodePtr = xvfs_errorToErrno(length);
return(-1);
}
memcpy(buf, data, length);
channelInstanceData->currentOffset += length;
return(length);
}
static void xvfs_tclfs_watchChannel(ClientData channelInstanceData_p, int mask) {
if ((mask & TCL_READABLE) != TCL_READABLE) {
return;
}
/* XXX:TODO: fprintf(stderr, "Incomplete watch called, mask = %i\n", mask); */
return;
}
static int xvfs_tclfs_seekChannel(ClientData channelInstanceData_p, long offset, int mode, int *errorCodePtr) {
struct xvfs_tclfs_channel_id *channelInstanceData;
Tcl_WideInt newOffset, fileSize;
|
>
>
>
>
>
>
>
>
>
>
>
|
|
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
|
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
|
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
|
static int xvfs_tclfs_readChannel(ClientData channelInstanceData_p, char *buf, int bufSize, int *errorCodePtr) {
struct xvfs_tclfs_channel_id *channelInstanceData;
const unsigned char *data;
Tcl_WideInt offset, length;
char *path;
channelInstanceData = (struct xvfs_tclfs_channel_id *) channelInstanceData_p;
/*
* If we are already at the end of the file we can skip
* attempting to read it
*/
if (channelInstanceData->eofMarked) {
return(0);
}
path = Tcl_GetString(channelInstanceData->path);
offset = channelInstanceData->currentOffset;
length = bufSize;
data = channelInstanceData->fsInstanceInfo->fsInfo->getDataProc(path, offset, &length);
if (length < 0) {
*errorCodePtr = xvfs_errorToErrno(length);
return(-1);
}
if (length == 0) {
channelInstanceData->eofMarked = 1;
} else {
memcpy(buf, data, length);
channelInstanceData->currentOffset += length;
}
return(length);
}
static int xvfs_tclfs_watchChannelEvent(Tcl_Event *event_p, int flags) {
struct xvfs_tclfs_channel_id *channelInstanceData;
struct xvfs_tclfs_channel_event *event;
event = (struct xvfs_tclfs_channel_event *) event_p;
channelInstanceData = event->channelInstanceData;
Tcl_NotifyChannel(channelInstanceData->channel, TCL_READABLE);
return(0);
}
static void xvfs_tclfs_watchChannel(ClientData channelInstanceData_p, int mask) {
struct xvfs_tclfs_channel_id *channelInstanceData;
struct xvfs_tclfs_channel_event *event;
if ((mask & TCL_READABLE) != TCL_READABLE) {
return;
}
channelInstanceData = (struct xvfs_tclfs_channel_id *) channelInstanceData_p;
/*
* If the read call has marked that we have reached EOF,
* do not signal any further
*/
if (channelInstanceData->eofMarked) {
return;
}
event = (struct xvfs_tclfs_channel_event *) Tcl_Alloc(sizeof(*event));
event->tcl.proc = xvfs_tclfs_watchChannelEvent;
event->tcl.nextPtr = NULL;
event->channelInstanceData = channelInstanceData;
Tcl_QueueEvent((Tcl_Event *) event, TCL_QUEUE_TAIL);
return;
}
static int xvfs_tclfs_seekChannel(ClientData channelInstanceData_p, long offset, int mode, int *errorCodePtr) {
struct xvfs_tclfs_channel_id *channelInstanceData;
Tcl_WideInt newOffset, fileSize;
|
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
|
*/
if (newOffset < 0 || newOffset > fileSize) {
*errorCodePtr = xvfs_errorToErrno(XVFS_RV_ERR_EINVAL);
return(-1);
}
channelInstanceData->currentOffset = newOffset;
return(channelInstanceData->currentOffset);
}
static void xvfs_tclfs_prepareChannelType(void) {
xvfs_tclfs_channelType.typeName = "xvfs";
xvfs_tclfs_channelType.version = TCL_CHANNEL_VERSION_2;
|
>
>
|
>
|
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
|
*/
if (newOffset < 0 || newOffset > fileSize) {
*errorCodePtr = xvfs_errorToErrno(XVFS_RV_ERR_EINVAL);
return(-1);
}
if (newOffset != channelInstanceData->currentOffset) {
channelInstanceData->eofMarked = 0;
channelInstanceData->currentOffset = newOffset;
}
return(channelInstanceData->currentOffset);
}
static void xvfs_tclfs_prepareChannelType(void) {
xvfs_tclfs_channelType.typeName = "xvfs";
xvfs_tclfs_channelType.version = TCL_CHANNEL_VERSION_2;
|