/* 
 * colldb.c
 *
 * $Author: dm3e $
 * $Date: 1993/01/08 21:43:50 $
 * $Revision: 1.2 $
 * $State: Exp $
 * $Locker: dm3e $
 */

/******************************************************************
 *        Copyright 1991 by Carnegie Mellon University
 *
 *                      All Rights Reserved
 *
 * Permission to use, copy, modify, and distribute this software and its
 * documentation for any purpose and without fee is hereby granted,
 * provided that the above copyright notice appear in all copies and that
 * both that copyright notice and this permission notice appear in
 * supporting documentation, and that the name of CMU not be
 * used in advertising or publicity pertaining to distribution of the
 * software without specific, written prior permission.
 *
 * CMU DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
 * ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL
 * CMU BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
 * ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
 * WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
 * ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
 * SOFTWARE.
 ******************************************************************/

#include "emtd.h"

/***************************************************************************
 * [exported] emtd_CollectionInTree
 *
 * Description:
 *      Checks to see if the collection specified in the request is associated
 *      with the tree that is specified in the request. If the collection is
 *      zero length or it is found in the file containing the list of
 *      collections in the tree, TRUE is returned. Otherwise FALSE is returned.
 *
 * Arguments:
 *      request - Pointer to the command request, and its information.
 *
 * Environment:
 *
 * History:
 *      Thu Jan  7 11:13:29 1993 - created by David Allen Markley
 *
 ***************************************************************************/
long emtd_CollectionInTree(request)
comreq_t request;
{
    FILE *treefd;
    char treepath[MAXPATHLEN];
    char buffer[MAXBUFSIZE];
    tree_t c_tree;
    char coll[MAXBUFSIZE];

    CheckAuth();
    if (0 == strlen(REQ_COLLECTION)) return TRUE;
    sprintf(coll, "%s\n", REQ_COLLECTION);
    sprintf(treepath, "%s/collections-%s", EMTD_DB_DIRECTORY, REQ_TREE);
    if (NULL == (treefd = fopen(treepath, "r"))) {
	emtd_Broadcast(request, EMTD_LOGERR,"Error opening %s (code = %ld).\n",
		       treepath, errno);
	emtd_Broadcast(request, EMTD_LOGERR, "Assuming %s collection not in %s tree.\n",
		       REQ_COLLECTION, REQ_TREE);
	return FALSE;
    }
    while (fgets (buffer, MAXBUFSIZE, treefd) != NULL) {
	if (!strcmp(buffer, coll)) {
	    Log ("Collection %s found in %s: %s", REQ_COLLECTION, REQ_TREE, buffer);
	    fclose(treefd);
	    return TRUE;
	}
    }
    fclose(treefd);
    if ((EMTD_CREATE != request->data->type) && (EMTD_MIGRATE != request->data->type)) {
	emtd_Broadcast(request, EMTD_LOGERR, "The %s collection not found in %s tree.\n",
		       REQ_COLLECTION, REQ_TREE);
	return FALSE;
    }
    for (c_tree = trees; NULL != c_tree; c_tree = c_tree->next) {
	if (!strcmp(c_tree->name, REQ_TREE)) continue;
	sprintf(treepath, "%s/collections-%s", EMTD_DB_DIRECTORY, c_tree->name);
	if (NULL == (treefd = fopen(treepath, "r"))) {
	    emtd_Broadcast(request, EMTD_LOGERR,"Error opening %s (code = %ld).\n",
			   treepath, errno);
	    emtd_Broadcast(request, EMTD_LOGERR, 
			   "Assuming %s collection might be in another tree.\n",
			   REQ_COLLECTION);
	    return FALSE;
	}
	while (fgets (buffer, MAXBUFSIZE, treefd) != NULL) {
	    if (!strcmp(buffer, coll)) {
		if (EMTD_MIGRATE == request->data->type) return TRUE;
		emtd_Broadcast(request, EMTD_LOGERR, 
			       "The %s collection is in the %s tree.\n",
			       REQ_COLLECTION, c_tree->name);
		fclose(treefd);
		return FALSE;
	    }
	}
	fclose(treefd);
    }
    if (EMTD_MIGRATE == request->data->type) {
	emtd_Broadcast(request, EMTD_LOGERR,
		       "The %s collection does not exist in any tree.\n",
		       REQ_COLLECTION);
	return FALSE;
    }
    return TRUE;
}

