Implemented mbfile move

This commit is contained in:
Michiel Broek
2001-12-02 20:49:36 +00:00
parent 752f85de56
commit deb4433932
9 changed files with 195 additions and 89 deletions

View File

@@ -46,8 +46,8 @@ extern int do_annon; /* Supress announce file */
void AdoptFile(int Area, char *File, char *Description)
{
FILE *pAreas, *fp;
char *sAreas, *temp, *temp2, *unarc, *pwd;
FILE *fp;
char *temp, *temp2, *unarc, *pwd;
char Desc[256], TDesc[256];
int IsArchive = FALSE, MustRearc = FALSE, UnPacked = FALSE;
int IsVirus = FALSE, File_Id = FALSE;
@@ -59,34 +59,8 @@ void AdoptFile(int Area, char *File, char *Description)
if (!do_quiet)
colour(CYAN, BLACK);
sAreas = calloc(PATH_MAX, sizeof(char));
sprintf(sAreas, "%s/etc/fareas.data", getenv("MBSE_ROOT"));
if ((pAreas = fopen (sAreas, "r")) == NULL) {
WriteError("$Can't open %s", sAreas);
if (!do_quiet)
printf("Can't open %s\n", sAreas);
if (LoadAreaRec(Area) == FALSE)
die(0);
}
fread(&areahdr, sizeof(areahdr), 1, pAreas);
if (fseek(pAreas, ((Area - 1) * areahdr.recsize) + areahdr.hdrsize, SEEK_SET)) {
WriteError("$Can't seek record %d in %s", Area, sAreas);
if (!do_quiet)
printf("Can't seek record %d in %s\n", Area, sAreas);
fclose(pAreas);
free(sAreas);
die(0);
}
if (fread(&area, areahdr.recsize, 1, pAreas) != 1) {
WriteError("$Can't read record %d in %s", Area, sAreas);
if (!do_quiet)
printf("Can't read record %d in %s\n", Area, sAreas);
fclose(pAreas);
free(sAreas);
die(0);
}
if (area.Available) {
temp = calloc(PATH_MAX, sizeof(char));
@@ -308,12 +282,9 @@ void AdoptFile(int Area, char *File, char *Description)
printf("Area %d is not available\n", Area);
}
fclose(pAreas);
if (!do_quiet) {
printf("\r \r");
fflush(stdout);
}
free(sAreas);
}

View File

@@ -119,20 +119,11 @@ int main(int argc, char **argv)
do_annon = TRUE;
} else {
Description = xstrcpy(argv[i]);
if (argc > (i + 1)) {
i++;
cmd = xstrcat(cmd, (char *)" ");
cmd = xstrcat(cmd, argv[i]);
if (!strncasecmp(argv[i], "-a", 2)) {
do_annon = TRUE;
}
}
}
}
}
if (!strncasecmp(argv[i], "in", 2))
} else if (!strncasecmp(argv[i], "in", 2)) {
do_index = TRUE;
if (!strncasecmp(argv[i], "im", 2)) {
} else if (!strncasecmp(argv[i], "im", 2)) {
if (argc > (i + 1)) {
do_import = TRUE;
i++;
@@ -140,8 +131,7 @@ int main(int argc, char **argv)
cmd = xstrcat(cmd, (char *)" ");
cmd = xstrcat(cmd, argv[i]);
}
}
if (!strncasecmp(argv[i], "l", 1)) {
} else if (!strncasecmp(argv[i], "l", 1)) {
do_list = TRUE;
if (argc > (i + 1)) {
i++;
@@ -149,8 +139,7 @@ int main(int argc, char **argv)
cmd = xstrcat(cmd, (char *)" ");
cmd = xstrcat(cmd, argv[i]);
}
}
if (!strncasecmp(argv[i], "m", 1)) {
} else if (!strncasecmp(argv[i], "m", 1)) {
if (argc > (i + 1)) {
i++;
Area = atoi(argv[i]);
@@ -170,20 +159,22 @@ int main(int argc, char **argv)
}
}
}
}
if (!strncasecmp(argv[i], "p", 1))
} else if (!strncasecmp(argv[i], "p", 1)) {
do_pack = TRUE;
if (!strncasecmp(argv[i], "c", 1))
} else if (!strncasecmp(argv[i], "c", 1)) {
do_check = TRUE;
if (!strncasecmp(argv[i], "k", 1))
} else if (!strncasecmp(argv[i], "k", 1)) {
do_kill = TRUE;
if (!strncasecmp(argv[i], "t", 1))
} else if (!strncasecmp(argv[i], "t", 1)) {
do_tobe = TRUE;
if (!strncasecmp(argv[i], "-q", 2))
} else if (!strncasecmp(argv[i], "-q", 2)) {
do_quiet = TRUE;
} else if (!strncasecmp(argv[i], "-a", 2)) {
do_annon = TRUE;
}
}
if (!(do_pack || do_check || do_kill || do_index || do_import || do_list || do_adopt || do_tobe))
if (!(do_pack || do_check || do_kill || do_index || do_import || do_list || do_adopt || do_move || do_tobe))
Help();
ProgName();

