This repository has been archived on 2024-04-08. You can view files and clone it, but cannot push or open issues or pull requests.
deb-mbse/mbfido/mbfmove.c

275 lines
7.2 KiB
C
Raw Normal View History

2001-12-02 19:17:14 +00:00
/*****************************************************************************
*
* $Id$
* Purpose: File Database Maintenance - Move a file
*
*****************************************************************************
2004-02-21 17:22:00 +00:00
* Copyright (C) 1997-2004
2001-12-02 19:17:14 +00:00
*
* Michiel Broek FIDO: 2:280/2802
* Beekmansbos 10
* 1971 BV IJmuiden
* the Netherlands
*
* This file is part of MBSE BBS.
*
* This BBS is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the
* Free Software Foundation; either version 2, or (at your option) any
* later version.
*
* MBSE BBS is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with MBSE BBS; see the file COPYING. If not, write to the Free
2003-08-15 20:05:34 +00:00
* Software Foundation, 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
2001-12-02 19:17:14 +00:00
*****************************************************************************/
2002-06-30 12:48:44 +00:00
#include "../config.h"
2004-02-21 17:22:00 +00:00
#include "../lib/mbselib.h"
2002-01-07 19:16:03 +00:00
#include "../lib/users.h"
2004-02-21 17:22:00 +00:00
#include "../lib/mbsedb.h"
2001-12-02 19:17:14 +00:00
#include "mbfutil.h"
#include "mbfmove.h"
extern int do_quiet; /* Suppress screen output */
2001-12-02 19:17:14 +00:00
/*
* Move a file
*/
void Move(int From, int To, char *File)
{
2004-05-06 14:16:03 +00:00
char *frompath, *topath, *temp1, *fromlink, *tolink, *fromthumb, *tothumb;
2004-03-06 21:48:41 +00:00
struct FILE_record f_db;
int rc = FALSE, Found = FALSE;
2004-05-06 14:16:03 +00:00
#ifdef USE_EXPERIMENT
struct _fdbarea *src_area = NULL;
#else
struct FILE_recordhdr f_dbhdr;
FILE *fp1, *fp2;
char *temp2;
#endif
2001-12-02 20:49:36 +00:00
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");
2002-10-20 20:58:55 +00:00
die(MBERR_COMMANDLINE);
2001-12-02 20:49:36 +00:00
}
/*
* Check From area
*/
if (LoadAreaRec(From) == FALSE) {
WriteError("Can't load record %d", From);
2002-10-20 20:58:55 +00:00
die(MBERR_INIT_ERROR);
2001-12-02 20:49:36 +00:00
}
if (!area.Available) {
WriteError("Area %d not available", From);
if (!do_quiet)
printf("Area %d not available\n", From);
2002-10-20 20:58:55 +00:00
die(MBERR_COMMANDLINE);
2001-12-02 20:49:36 +00:00
}
if (area.CDrom) {
WriteError("Can't move from CD-ROM");
if (!do_quiet)
printf("Can't move from CD-ROM\n");
2002-10-20 20:58:55 +00:00
die(MBERR_COMMANDLINE);
2001-12-02 20:49:36 +00:00
}
if (CheckFDB(From, area.Path))
2002-10-20 20:58:55 +00:00
die(MBERR_GENERAL);
/*
* Find the file in the "from" area, check LFN and 8.3 names.
*/
2004-05-06 14:16:03 +00:00
#ifdef USE_EXPERIMENT
if ((src_area = mbsedb_OpenFDB(From, 30)) == NULL)
die(MBERR_GENERAL);
while (fread(&f_db, fdbhdr.recsize, 1, src_area->fp) == 1) {
if ((strcmp(f_db.LName, File) == 0) || strcmp(f_db.Name, File) == 0) {
Found = TRUE;
break;
}
}
temp1 = calloc(PATH_MAX, sizeof(char)); // not serious
#else
temp1 = calloc(PATH_MAX, sizeof(char));
2004-03-06 21:48:41 +00:00
sprintf(temp1, "%s/fdb/file%d.data", getenv("MBSE_ROOT"), From);
if ((fp1 = fopen(temp1, "r")) == NULL)
die(MBERR_GENERAL);
2004-03-06 21:48:41 +00:00
fread(&f_dbhdr, sizeof(fdbhdr), 1, fp1);
while (fread(&f_db, f_dbhdr.recsize, 1, fp1) == 1) {
if ((strcmp(f_db.LName, File) == 0) || strcmp(f_db.Name, File) == 0) {
Found = TRUE;
break;
}
}
fclose(fp1);
2004-05-06 14:16:03 +00:00
#endif
if (!Found) {
WriteError("File %s not found in area %d", File, From);
if (!do_quiet)
printf("File %s not found in area %d\n", File, From);
free(temp1);
die(MBERR_GENERAL);
}
2001-12-02 20:49:36 +00:00
frompath = xstrcpy(area.Path);
frompath = xstrcat(frompath, (char *)"/");
2004-03-06 21:48:41 +00:00
frompath = xstrcat(frompath, f_db.Name);
fromlink = xstrcpy(area.Path);
fromlink = xstrcat(fromlink, (char *)"/");
2004-03-06 21:48:41 +00:00
fromlink = xstrcat(fromlink, f_db.LName);
2002-12-09 21:11:43 +00:00
fromthumb = xstrcpy(area.Path);
fromthumb = xstrcat(fromthumb, (char *)"/.");
2004-03-06 21:48:41 +00:00
fromthumb = xstrcat(fromthumb, f_db.Name);
2001-12-02 20:49:36 +00:00
/*
* Check Destination area
*/
if (LoadAreaRec(To) == FALSE) {
WriteError("Can't load record %d", To);
2002-10-20 20:58:55 +00:00
die(MBERR_GENERAL);
2001-12-02 20:49:36 +00:00
}
if (!area.Available) {
WriteError("Area %d not available", To);
if (!do_quiet)
printf("Area %d not available\n", To);
2002-10-20 20:58:55 +00:00
die(MBERR_GENERAL);
2001-12-02 20:49:36 +00:00
}
if (area.CDrom) {
WriteError("Can't move to CD-ROM");
if (!do_quiet)
printf("Can't move to CD-ROM\n");
2002-10-20 20:58:55 +00:00
die(MBERR_COMMANDLINE);
2001-12-02 20:49:36 +00:00
}
if (CheckFDB(To, area.Path))
2002-10-20 20:58:55 +00:00
die(MBERR_GENERAL);
2001-12-02 20:49:36 +00:00
topath = xstrcpy(area.Path);
topath = xstrcat(topath, (char *)"/");
2004-03-06 21:48:41 +00:00
topath = xstrcat(topath, f_db.Name);
tolink = xstrcpy(area.Path);
tolink = xstrcat(tolink, (char *)"/");
2004-03-06 21:48:41 +00:00
tolink = xstrcat(tolink, f_db.LName);
2002-12-09 21:11:43 +00:00
tothumb = xstrcpy(area.Path);
tothumb = xstrcat(tothumb, (char *)"/.");
2004-03-06 21:48:41 +00:00
tothumb = xstrcat(tothumb, f_db.Name);
2001-12-02 20:49:36 +00:00
if (file_exist(topath, F_OK) == 0) {
WriteError("File %s already exists in area %d", File, To);
if (!do_quiet)
printf("File %s already exists in area %d\n", File, To);
die(MBERR_COMMANDLINE);
}
2004-05-06 14:16:03 +00:00
#ifdef USE_EXPERIMENT
rc = AddFile(f_db, To, topath, frompath, tolink);
if (rc) {
unlink(fromlink);
unlink(frompath);
/*
* Try to move thumbnail if it exists
*/
if (file_exist(fromthumb, R_OK) == 0) {
file_mv(fromthumb, tothumb);
}
}
if (mbsedb_LockFDB(src_area, 30)) {
f_db.Deleted = TRUE;
fseek(src_area->fp, - fdbhdr.recsize, SEEK_CUR);
fwrite(&f_db, fdbhdr.recsize, 1, src_area->fp);
mbsedb_UnlockFDB(src_area);
}
mbsedb_PackFDB(src_area);
mbsedb_CloseFDB(src_area);
colour(CYAN, BLACK);
#else
2001-12-02 20:49:36 +00:00
temp2 = calloc(PATH_MAX, sizeof(char));
2004-03-06 21:48:41 +00:00
sprintf(temp2, "%s/fdb/file%d.temp", getenv("MBSE_ROOT"), From);
2001-12-02 20:49:36 +00:00
if ((fp1 = fopen(temp1, "r")) == NULL)
2002-10-20 20:58:55 +00:00
die(MBERR_GENERAL);
2004-03-06 21:48:41 +00:00
fread(&f_dbhdr, sizeof(fdbhdr), 1, fp1);
2001-12-02 20:49:36 +00:00
if ((fp2 = fopen(temp2, "a+")) == NULL)
2002-10-20 20:58:55 +00:00
die(MBERR_GENERAL);
2004-03-06 21:48:41 +00:00
fwrite(&f_dbhdr, f_dbhdr.hdrsize, 1, fp2);
2001-12-02 20:49:36 +00:00
/*
* Search the file if the From area, if found, the
* temp database holds all records except the moved
* file.
*/
2004-03-06 21:48:41 +00:00
while (fread(&f_db, f_dbhdr.recsize, 1, fp1) == 1) {
if (strcmp(f_db.LName, File) && strcmp(f_db.Name, File))
fwrite(&f_db, f_dbhdr.recsize, 1, fp2);
else {
2004-03-06 21:48:41 +00:00
rc = AddFile(f_db, To, topath, frompath, tolink);
if (rc) {
2002-12-09 21:11:43 +00:00
unlink(fromlink);
unlink(frompath);
/*
* Try to move thumbnail if it exists
*/
2002-12-09 21:11:43 +00:00
if (file_exist(fromthumb, R_OK) == 0) {
file_mv(fromthumb, tothumb);
}
}
}
2001-12-02 20:49:36 +00:00
}
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);
}
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);
}
2004-05-06 14:16:03 +00:00
free(temp2);
#endif
2001-12-02 20:49:36 +00:00
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(frompath);
free(fromlink);
2002-12-09 21:11:43 +00:00
free(fromthumb);
2001-12-02 20:49:36 +00:00
free(topath);
free(tolink);
2002-12-09 21:11:43 +00:00
free(tothumb);
2001-12-02 19:17:14 +00:00
}