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 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 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441
|
/*
AVFS: A Virtual File System Library
Copyright (C) 1998 Miklos Szeredi <miklos@szeredi.hu>
This program can be distributed under the terms of the GNU GPL.
See the file COPYING.
FLOPPY module (interface for mtools)
*/
#include "remote.h"
#include "runprog.h"
#include <sys/stat.h>
#include <fcntl.h>
struct floppylocalfile {
char *tmpfile;
struct program *pr;
avoff_t currsize;
};
static void strip_spaces(const char *buf, int *ip)
{
int i = *ip;
while(isspace((unsigned char) buf[i])) i++;
*ip = i;
}
static void strip_nonspace(const char *buf, int *ip)
{
int i = *ip;
while(!isspace((unsigned char) buf[i])) i++;
*ip = i;
}
static void strip_spaces_end(char *buf)
{
unsigned int i = strlen(buf);
while(i > 0 && isspace((unsigned char) buf[i-1]))
i--;
buf[i] = '\0';
}
static int get_num(const char *s, int *ip)
{
int i;
int num;
i = *ip;
if(s[i] < '0' || s[i] > '9') return -1;
num = 0;
for(;; i++) {
if(s[i] >= '0' && s[i] <= '9') num = (num * 10) + (s[i] - '0');
else if(s[i] != ',' && s[i] != '.') break;
}
*ip = i;
return num;
}
static int conv_date(const char *s, struct avtm *tms)
{
int num;
int i;
i = 0;
if((num = get_num(s, &i)) == -1 || num < 1 || num > 12) return -1;
tms->mon = num - 1;
i++;
if((num = get_num(s, &i)) == -1 || num < 1 || num > 31) return -1;
tms->day = num;
i++;
if((num = get_num(s, &i)) == -1) return -1;
if(num >= 80 && num < 100) num += 1900;
else if(num >= 0 && num < 80) num += 2000;
if(num < 1900) return -1;
tms->year = num - 1900;
return 0;
}
static int conv_time(const char *s, struct avtm *tms)
{
int num;
int i;
i = 0;
if((num = get_num(s, &i)) == -1 || num < 0) return -1;
tms->hour = num;
i++;
if((num = get_num(s, &i)) == -1 || num < 0 || num > 59) return -1;
tms->min = num;
if(s[i] == ':') {
i++;
if((num = get_num(s, &i)) == -1 || num < 0 || num > 59) return -1;
tms->sec = num;
}
else tms->sec = 0;
if((s[i] == 'p' || s[i] == 'P') && tms->hour < 12) tms->hour += 12;
if(tms->hour > 24) return -1;
if(tms->hour == 24) tms->hour = 0;
return 0;
}
static int process_vollabel(const char *buf, struct avstat *st, char **namep)
{
*namep = av_stradd(NULL, ".vol-", buf + 22, NULL);
strip_spaces_end(*namep);
st->mode = AV_IFREG | 0444;
st->size = 0;
return 0;
}
static int process_dir_line(const char *buf, int vollabel, struct avstat *st,
char **namep)
{
int i, start;
int namelen;
struct avtm tms;
char shortname[32];
i = 0;
if(strncmp(buf, " Volume in drive ", 17) == 0 &&
buf[17] && strncmp(buf+18, " is ", 4) == 0 && buf[22]) {
if(vollabel)
return process_vollabel(buf, st, namep);
else
return -1;
}
strip_nonspace(buf, &i);
if(!buf[i] || i == 0 || i > 8) return -1;
namelen = i;
strncpy(shortname, buf, namelen);
shortname[namelen] = '\0';
strip_spaces(buf, &i);
if(i == 9) {
int extlen;
strip_nonspace(buf, &i);
extlen = i - 9;
if(extlen > 3) return -1;
shortname[namelen++] = '.';
strncpy(shortname+namelen, buf+9, extlen);
namelen += extlen;
shortname[namelen] = '\0';
strip_spaces(buf, &i);
}
if(!buf[i] || i < 13) return -1;
start = i;
strip_nonspace(buf, &i);
if(strncmp("<DIR>", buf + start, i - start) == 0) {
st->size = 0;
st->mode = AV_IFDIR | 0777;
}
else {
int size;
if((size = get_num(buf, &start)) == -1) return -1;
st->size = size;
st->mode = AV_IFREG | 0666;
}
strip_spaces(buf, &i);
if(!buf[i]) return -1;
start = i;
strip_nonspace(buf, &i);
if(conv_date(buf + start, &tms) == -1) return -1;
strip_spaces(buf, &i);
if(!buf[i]) return -1;
start = i;
strip_nonspace(buf, &i);
if(conv_time(buf + start, &tms) == -1) return -1;
strip_spaces(buf, &i);
st->mtime.sec = av_mktime(&tms);
st->mtime.nsec = 0;
if(buf[i]) {
*namep = av_strdup(buf+i);
strip_spaces_end(*namep);
}
else
*namep = av_strdup(shortname);
return 0;
}
static void floppy_parse_line(const char *line, struct remdirlist *dl)
{
int res;
char *filename;
struct avstat stbuf;
int vollabel;
if(strcmp(dl->hostpath.path, "/") == 0)
vollabel = 1;
else
vollabel = 0;
av_default_stat(&stbuf);
res = process_dir_line(line, vollabel, &stbuf, &filename);
if(res != 0)
return;
stbuf.nlink = 1;
stbuf.blksize = 512;
stbuf.blocks = AV_BLOCKS(stbuf.size);
stbuf.atime = stbuf.mtime;
stbuf.ctime = stbuf.mtime;
av_remote_add(dl, filename, NULL, &stbuf);
av_free(filename);
}
static int floppy_read_list(struct program *pr, struct remdirlist *dl)
{
int res;
while(1) {
char *line;
res = av_program_getline(pr, &line, -1);
if(res <= 0)
return res;
if(line == NULL)
return 0;
floppy_parse_line(line, dl);
av_free(line);
}
}
static int floppy_get_path(struct remote *rem, struct remhostpath *hp,
char **resp)
{
char drive[2];
char *alias = (char *) rem->data;
if(alias != NULL)
drive[0] = alias[0];
else {
if(strlen(hp->host) != 1)
return -ENOENT;
drive[0] = tolower((unsigned char) hp->host[0]);
if(drive[0] < 'a' || drive[0] > 'z')
return -ENOENT;
}
drive[1] = '\0';
*resp = av_stradd(NULL, drive, ":", hp->path, NULL);
return 0;
}
static int floppy_list(struct remote *rem, struct remdirlist *dl)
{
int res;
struct program *pr;
const char *prog[4];
char *path;
res = floppy_get_path(rem, &dl->hostpath, &path);
if(res < 0)
return res;
prog[0] = "mdir";
prog[1] = "-a";
prog[2] = path;
prog[3] = NULL;
res = av_start_program(prog, &pr);
if(res == 0) {
res = floppy_read_list(pr, dl);
av_unref_obj(pr);
}
av_free(path);
return res;
}
static void floppy_free_localfile(struct floppylocalfile *lf)
{
if(lf->pr != NULL) {
av_program_log_output(lf->pr);
av_unref_obj(lf->pr);
}
}
static int floppy_get(struct remote *rem, struct remgetparam *gp)
{
int res;
struct floppylocalfile *lf;
char *tmpfile;
const char *prog[4];
char *path;
res = floppy_get_path(rem, &gp->hostpath, &path);
if(res < 0)
return res;
res = av_get_tmpfile(&tmpfile);
if(res < 0) {
av_free(path);
return res;
}
if(strncmp(gp->hostpath.path, "/.vol-", 6) == 0) {
av_free(path);
open(tmpfile, O_WRONLY | O_CREAT | O_TRUNC, 0600);
gp->data = NULL;
gp->localname = tmpfile;
return 0;
}
AV_NEW_OBJ(lf, floppy_free_localfile);
lf->pr = NULL;
lf->tmpfile = tmpfile;
prog[0] = "mcopy";
prog[1] = path;
prog[2] = lf->tmpfile;
prog[3] = NULL;
res = av_start_program(prog, &lf->pr);
av_free(path);
if(res < 0) {
av_unref_obj(lf);
av_del_tmpfile(tmpfile);
return res;
}
lf->currsize = 0;
gp->data = lf;
gp->localname = lf->tmpfile;
return 1;
}
static int floppy_wait(struct remote *rem, void *data, avoff_t end)
{
int res;
struct floppylocalfile *lf = (struct floppylocalfile *) data;
/* FIXME: timeout? */
do {
struct stat stbuf;
res = av_program_log_output(lf->pr);
if(res <= 0)
return res;
res = stat(lf->tmpfile, &stbuf);
if(res == 0)
lf->currsize = stbuf.st_size;
if(lf->currsize < end)
av_sleep(250);
} while(lf->currsize < end);
return 1;
}
static void floppy_destroy(struct remote *rem)
{
av_free(rem->name);
av_free(rem->data);
av_free(rem);
}
static int floppy_init(struct vmodule *module, const char *modname,
const char *alias)
{
int res;
struct remote *rem;
struct avfs *avfs;
AV_NEW(rem);
/* FIXME: Dont cache (at least not for long) */
rem->flags = REM_DIR_ONLY | REM_NOCASE;
rem->data = av_strdup(alias);
rem->name = av_strdup(modname);
rem->destroy = floppy_destroy;
rem->list = floppy_list;
rem->get = floppy_get;
rem->wait = floppy_wait;
res = av_remote_init(module, rem, &avfs);
return res;
}
extern int av_init_module_floppy(struct vmodule *module);
int av_init_module_floppy(struct vmodule *module)
{
int res;
res = floppy_init(module, "floppy", NULL);
if(res < 0)
return res;
res = floppy_init(module, "a", "a");
if(res < 0)
return res;
return 0;
}
|