예제 #1
0
파일: orient_xsb.c 프로젝트: jianqiao/code
void set_xsbinfo_dir () {
  struct stat *fileinfo = mem_alloc(1*sizeof(struct stat),LEAK_SPACE);
  char old_xinitrc[MAXPATHLEN], new_xinitrc[MAXPATHLEN],
    user_config_dir[MAXPATHLEN], user_arch_dir[MAXPATHLEN];
  int retcode;

  if (!fileinfo) {
    xsb_abort("No core memory to allocate stat structure.\n");
  }
  snprintf(xsbinfo_dir_gl, MAXPATHLEN, "%s%c.xsb", user_home_gl, SLASH);
  snprintf(old_xinitrc, MAXPATHLEN, "%s%c.xsbrc", user_home_gl, SLASH);
  snprintf(new_xinitrc, MAXPATHLEN, "%s%cxsbrc", xsbinfo_dir_gl, SLASH);
  snprintf(user_config_dir, MAXPATHLEN, "%s%cconfig", xsbinfo_dir_gl, SLASH);
  snprintf(user_arch_dir, MAXPATHLEN, "%s%c%s", user_config_dir, SLASH, FULL_CONFIG_NAME);

  /* Create USER_HOME/.xsb directory, if it doesn't exist. */
  check_create_dir(xsbinfo_dir_gl);
  check_create_dir(user_config_dir);
  check_create_dir(user_arch_dir);
  retcode = stat(old_xinitrc, fileinfo);

  if ((retcode == 0) && (stat(new_xinitrc, fileinfo) != 0)) {
    xsb_warn("It appears that you have an old-style `.xsbrc' file!\n           The XSB initialization file is now %s.\n           If your `.xinitrc' defines the `library_directory' predicate,\n           please consult the XSB manual for the new conventions.", new_xinitrc);
  }
  mem_dealloc(fileinfo,1*sizeof(struct stat),LEAK_SPACE);
}
예제 #2
0
static int csv_create_file (const char *filename, const data_set_t *ds)
{
	FILE *csv;
	int i;

	if (check_create_dir (filename))
		return (-1);

	csv = fopen (filename, "w");
	if (csv == NULL)
	{
		char errbuf[1024];
		ERROR ("csv plugin: fopen (%s) failed: %s",
				filename,
				sstrerror (errno, errbuf, sizeof (errbuf)));
		return (-1);
	}

	fprintf (csv, "epoch");
	for (i = 0; i < ds->ds_num; i++)
		fprintf (csv, ",%s", ds->ds[i].name);

	fprintf (csv, "\n");
	fclose (csv);

	return 0;
} /* int csv_create_file */
예제 #3
0
/*
 * Public functions
 */
int cu_rrd_create_file (const char *filename, /* {{{ */
                        const data_set_t *ds, const value_list_t *vl,
                        const rrdcreate_config_t *cfg)
{
    char **argv;
    int argc;
    char **rra_def;
    int rra_num;
    char **ds_def;
    int ds_num;
    int status = 0;

    if (check_create_dir (filename))
        return (-1);

    if ((rra_num = rra_get (&rra_def, vl, cfg)) < 1)
    {
        ERROR ("cu_rrd_create_file failed: Could not calculate RRAs");
        return (-1);
    }

    if ((ds_num = ds_get (&ds_def, ds, vl, cfg)) < 1)
    {
        ERROR ("cu_rrd_create_file failed: Could not calculate DSes");
        return (-1);
    }

    argc = ds_num + rra_num;

    if ((argv = (char **) malloc (sizeof (char *) * (argc + 1))) == NULL)
    {
        char errbuf[1024];
        ERROR ("cu_rrd_create_file failed: %s",
               sstrerror (errno, errbuf, sizeof (errbuf)));
        return (-1);
    }

    memcpy (argv, ds_def, ds_num * sizeof (char *));
    memcpy (argv + ds_num, rra_def, rra_num * sizeof (char *));
    argv[ds_num + rra_num] = NULL;

    status = srrd_create (filename,
                          (cfg->stepsize > 0) ? cfg->stepsize : vl->interval,
                          (vl->time > 10) ? (vl->time - 10) : vl->time,
                          argc, (const char **) argv);

    free (argv);
    ds_free (ds_num, ds_def);
    rra_free (rra_num, rra_def);

    if (status != 0)
    {
        WARNING ("cu_rrd_create_file: srrd_create (%s) returned status %i.",
                 filename, status);
    }
    else
    {
        DEBUG ("cu_rrd_create_file: Successfully created RRD file \"%s\".",
               filename);
    }

    return (status);
} /* }}} int cu_rrd_create_file */
예제 #4
0
/*
 * Public functions
 */
