/*
 * tools/lib/vg_create_dir_and_group.c
 *
 * Copyright (C) 1997 - 2000  Heinz Mauelshagen, Germany
 *
 * May 1997
 * January,December 1998
 * January,February 1999
 *
 *
 * This LVM library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This LVM library 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
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this LVM library; if not, write to the Free
 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
 * MA 02111-1307, USA
 *
 */

/*
 *
 * Changelog
 *
 *    11/01/1998 - added deletion of volume group special file directory
 *    13/12/1998 - corrected flow
 *    26/01/1999 - made volume group directory prefix a preprocessor option
 *    16/02/1999 - changed special generation to new scheme
 *
 */

#include <liblvm.h>

int vg_create_dir_and_group ( vg_t *vg) {
   int ret = 0;
   char name[NAME_LEN];

#ifdef DEBUG
   debug ( "vg_create_dir_and_group -- CALLED\n");
#endif

   if ( vg == NULL || vg_check_name ( vg->vg_name) < 0) return -LVM_EPARAM;

   sprintf ( name, "rm -fr " LVM_DIR_PREFIX "%s%c", vg->vg_name, 0);
   system ( name);
   mkdir ( LVM_DIR_PREFIX, 0555);
   sprintf ( name, LVM_DIR_PREFIX "%s%c", vg->vg_name, 0);
   if ( mkdir ( name, 0555) == -1) ret = -LVM_EVG_CREATE_DIR_AND_GROUP_MKDIR;
   else {
      if ( chmod ( name, 0555) == -1)
         ret = -LVM_EVG_CREATE_DIR_AND_GROUP_CHMOD_DIR;
      else {
         strcat ( name, "/group");
         if ( mknod ( name, S_IFCHR | S_IRUSR | S_IWUSR | S_IRGRP,
                      ( LVM_CHAR_MAJOR << MINORBITS) +
                      vg->vg_number) == -1)
            ret = -LVM_EVG_CREATE_DIR_AND_GROUP_MKNOD;
         else if ( chmod ( name, 0640) == -1) 
                 ret = -LVM_EVG_CREATE_DIR_AND_GROUP_CHMOD_GROUP;
      }
   }

#ifdef DEBUG
   debug ( "vg_create_dir_and_group -- LEAVING\n");
#endif
   return ret;
}