View File

@@ -46,8 +46,8 @@ extern int do_annon; /* Supress announce files */
void ImportFiles(int Area)
{
char *sAreas, *pwd, *temp, *temp2, *String, *token, *dest, *unarc;
FILE *pAreas, *fbbs;
char *pwd, *temp, *temp2, *String, *token, *dest, *unarc;
FILE *fbbs;
int Append = FALSE, Files = 0, i, j = 0, k = 0, x, Doit;
int Imported = 0, Errors = 0;
struct FILERecord fdb;
@@ -58,37 +58,8 @@ void ImportFiles(int Area)
if (!do_quiet)
colour(CYAN, BLACK);
sAreas = calloc(PATH_MAX, sizeof(char));
sprintf(sAreas, "%s/etc/fareas.data", getenv("MBSE_ROOT"));
if ((pAreas = fopen (sAreas, "r")) == NULL) {
WriteError("$Can't open %s", sAreas);
if (!do_quiet)
printf("Can't open %s\n", sAreas);
if (LoadAreaRec(Area) == FALSE)
die(0);
}
fread(&areahdr, sizeof(areahdr), 1, pAreas);
if (fseek(pAreas, ((Area - 1) * areahdr.recsize) + areahdr.hdrsize, SEEK_SET)) {
WriteError("$Can't seek record %d in %s", Area, sAreas);
if (!do_quiet)
printf("Can't seek record %d in %s\n", Area, sAreas);
fclose(pAreas);
free(sAreas);
die(0);
}
if (fread(&area, areahdr.recsize, 1, pAreas) != 1) {
WriteError("$Can't read record %d in %s", Area, sAreas);
if (!do_quiet)
printf("Can't read record %d in %s\n", Area, sAreas);
fclose(pAreas);
free(sAreas);
die(0);
}
fclose(pAreas);
free(sAreas);
if (area.Available && !area.CDrom) {
temp = calloc(PATH_MAX, sizeof(char));

View File

@@ -48,6 +48,127 @@ extern int do_quiet; /* Supress screen output */
*/
void Move(int From, int To, char *File)
{
char *frompath, *topath, *temp1, *temp2;
struct FILERecord fdb;
FILE *fp1, *fp2;
int rc = FALSE;
IsDoing("Move file");
colour(LIGHTRED, BLACK);
if (From == To) {
WriteError("Area numbers are the same");
if (!do_quiet)
printf("Can't move to the same area\n");
die(0);
}
/*
* Check From area
*/
if (LoadAreaRec(From) == FALSE) {
WriteError("Can't load record %d", From);
die(0);
}
if (!area.Available) {
WriteError("Area %d not available", From);
if (!do_quiet)
printf("Area %d not available\n", From);
die(0);
}
if (area.CDrom) {
WriteError("Can't move from CD-ROM");
if (!do_quiet)
printf("Can't move from CD-ROM\n");
die(0);
}
if (CheckFDB(From, area.Path))
die(0);
frompath = xstrcpy(area.Path);
frompath = xstrcat(frompath, (char *)"/");
frompath = xstrcat(frompath, File);
/*
* Check Destination area
*/
if (LoadAreaRec(To) == FALSE) {
WriteError("Can't load record %d", To);
die(0);
}
if (!area.Available) {
WriteError("Area %d not available", To);
if (!do_quiet)
printf("Area %d not available\n", To);
die(0);
}
if (area.CDrom) {
WriteError("Can't move to CD-ROM");
if (!do_quiet)
printf("Can't move to CD-ROM\n");
die(0);
}
if (CheckFDB(To, area.Path))
die(0);
topath = xstrcpy(area.Path);
topath = xstrcat(topath, (char *)"/");
topath = xstrcat(topath, File);
temp1 = calloc(PATH_MAX, sizeof(char));
temp2 = calloc(PATH_MAX, sizeof(char));
sprintf(temp1, "%s/fdb/fdb%d.data", getenv("MBSE_ROOT"), From);
sprintf(temp2, "%s/fdb/fdb%d.temp", getenv("MBSE_ROOT"), From);
if ((fp1 = fopen(temp1, "r")) == NULL)
die(0);
if ((fp2 = fopen(temp2, "a+")) == NULL)
die(0);
/*
* Search the file if the From area, if found, the
* temp database holds all records except the moved
* file.
*/
while (fread(&fdb, sizeof(fdb), 1, fp1) == 1) {
if (strcmp(fdb.LName, File))
fwrite(&fdb, sizeof(fdb), 1, fp2);
else
rc = AddFile(fdb, To, topath, frompath);
}
fclose(fp1);
fclose(fp2);
if (rc) {
/*
* The move was successfull
*/
if (unlink(temp1) == 0) {
rename(temp2, temp1);
chmod(temp1, 0660);
} else {
WriteError("$Can't unlink %s", temp1);
unlink(temp2);
}
unlink(frompath);
colour(CYAN, BLACK);
} else {
/*
* The move failed, it is possible that the file is
* copied already. Don't remove it here, it might
* be removed if it was not meant to be, ie if you
* gave this command twice. Let "mbfile check" take
* care of unwanted copies.
*/
unlink(temp2);
}
Syslog('+', "Move %s from %d to %d %s", File, From, To, rc ? "successfull":"failed");
if (!do_quiet)
printf("Move %s from %d to %d %s\n", File, From, To, rc ? "successfull":"failed");
free(temp1);
free(temp2);
free(frompath);
free(topath);
}

View File

@@ -455,3 +455,47 @@ int CheckFDB(int Area, char *Path)
}
/*
* Load Area Record
*/
int LoadAreaRec(int Area)
{
FILE *pAreas;
char *sAreas;
sAreas = calloc(PATH_MAX, sizeof(char));
sprintf(sAreas, "%s/etc/fareas.data", getenv("MBSE_ROOT"));
if ((pAreas = fopen (sAreas, "r")) == NULL) {
WriteError("$Can't open %s", sAreas);
if (!do_quiet)
printf("Can't open %s\n", sAreas);
return FALSE;
}
fread(&areahdr, sizeof(areahdr), 1, pAreas);
if (fseek(pAreas, ((Area - 1) * areahdr.recsize) + areahdr.hdrsize, SEEK_SET)) {
WriteError("$Can't seek record %d in %s", Area, sAreas);
if (!do_quiet)
printf("Can't seek record %d in %s\n", Area, sAreas);
fclose(pAreas);
free(sAreas);
return FALSE;
}
if (fread(&area, areahdr.recsize, 1, pAreas) != 1) {
WriteError("$Can't read record %d in %s", Area, sAreas);
if (!do_quiet)
printf("Can't read record %d in %s\n", Area, sAreas);
fclose(pAreas);
free(sAreas);
return FALSE;
}
fclose(pAreas);
free(sAreas);
return TRUE;
}

View File

@@ -11,5 +11,6 @@ void DeleteVirusWork(void); /* Delete unarc directory */
int UnpackFile(char *File); /* Unpack archive */
int AddFile(struct FILERecord, int, char *, char *);
int CheckFDB(int, char *); /* Check FDB of area */
int LoadAreaRec(int); /* Load Area record */
#endif