int cu_rrd_create_file(const char *filename, /* {{{ */
                       const data_set_t *ds, const value_list_t *vl,
                       const rrdcreate_config_t *cfg) {
  char **argv;
  int argc;
  char **rra_def = NULL;
  int rra_num;
  char **ds_def = NULL;
  int ds_num;
  int status = 0;
  time_t last_up;
  unsigned long stepsize;

  if (check_create_dir(filename))
    return -1;

  if ((rra_num = rra_get(&rra_def, vl, cfg)) < 1) {
    P_ERROR("cu_rrd_create_file failed: Could not calculate RRAs");
    return -1;
  }

  if ((ds_num = ds_get(&ds_def, ds, vl, cfg)) < 1) {
    P_ERROR("cu_rrd_create_file failed: Could not calculate DSes");
    rra_free(rra_num, rra_def);
    return -1;
  }

  argc = ds_num + rra_num;

  if ((argv = malloc(sizeof(*argv) * (argc + 1))) == NULL) {
    P_ERROR("cu_rrd_create_file failed: %s", STRERRNO);
    rra_free(rra_num, rra_def);
    ds_free(ds_num, ds_def);
    return -1;
  }

  memcpy(argv, ds_def, ds_num * sizeof(char *));
  memcpy(argv + ds_num, rra_def, rra_num * sizeof(char *));
  argv[ds_num + rra_num] = NULL;

  last_up = CDTIME_T_TO_TIME_T(vl->time);
  if (last_up <= 0)
    last_up = time(NULL);
  last_up -= 1;

  if (cfg->stepsize > 0)
    stepsize = cfg->stepsize;
  else
    stepsize = (unsigned long)CDTIME_T_TO_TIME_T(vl->interval);

  if (cfg->async) {
    status = srrd_create_async(filename, stepsize, last_up, argc,
                               (const char **)argv);
    if (status != 0)
      P_WARNING("cu_rrd_create_file: srrd_create_async (%s) "
                "returned status %i.",
                filename, status);
  } else /* synchronous */
  {
    status = lock_file(filename);
    if (status != 0) {
      if (status == EEXIST)
        P_NOTICE("cu_rrd_create_file: File \"%s\" is already being created.",
                 filename);
      else
        P_ERROR("cu_rrd_create_file: Unable to lock file \"%s\".", filename);
    } else {
      status =
          srrd_create(filename, stepsize, last_up, argc, (const char **)argv);

      if (status != 0) {
        P_WARNING("cu_rrd_create_file: srrd_create (%s) returned status %i.",
                  filename, status);
      } else {
        DEBUG("cu_rrd_create_file: Successfully created RRD file \"%s\".",
              filename);
      }
      unlock_file(filename);
    }
  }

  free(argv);
  ds_free(ds_num, ds_def);
  rra_free(rra_num, rra_def);

  return status;
} /* }}} int cu_rrd_create_file */
예제 #5
0
/*
 * Public functions
 */