/***************************************************************************
 * [exported] emtd_AddCollectionToTree
 *
 * Description:
 *
 * Arguments:
 *
 * Environment:
 *
 * History:
 *      Thu Jan  7 13:59:49 1993 - created by David Allen Markley
 *
 ***************************************************************************/
long emtd_AddCollectionToTree(request)
comreq_t request;
{
    FILE *treefd;
    char treepath[MAXPATHLEN];
    char buffer[MAXBUFSIZE];
    char coll[MAXBUFSIZE];
    tree_t c_tree;

    CheckAuth();
    sprintf(coll, "%s\n", REQ_COLLECTION);
    sprintf(treepath, "%s/collections-%s", EMTD_DB_DIRECTORY, REQ_TREE);
    if (NULL == (treefd = fopen(treepath, "r"))) {
	emtd_Broadcast(request, EMTD_LOGERR, "Error opening %s (code = %ld).\n",
		       treepath, errno);
	emtd_Broadcast(request, EMTD_LOGERR, "%s collection not added to %s tree.\n",
		       REQ_COLLECTION, REQ_TREE);
	return errno;
    }
    while (fgets (buffer, MAXBUFSIZE, treefd) != NULL) {
	if (!strcmp(buffer, coll)) {
	    fclose(treefd);
	    return OK;
	}
    }
    fclose(treefd);
    if (NULL == (treefd = fopen(treepath, "a"))) {
	emtd_Broadcast(request, EMTD_LOGERR, "Error opening %s (code = %ld).\n",
		       treepath, errno);
	emtd_Broadcast(request, EMTD_LOGERR, "%s collection not added to %s tree.\n",
		       REQ_COLLECTION, REQ_TREE);
	return errno;
    }
    fprintf(treefd, "%s\n", REQ_COLLECTION);
    fclose(treefd);
    fflush(treefd);
    return OK;
}

/***************************************************************************
 * [exported] emtd_DeleteCollectionFromTree
 *
 * Description:
 *
 * Arguments:
 *
 * Environment:
 *
 * History:
 *      Fri Jan  8 11:44:20 1993 - created by David Allen Markley
 *
 ***************************************************************************/
long emtd_DeleteCollectionFromTree(request)
comreq_t request;
{
    FILE *treefd, *newfd;
    char treepath[MAXPATHLEN], newpath[MAXPATHLEN];
    char buffer[MAXBUFSIZE];
    char coll[MAXBUFSIZE];
    tree_t c_tree;

    CheckAuth();
    sprintf(coll, "%s\n", REQ_COLLECTION);
    sprintf(treepath, "%s/collections-%s", EMTD_DB_DIRECTORY, REQ_TREE);
    sprintf(newpath, "%s/collections-%s.NEW", EMTD_DB_DIRECTORY, REQ_TREE); 
    if (NULL == (treefd = fopen(treepath, "r"))) {
	emtd_Broadcast(request, EMTD_LOGERR, "Error opening %s (code = %ld).\n",
		       treepath, errno);
	emtd_Broadcast(request, EMTD_LOGERR, "%s collection not deleted from %s tree.\n",
		       REQ_COLLECTION, REQ_TREE);
	return errno;
    }
    if (NULL == (newfd = fopen(newpath, "w"))) {
	emtd_Broadcast(request, EMTD_LOGERR, "Error opening %s (code = %ld).\n",
		       newpath, errno);
	emtd_Broadcast(request, EMTD_LOGERR, "%s collection not deleted from %s tree.\n",
		       REQ_COLLECTION, REQ_TREE);
	fclose(treefd);
	return errno;
    }
    while (fgets (buffer, MAXBUFSIZE, treefd) != NULL) {
	if (!strcmp(buffer, coll)) continue;
	fputs(buffer, newfd);
	fflush(newfd);
    }
    fclose(treefd);
    fflush(newfd);
    fclose(newfd);
    if (NULL != rename(newpath, treepath)) {
	emtd_Broadcast(request, EMTD_LOGERR, "Error renaming %s (code = %ld).\n",
		       newpath, errno);
	emtd_Broadcast(request, EMTD_LOGERR, "%s collection not deleted from %s tree.\n",
		       REQ_COLLECTION, REQ_TREE);
	return errno;
    }
    return OK;
}
