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 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504
|
/* --------------------------------------------------------------------
Project: Communication package Linux-HPx00LX Filer
Module: lxdir.c
Author: A. Garzotto
Started: 28. Nov. 95
Last Update: 12-DEC-95
Subject: Copy between Linux and the palmtop
-------------------------------------------------------------------- */
/* --------------------------------------------------------------------
includes
-------------------------------------------------------------------- */
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/stat.h>
#include <ctype.h>
#include <dirent.h>
#define NIL 0
/* --------------------------------------------------------------------
local includes
-------------------------------------------------------------------- */
#include "pal.h"
#include "palpriv.h"
#include "config.h"
size_t MySendBlock(void *Buf, size_t Size, void *Handle);
size_t MyRecvBlock(void *Buf, size_t Size, void *Handle);
/* --------------------------------------------------------------------
Global types and vars
-------------------------------------------------------------------- */
struct list_desc
{
char *name;
char *fname;
int processed;
struct list_desc *next;
};
typedef struct list_desc *LIST;
LIST inlist = NIL;
LIST outlist = NIL;
int recursive = 0;
int archive = 0;
int overwrite = 1;
/* --------------------------------------------------------------------
display help
-------------------------------------------------------------------- */
static void help(void)
{
fprintf(stderr, "USAGE: lxcopy [options] <source file > {<source file>} <dest file>\n");
fprintf(stderr, " options: -<n> sets comm port <n>\n");
fprintf(stderr, " -b <baud> sets baud rate to <baud>\n");
fprintf(stderr, " -r enters subdirectories recursively\n");
fprintf(stderr, " -a only copies files with archive bit set\n");
fprintf(stderr, " -o do not overwrite existing files\n");
fprintf(stderr, " note that the palmtop file name must have a drive specified\n");
exit(1);
}
/* --------------------------------------------------------------------
make DOS directory string
-------------------------------------------------------------------- */
static void makedir(char *dir, int stars)
{
char *p = dir;
int hasdot = 0;
int hasstar = 0;
while (*p)
{
if (*p == '/') *p = '\\';
if (*p == '.') hasdot = 1;
if (*p == '*') hasstar = 1;
p++;
}
if (stars && !hasdot && !hasstar)
{
if (p[-1] == '\\')
strcat(dir, "*.*");
else
strcat(dir, "\\*.*");
}
}
/* --------------------------------------------------------------------
extract the directory from a file name
-------------------------------------------------------------------- */
static void makebase(char *dir, char *base)
{
char *p;
strcpy(base, dir);
p = &base[strlen(base) - 1];
while ((p > base) && (*p != '\\')) p--;
*p = '\0';
}
/* --------------------------------------------------------------------
User defined GET/SEND blocks
-------------------------------------------------------------------- */
FLCB MyFlCb;
/* User defined Send-block routine, replaces default FlCb */
size_t MySendBlock(void *Buf, size_t Size, void *Handle)
{
fprintf(stderr, ".");
return fread(Buf, 1, Size, Handle);
}
/* User defined Get-block routine, replaces default FlCb */
size_t MyRecvBlock(void *Buf, size_t Size, void *Handle)
{
fprintf(stderr, ".");
return fwrite(Buf, 1, Size, Handle);
}
/* --------------------------------------------------------------------
Add name to file list
-------------------------------------------------------------------- */
LIST add_list(char *fname, int input, int processed)
{
LIST li = inlist;
LIST l = (LIST)malloc(sizeof(struct list_desc));
while (li && (li->next)) li = li->next;
l->name = (char *)malloc(strlen(fname) + 15);
strcpy(l->name, fname);
l->processed = processed;
l->fname = NIL;
l->next = NIL;
if (input)
{
if (li)
li->next = l;
else
inlist = l;
}
else
outlist = l;
return l;
}
/* --------------------------------------------------------------------
Create Linux dir if it does not exist
-------------------------------------------------------------------- */
void make_dir(char *path)
{
struct stat statbuf;
char *p = path;
while (*p)
{
while (*p && (*p != '/')) p++;
if (*p)
{
*p = '\0';
if (stat(path, &statbuf))
{
fprintf(stderr, "Creating '%s'...\n", path);
if (mkdir(path, 0777))
perror("Could not create directory");
}
*p = '/';
}
p++;
}
}
/* --------------------------------------------------------------------
Copy from LX to Linux
-------------------------------------------------------------------- */
void from_lx(FILERCOM *pFiler)
{
int sta, num, is_dir;
LIST l = inlist;
LIST l1, l2;
char *p, source[256], target[256], base[256];
struct stat statbuf;
if (recursive)
fprintf(stderr, "Collecting file names...\n");
while (l)
{
if (!l->processed)
{
makedir(l->name, 1);
makebase(l->name, base);
fprintf(stderr, " %s\n", l->name);
sta = FilerAskDir(pFiler, l->name);
if (sta == NO_RESPONSE) fprintf(stderr, "\nServer Not responding.\n");
num = 0;
while (1)
{
if(FilerGetDir(pFiler) == CANNOT_GET_ENTRY) break;
if (pFiler->Attribute & 0x10)
{
if (recursive && (*pFiler->Name != '.'))
{
sprintf(source, "%s\\%s", base, pFiler->Name);
/* fprintf(stderr, "%s\n", source); */
l2 = add_list(source , 1, 0);
l2->fname = (char *) malloc(128);
*l2->fname = '\0';
if (l->fname)
{
strcat(l2->fname, l->fname);
strcat(l2->fname, "/");
}
strcat(l2->fname, (char *)pFiler->Name);
}
continue;
}
if (!archive || (pFiler->Attribute & 32))
{
num++;
l1 = add_list(l->name, 1, 1);
if (l->fname)
{
l1->fname = (char *)malloc(strlen(l->fname) + 20);
strcpy(l1->fname, l->fname);
strcat(l1->fname, "/");
strcat(l1->fname, (char *)pFiler->Name);
}
else
{
l1->fname = (char *)malloc(strlen((char *)pFiler->Name) + 1);
strcpy(l1->fname, (char *)pFiler->Name);
}
p = &(l1->name[strlen(l1->name) - 1]);
while (*p != '\\') p--;
*p = '\0';
/* fprintf(stderr, "%s\n", l1->fname); */
}
}
if (!num && !recursive)
fprintf(stderr, "No match for '%s'\n", l->name);
}
l = l->next;
}
stat(outlist->name, &statbuf);
is_dir = statbuf.st_mode & S_IFDIR;
l = inlist;
while (l)
{
if (l->processed)
{
p = &l->fname[strlen(l->fname) - 1];
while ((p > l->fname) && (*p != '/') && (*p != '\\')) p--;
if (p > l->fname) p++;
sprintf(source, "%s\\%s", l->name, p);
if (is_dir)
{
sprintf(target, "%s/%s", outlist->name, l->fname);
}
else
sprintf(target, "%s", outlist->name);
#ifdef LOWERCASE
p = target;
while (*p) *(p++) = tolower(*p);
#endif
make_dir(target);
if (overwrite || stat(target, &statbuf))
{
fprintf(stderr, "Copying '%s' to '%s'.", source, target);
sta = FilerGetFile(pFiler, source, target);
if (sta != GOT_FILE_OK) fprintf(stderr, "\nCannot get file.\n");
fprintf(stderr, "\n");
}
else
fprintf(stderr, "Preserving '%s'.\n", target);
}
l = l->next;
}
}
/* --------------------------------------------------------------------
Create a directory on the LX
-------------------------------------------------------------------- */
void make_lxdir(FILERCOM *pFiler, char *dir)
{
char *p = dir;
int stat;
while (*p && (*p != '\\')) p++;
p++;
while (*p)
{
while (*p && (*p != '\\')) p++;
if (*p)
{
*p = '\0';
fprintf(stderr, "Making sure '%s' exists...\n", dir);
stat = FilerMakeDir(pFiler, dir);
if(stat==CANNOT_CREATE_DIR)
FilerSync(pFiler);
*p = '\\';
}
p++;
}
}
/* --------------------------------------------------------------------
Copy from Linux to LX
-------------------------------------------------------------------- */
void from_linux(FILERCOM *pFiler)
{
char target[356];
struct stat statbuf;
DIR *dir;
struct dirent *dire;
LIST l = inlist, l1;
char *p;
int sta, is_dir = 0;
if (recursive)
fprintf(stderr, "Collecting file names...\n");
while (l)
{
if (!stat(l->name, &statbuf))
{
if (statbuf.st_mode & S_IFDIR)
{
if (recursive)
{
fprintf(stderr, " %s/*\n", l->name);
dir = opendir(l->name);
while ((dire = readdir(dir)) != 0)
{
sprintf(target, "%s/%s", l->name, dire->d_name);
stat(target, &statbuf);
if (*(dire->d_name) != '.')
{
if (statbuf.st_mode & S_IFDIR)
{
l1 = add_list(target, 1, 0);
}
else
{
l1 = add_list(target, 1, 1);
}
l1->fname = (char *)malloc(128);
if (l->fname)
strcpy(l1->fname, l->fname);
else
strcpy(l1->fname, "");
strcat(l1->fname, "\\");
strcat(l1->fname, dire->d_name);
}
}
}
}
else
l->processed = 1;
}
l = l->next;
}
makedir(outlist->name, 0);
sta = FilerAskDir(pFiler, outlist->name);
if (sta == NO_RESPONSE) fprintf(stderr, "\nServer Not responding.\n");
while (1)
{
if(FilerGetDir(pFiler) == CANNOT_GET_ENTRY) break;
if (pFiler->Attribute & 0x10) is_dir = 1;
}
if (outlist->name[strlen(outlist->name) - 1] == '\\')
{
is_dir = 1;
outlist->name[strlen(outlist->name) - 1] = '\0';
}
l = inlist;
while (l)
{
if (l->processed)
{
strcpy(target, outlist->name);
if (is_dir)
{
if (recursive)
{
strcat(target, l->fname);
}
else
{
p = &(l->name[strlen(l->name) - 1]);
while ((p > l->name) && (*p != '/')) p--;
if (*p == '/') p++;
strcat(target, "\\");
strcat(target, p);
}
}
fprintf(stderr, "Copying '%s' to '%s'.", l->name, target);
sta = FilerSendFile(pFiler, l->name, target);
if (sta != FILE_SEND_OK)
{
fprintf(stderr, "failed!\n");
FilerSync(pFiler);
make_lxdir(pFiler, target);
fprintf(stderr, "Trying again.");
sta = FilerSendFile(pFiler, l->name, target);
if (sta != FILE_SEND_OK)
{
fprintf(stderr, "\nCannot send file.\n");
FilerSync(pFiler);
}
}
fprintf(stderr, "\n");
}
l = l->next;
}
}
/* --------------------------------------------------------------------
M A I N
-------------------------------------------------------------------- */
int main (int argc, char **argv)
{
int port = 1;
int speed = DEF_BAUD;
FILERCOM *pFiler;
FLCB MyFlCb;
int i = 1;
fprintf(stderr, "LXCOPY %s by A. Garzotto\n\n", VERSION);
while (i < argc)
{
if (argv[i][0] == '-')
{
switch (argv[i][1])
{
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8': port = argv[i][1] - '0'; break;
case 'B':
case 'b': speed = atoi(argv[++i]); break;
case 'R':
case 'r': recursive = 1; break;
case 'A':
case 'a': archive = 1; break;
case 'O':
case 'o': overwrite = 0; break;
default: help(); break;
}
}
else if (i < argc - 1)
add_list(argv[i], 1, 0);
else
add_list(argv[i], 0, 0);
i++;
}
if (!inlist || !outlist) help();
/* replace default (PAL) FlCb handler by one of our own */
MyFlCb = FlCb;
MyFlCb.FlcbSendBlock = MySendBlock;
MyFlCb.FlcbRecvBlock = MyRecvBlock;
if(!(pFiler = FilerConnect(port, speed, &MyFlCb))) {
fprintf(stderr, "\nUnable to connect to palmtop!\n");
exit(1);
}
if (strchr(outlist->name, ':'))
from_linux(pFiler);
else
from_lx(pFiler);
FilerDisconnect(pFiler);
exit(0);
}
|