int cu_rrd_create_file (const char *filename, /* {{{ */
    const data_set_t *ds, const value_list_t *vl,
    const rrdcreate_config_t *cfg)
{
  char **argv;
  int argc;
  char **rra_def;
  int rra_num;
  char **ds_def;
  int ds_num;
  int status = 0;
  time_t last_up;
  unsigned long stepsize;

  if (check_create_dir (filename))
    return (-1);

  if ((rra_num = rra_get (&rra_def, vl, cfg)) < 1)
  {
    ERROR ("cu_rrd_create_file failed: Could not calculate RRAs");
    return (-1);
  }

  if ((ds_num = ds_get (&ds_def, ds, vl, cfg)) < 1)
  {
    ERROR ("cu_rrd_create_file failed: Could not calculate DSes");
    return (-1);
  }

  argc = ds_num + rra_num;

  if ((argv = (char **) malloc (sizeof (char *) * (argc + 1))) == NULL)
  {
    char errbuf[1024];
    ERROR ("cu_rrd_create_file failed: %s",
        sstrerror (errno, errbuf, sizeof (errbuf)));
    return (-1);
  }

  memcpy (argv, ds_def, ds_num * sizeof (char *));
  memcpy (argv + ds_num, rra_def, rra_num * sizeof (char *));
  argv[ds_num + rra_num] = NULL;

  last_up = CDTIME_T_TO_TIME_T (vl->time);
  if (last_up <= 0)
    last_up = time (NULL);
  last_up -= 1;

  if (cfg->stepsize > 0)
    stepsize = cfg->stepsize;
  else
    stepsize = (unsigned long) CDTIME_T_TO_TIME_T (vl->interval);

  if (cfg->async)
  {
    status = srrd_create_async (filename, stepsize, last_up,
        argc, (const char **) argv);
    if (status != 0)
      WARNING ("cu_rrd_create_file: srrd_create_async (%s) "
          "returned status %i.",
          filename, status);
  }
  else /* synchronous */
  {
    status = srrd_create (filename, stepsize, last_up,
        argc, (const char **) argv);

    if (status != 0)
    {
      WARNING ("cu_rrd_create_file: srrd_create (%s) returned status %i.",
          filename, status);
    }
    else
    {
      DEBUG ("cu_rrd_create_file: Successfully created RRD file \"%s\".",
          filename);
    }
  }

  free (argv);
  ds_free (ds_num, ds_def);
  rra_free (rra_num, rra_def);

  return (status);
} /* }}} int cu_rrd_create_file */
예제 #6
0
void handle_backup_req(int sd, char* clientIP){
	char buf[BUFSIZE+100];
	char log_buf[STR_MAX];
	char user[STR_MAX];
	char filename[STR_MAX];
	char filepath[STR_MAX];
	char backup_dir_path[STR_MAX];
	char day[4];
	char date[12];
	char subject[STR_MAX];
	char cmd[STR_MAX];
	int response, len, fid;
	FILE* fp;
	int nsize, fsize, fsize2, fpsize, fpsize2;
	const int endQ = -1;
	int Q, tlen, tresp, i;
	tlen = 0; Q = -1;

	logger(SVR, "BACKUP REQUEST START");

	read(sd, &len, sizeof(len));
	len = ntohl(len);//
	read(sd, user, len);
	
	response = isMatch(user, clientIP);
	tresp = htonl(response);//
	write(sd, &tresp, sizeof(tresp));

	if(response){
		//get file
		read(sd, &len, sizeof(len));
		len = ntohl(len);//
		read(sd, day, len);

		read(sd, &len, sizeof(len));
		len = ntohl(len); //
		read(sd, date, len);

		check_create_dir(backup_dir_path);
		sprintf(backup_dir_path, "%s/%s", BACKUP_DIR, user);
		check_create_dir(backup_dir_path);
		sprintf(backup_dir_path, "%s/%s", backup_dir_path, day);
		check_create_dir(backup_dir_path);
		sprintf(log_buf, "Set Backup directory : %s", backup_dir_path);
		logger(SVR, log_buf);
		
		//check and create directory
		
		while(TRUE){
			read(sd, &len, sizeof(len));
			len = ntohl(len);//
			if(len == endQ) break;
			read(sd, filename, len);
#if DEBUE
			printf("%d %d filename : %s\n", len, endQ, filename);		
#endif
			sprintf(log_buf, "Get filename from %s[%s] : %s\n", user, clientIP, filename);
			logger(SVR, log_buf);

			sprintf(filepath, "%s/%s", backup_dir_path, filename);
			
			fp = fopen(filepath, "wb");

			if((len = recv(sd, &fsize2, sizeof(fsize), 0)) < 0){
				perror("recv");
				exit(1);
			}

			fsize = ntohl(fsize2);
			i = 0;
			nsize = 0;
			printf("file size : %d\n", fsize);
			
			while(nsize < fsize){
				//recv(sd, &fpsize2, sizeof(fpsize2), 0);
				//fpsize = ntohl(fpsize2);
				//printf("fpsize : %d\n", fpsize);
		
				if((len = recv(sd, buf, 4048, 0)) <= 0){
					perror("recv");
					exit(1);
				}
				nsize += len;
				printf("file size = %d, seq = %d, now_size = %d, recv_size = %d\n", fsize, i++, nsize, len);

				if(nsize <= fsize)	fwrite(buf, 1, len, fp);
				else						fwrite(buf, 1, len - sizeof(int), fp);
			}
			
			/*((char*)Q)[0] = buf[len-sizeof(int)];
			((char*)Q)[1] = buf[len-sizeof(int)+1];
			((char*)Q)[2] = buf[len-sizeof(int)+2];
			((char*)Q)[3] = buf[len-sizeof(int)+3];*/
			memcpy((char*)&Q, &buf[len-4], sizeof(Q));
			printf("Q is  %d\n", Q);
			//printf("%d %d %d %d\n", buf[len-4], buf[len-3], buf[len-2], buf[len-1]);
			
			//printf("%d\n", Q);
			fclose(fp);

			//if(Q == endQ) break;
			//recv(sd, &Q, sizeof(Q), 0);
			//Q = ntohl(Q);
			//printf("%d\n", Q);
			//exit(1);
			/*
			if( (fid = open(filepath, O_CREAT | O_WRONLY | O_TRUNC, 0755)) < 0 ){
				sprintf(log_buf, "file open fail : %s", filepath);
				logger(ERR, log_buf);
				exit(1);
			}
			
			i = 0;
			while(TRUE){
				while((tlen = recv(sd, &len, sizeof(len), 0)) != sizeof(tlen)){
					perror("recv");
					//exit(1);
				}
				printf("%d %d %d\n", i++, len, ntohl(len));
				tlen = ntohl(len);

				if(tlen == endQ) break;

				while((len = recv(sd, buf, tlen, 0)) != tlen){
					printf("%d %d\n", len, tlen);
					perror("recv2");
					exit(1);
				}
				if(write(fid, buf, len) < 0){
					perror("write");
					exit(1);
				}

			}

			
			i = 0;
			while(TRUE){
				if(read(sd, &len, sizeof(len)) < 0){
					perror("read");
					exit(1);
				}
				printf("%d %d %d\n", i++, len, ntohl(len));
				len = ntohl(len);//
#if DEBUG
				//printf("len %d %d\n", len, endQ);
#endif
				if(len == endQ) break;

				if((len = read(sd, buf, len)) < 0){
					perror("read2");
					exit(1);
				}
				if(write(fid, buf, len) < 0){
					perror("write");
					exit(1);
				}
			}
		
			if(fid) close(fid);
			sprintf(log_buf, "file[%s] is uploaded.", filepath);
			logger(SVR, log_buf);*/
		}
		/*	
			sprintf(log_buf, "Call: HTMLgen %s %s", user, date);
			logger(SVR, log_buf);

			sprintf(cmd, "./HTMLgen %s %s", user, date);
#if DEBUG
			printf("%s\n", cmd);
#endif	
			system(cmd);

			sprintf(cmd, "./mail_sender %s %s %s", user, clientIP, date);
			system(cmd);

			sprintf(subject, "The backup of %s is finish.", user);
			sprintf(cmd, "mail -s \"$(echo -e \"%s\\nContent-Type: text/html\")\" %s < %s", subject, MAIL_RCPT, MAIL_PATH); 

			system(cmd);
		}*/
	}

	logger(SVR, "BACKUP REQUEST END");
	if(sd) close(sd);
	exit(1);

}