<commit_before>/******************************************************************************
*******************************************************************************
**
**  Copyright (C) 2005 Red Hat, Inc.  All rights reserved.
**
**  This copyrighted material is made available to anyone wishing to use,
**  modify, copy, or redistribute it subject to the terms and conditions
**  of the GNU General Public License v.2.
**
*******************************************************************************
******************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <string.h>

#include <endian.h>
#include <byteswap.h>

#include "old_parser.h"

#define DEFAULT_BBS 16384 /* default basic block size */


static int upgrade_file_archive(char *location);
static int upgrade_device_archive(char *location);

int upgrade(char *location){
  struct stat stat_buf;

  if(stat(location, &stat_buf)){
    fprintf(stderr, "Unable to stat %s: %s\n", location, strerror(errno));
    return -errno;
  }

  if(S_ISBLK(stat_buf.st_mode)){
    return upgrade_device_archive(location);
  } else {
    return upgrade_file_archive(location);
  }
}

static int _upgrade_file_archive(int fd){
  int error = 0;
  ccs_node_t *cluster_cn = NULL;
  ccs_node_t *fence_cn = NULL;
  ccs_node_t *nodes_cn = NULL;
  ccs_node_t *tmp_cn;

  if((error = parse_ccs_file(fd, &cluster_cn, "cluster.ccs"))){
    return error;
  }

  if((error = parse_ccs_file(fd, &nodes_cn, "nodes.ccs"))){
    return error;
  }

  if((error = parse_ccs_file(fd, &fence_cn, "fence.ccs"))){
    return error;
  }

  tmp_cn = find_ccs_node(cluster_cn, "cluster/name", '/');
  if(!tmp_cn || !tmp_cn->v){
    fprintf(stderr, "Unable to find cluster name.\n");
    error = -EINVAL;
    goto fail;
  }

  printf("<cluster name=\"%s\" config_version=\"1\">\n", tmp_cn->v->v.str);

  /* No cman on upgrade
  printf("\n<cman>\n");
  printf("</cman>\n");
  */

  tmp_cn = find_ccs_node(cluster_cn, "cluster/lock_gulm/servers", '/');
  printf("\n<gulm>\n");
  if(tmp_cn && tmp_cn->v){
    ccs_value_t *server;
    for(server = tmp_cn->v; server; server = server->next)
      printf("    <lockserver name=\"%s\"/>\n", server->v.str);
  }
  printf("</gulm>\n");

  printf("\n<clusternodes>\n");

  for(tmp_cn = nodes_cn->child; tmp_cn; tmp_cn = tmp_cn->sib){
    ccs_node_t *fence, *method, *device, *params;

    printf("\n  <clusternode name=\"%s\" votes=\"1\">\n", tmp_cn->key);
    for(fence = tmp_cn->child; fence; fence = fence->sib){
      if(!strcmp(fence->key, "fence")){
	printf("    <fence>\n");
	for(method = fence->child; method; method = method->sib){
	  printf("      <method name=\"%s\">\n", method->key);
	  for(device = method->child; device; device=device->sib){
	    printf("        <device name=\"%s\"", device->key);
	    for(params = device->child; params; params = params->sib){
	      printf(" %s=\"%s\"", params->key, params->v->v.str);
	    }
	    printf("/>\n");
	  }
	  printf("      </method>\n");
	}
	printf("    </fence>\n");
      }
    }
    printf("  </clusternode>\n");
  }

  printf("\n</clusternodes>\n");

  printf("\n<fencedevices>\n");
  for(tmp_cn = fence_cn->child; tmp_cn; tmp_cn = tmp_cn->sib){
    ccs_node_t *params;

    printf("  <fencedevice name=\"%s\"", tmp_cn->key);
    for(params = tmp_cn->child; params; params = params->sib){
      printf(" %s=\"%s\"", params->key, params->v->v.str);
    }
    printf("/>\n");
  }

  printf("\n</fencedevices>\n");
<commit_msg>- ccs_tool can seg fault on upgrade bug 186121<commit_after>/******************************************************************************
*******************************************************************************
**
**  Copyright (C) 2005 Red Hat, Inc.  All rights reserved.
**
**  This copyrighted material is made available to anyone wishing to use,
**  modify, copy, or redistribute it subject to the terms and conditions
**  of the GNU General Public License v.2.
**
*******************************************************************************
******************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <string.h>

#include <endian.h>
#include <byteswap.h>

#include "old_parser.h"

#define DEFAULT_BBS 16384 /* default basic block size */


static int upgrade_file_archive(char *location);
static int upgrade_device_archive(char *location);

int upgrade(char *location){
  struct stat stat_buf;

  if(stat(location, &stat_buf)){
    fprintf(stderr, "Unable to stat %s: %s\n", location, strerror(errno));
    return -errno;
  }

  if(S_ISBLK(stat_buf.st_mode)){
    return upgrade_device_archive(location);
  } else {
    return upgrade_file_archive(location);
  }
}

static int _upgrade_file_archive(int fd){
  int error = 0;
  ccs_node_t *cluster_cn = NULL;
  ccs_node_t *fence_cn = NULL;
  ccs_node_t *nodes_cn = NULL;
  ccs_node_t *tmp_cn;

  if((error = parse_ccs_file(fd, &cluster_cn, "cluster.ccs"))){
    return error;
  }

  if((error = parse_ccs_file(fd, &nodes_cn, "nodes.ccs"))){
    return error;
  }

  if((error = parse_ccs_file(fd, &fence_cn, "fence.ccs"))){
    return error;
  }

  tmp_cn = find_ccs_node(cluster_cn, "cluster/name", '/');
  if(!tmp_cn || !tmp_cn->v){
    fprintf(stderr, "Unable to find cluster name.\n");
    error = -EINVAL;
    goto fail;
  }

  printf("<cluster name=\"%s\" config_version=\"1\">\n", tmp_cn->v->v.str);

  /* No cman on upgrade
  printf("\n<cman>\n");
  printf("</cman>\n");
  */

  tmp_cn = find_ccs_node(cluster_cn, "cluster/lock_gulm/servers", '/');
  printf("\n<gulm>\n");
  if(tmp_cn && tmp_cn->v){
    ccs_value_t *server;
    for(server = tmp_cn->v; server; server = server->next)
      printf("    <lockserver name=\"%s\"/>\n", server->v.str);
  }
  printf("</gulm>\n");

  printf("\n<clusternodes>\n");

  for(tmp_cn = nodes_cn->child; tmp_cn; tmp_cn = tmp_cn->sib){
    ccs_node_t *fence, *method, *device, *params;

    printf("\n  <clusternode name=\"%s\" votes=\"1\">\n", tmp_cn->key);
    for(fence = tmp_cn->child; fence; fence = fence->sib){
      if(!strcmp(fence->key, "fence")){
	printf("    <fence>\n");
	for(method = fence->child; method; method = method->sib){
	  printf("      <method name=\"%s\">\n", method->key);
	  for(device = method->child; device; device=device->sib){
	    printf("        <device name=\"%s\"", device->key);
	    for(params = device->child; params; params = params->sib){
	      if (params->v->type == CCS_STRING)
		printf(" %s=\"%s\"", params->key, params->v->v.str);
	      else if (params->v->type == CCS_INT)
		printf(" %s=\"%d\"", params->key, params->v->v.i);
	      else if (params->v->type == CCS_FLOAT)
		printf(" %s=[DECIMAL NOT CONVERTED]", params->key);
	      else
		printf(" %s=[BAD TYPE CONVERSION]", params->key);
	    }
	    printf("/>\n");
	  }
	  printf("      </method>\n");
	}
	printf("    </fence>\n");
      }
    }
    printf("  </clusternode>\n");
  }

  printf("\n</clusternodes>\n");

  printf("\n<fencedevices>\n");
  for(tmp_cn = fence_cn->child; tmp_cn; tmp_cn = tmp_cn->sib){
    ccs_node_t *params;

    printf("  <fencedevice name=\"%s\"", tmp_cn->key);
    for(params = tmp_cn->child; params; params = params->sib){
      printf(" %s=\"%s\"", params->key, params->v->v.str);
    }
    printf("/>\n");
  }

  printf("\n</fencedevices>\n");

{'fraction_non_alphanumeric': 0.26445410246279466, 'fraction_numerical': 0.004214408007375214, 'mean_word_length': 3.8741976893453147, 'pattern_counts': {'":': 0, '<': 59, '<?xml version=': 0, '>': 134, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6574799', 'n_tokens_mistral': 2773, 'n_tokens_neox': 2667, 'n_words': 556}