コード例 #1
0
ファイル: gpmondb.c プロジェクト: karthijrk/gpdb
void gpdb_import_alert_log(apr_pool_t *pool)
{
	// Get alert log files to be imported.
	apr_array_header_t* tail_files = apr_array_make(pool, 10, sizeof(char*));
	apr_array_header_t* success_append_files = apr_array_make(pool, 10, sizeof(char*));
	get_alert_log_tail_files(tail_files, pool);

	// Create or truncate stage file.
	char *dst_file = apr_pstrcat(pool, GPMON_LOG, "/", GPMON_ALERT_LOG_STAGE, NULL);
	apr_status_t status = truncate_file(dst_file, pool);
	if (status != APR_SUCCESS)
	{
	    gpmon_warningx(FLINE, 0, "failed truncating stage file:%s", dst_file);
	    return;
	}

	// Append alert log tail file to stage file
	void *tail_file = NULL;
	while ((tail_file = apr_array_pop(tail_files)))
	{
		char *filename = *(char**)tail_file;
		void *success_file_slot = apr_array_push(success_append_files);
		if (!success_file_slot)
		{
			gpmon_warningx(
				FLINE, 0, "failed appending file:%s to stage file:%s due to out of memory",
				filename, dst_file);
			continue;
		}
		(*(char**)success_file_slot) = NULL;

	    status = apr_file_append(filename, dst_file, APR_FILE_SOURCE_PERMS, pool);
	    if (status != APR_SUCCESS)
	    {
			gpmon_warningx(FLINE, status, "failed appending file:%s to stage file:%s", filename, dst_file);
			continue;
		}
	    else
	    {
			(*(char**)success_file_slot) = filename;
	    	TR1(("success appending file:%s to stage file:%s\n", filename, dst_file));
	    }
	}

	// Insert tail file to history table.
	if (gpdb_insert_alert_log())
	{
		// Delete tail file
		gpdb_remove_success_files(success_append_files, pool);
		truncate_file(dst_file, pool);
	}
}
コード例 #2
0
ファイル: dir.c プロジェクト: bmiro/EmoFS
/** Borra l'entrada de directori especificada y en cas de que sigui l'últim
 * enllaç existent borra el propi fitxer/directori. Té control de concurrència.
 * @path: ruta del fitxer/directori
 * @return:  0 si correctament
 *	     -1 en cas d'error.
 */
int emofs_unlink(const char *path) {
	int p_inode = 0;
	int p_dir_inode = 0;
	int p_entry = 0;

	emofs_inode inode;
	emofs_dir_entry *last_dir_entry;

	if(!mutex){
		emofs_sem_get(&mutex);
	}
	emofs_sem_wait(mutex);
	if (emofs_find_entry(path, &p_dir_inode, &p_inode, &p_entry) == -1) {
		puts("EmoFS_UnLink: El fitxer o directori font no existeix");
		emofs_sem_signal(mutex);
		return -1;
	}

	read_inode(p_dir_inode, &inode);
	/* Borram entrada de directori */
	if (inode.size > sizeof(emofs_dir_entry)) {
		/* Hi ha més entrades posam l'última al lloc de la que volem 
		 * borrar. */
		read_file(p_dir_inode, (void *)&last_dir_entry, \
			  inode.size-sizeof(emofs_dir_entry), \
			  sizeof(emofs_dir_entry));
		write_file(p_dir_inode, last_dir_entry, \
			   p_entry*sizeof(emofs_dir_entry), \
			   sizeof(emofs_dir_entry));
	}

	free(last_dir_entry);

	/* Truncam per l'ultima entrada, si sols hi ha la que volem borrar be
	 * sino llevam l'ultima que ara estara copiada al lloc de la que voliem
	 * borrar. */
	truncate_file(p_dir_inode, inode.size-sizeof(emofs_dir_entry));
	read_inode(p_inode, &inode);
	if (inode.link_count == 0) {
		/* Hem de alliberar blocs de dades i inode */
		truncate_file(p_inode, 0);
		free_inode(p_inode);
	} else {
		inode.link_count--;
		write_inode(p_inode, &inode);
	}
	emofs_sem_signal(mutex);
	return 0;
}
コード例 #3
0
ファイル: file_wrapper.hpp プロジェクト: Aantonb/gotham
inline void file_wrapper::truncate(offset_t length)
{
   if(!truncate_file(m_handle, length)){
      error_info err(system_error_code());
      throw interprocess_exception(err);
   }
}
コード例 #4
0
ファイル: yfs_04_25_3.c プロジェクト: rishika23/lab3
int Server_UnLink(int inum,char *name)
{	int inode_num,i;
	int dir_inum=inum;
	int last_slash=-1;
	struct inode curr_inode;
	char dir_path[MAXPATHNAMELEN];
	
	if(inum<1||inum>max_inode_num||name==NULL)
		return ERROR;

	inode_num=LookUp(name,inum,0);
	if((inode_num==ERROR)||(inode_num==0))
	{	printf("\nERROR: Path Not Found!");
		return ERROR;
	}

	GetInode(inode_num,&curr_inode);
	if(curr_inode.type==INODE_DIRECTORY)
	{	printf("\nERROR: Cannot Unlink a Directory!");
		return ERROR;
	}

	for(i=0;i<strlen(name);i++)
		if(name[i]=='/')
			last_slash=i;
	if(last_slash==-1)
		dir_inum=inum;
	else if (last_slash==0)
		dir_inum=ROOTINODE;
	else
	{
		strncpy(dir_path,name,last_slash);
		dir_path[last_slash]='\0';
		dir_inum=LookUp(dir_path,inum,0);
	}

	if(curr_inode.nlink>1)
	{
		curr_inode.nlink=curr_inode.nlink-1;
		
	}
	/*Else Delete the file inode and all the blocks allocated to it*/
	else 
	{
		truncate_file(&curr_inode);
		curr_inode.type=INODE_FREE;
		Add_free_inode(inode_num);
		
	}
	Delete_from_dir(dir_inum,inode_num);
	write_inode(inode_num,&curr_inode);
	return 0;

}
コード例 #5
0
//!
//! Initialize the IP Set handler structure
//!
//! @param[in] ipsh pointer to the IP set handler structure
//! @param[in] cmdprefix a string pointer to the prefix to use to run commands
//!
//! @return
//!
//! @see ipt_handler_init()
//!
//! @pre
//!    - The ipsh pointer should not be NULL
//!     - We should be able to create temporary files on the system
//!     - We should be able to execute ebtables commands.
//!
//! @post
//!     - Temporary files on disk: /tmp/ips_file-XXXXXX
//!     - If cmdprefix was provided, the table's cmdprefix field will be set with it
//!
//! @note
//!     - Once temporary file is initialized the filename will be reused throughout the process
//!       lifetime. The file will be truncated/created on each successive calls to the *_handler_init()
//!       method.
//!
int ips_handler_init(ips_handler * ipsh, const char *cmdprefix)
{
    int fd;
    char cmd[EUCA_MAX_PATH];
    char sTempFileName[EUCA_MAX_PATH] = "";

    if (!ipsh) {
        LOGERROR("invalid input\n");
        return (1);
    }

    if (ipsh->init) {
        snprintf(sTempFileName, EUCA_MAX_PATH, ipsh->ips_file);
        if (truncate_file(sTempFileName)) {
            return (1);
        }
        LOGDEBUG("Using already allocated temporary filename: %s\n", sTempFileName);
    } else {
        // Initialize new temp filename, only done once. 
        snprintf(sTempFileName, EUCA_MAX_PATH, "/tmp/ips_file-XXXXXX");
        if ((fd = safe_mkstemp(sTempFileName)) < 0) {
            LOGERROR("cannot create tmpfile '%s': check permissions\n", sTempFileName);
            return (1);
        }
        if (chmod(sTempFileName, 0600)) {
            LOGWARN("chmod failed: was able to create tmpfile '%s', but could not change file permissions\n", sTempFileName);
        }

        LOGDEBUG("Using Newly created temporary filename: %s\n", sTempFileName);
        close(fd);
    }

    bzero(ipsh, sizeof(ips_handler));

    snprintf(ipsh->ips_file, EUCA_MAX_PATH, sTempFileName);

    if (cmdprefix) {
        snprintf(ipsh->cmdprefix, EUCA_MAX_PATH, "%s", cmdprefix);
    } else {
        ipsh->cmdprefix[0] = '\0';
    }
    
    // test required shell-outs
    snprintf(cmd, EUCA_MAX_PATH, "%s ipset -L >/dev/null 2>&1", ipsh->cmdprefix);
    if (system(cmd)) {
        LOGERROR("could not execute required shell out '%s': check command/permissions\n", cmd);
        return (1);
    }

    ipsh->init = 1;
    return (0);
}
コード例 #6
0
ファイル: truncate.c プロジェクト: noahhl/statsd-server
int main(int argc, char *argv[])
{

  printf("Booting up...\n");
  if( argc != 3 || fopen(argv[1], "r") == NULL) {
    printf("Usage: truncate path/to/config/file aggregationLevel\n");
    printf("Exiting...\n");
    return 1;
  }

  statsdConfig *config = loadStatsdConfig(argv[1]);
  redisReply *reply;
  redisContext *redisInstance = redisConnect(config->redis_host, config->redis_port);
  if (redisInstance->err) {
    printf("Error: %s\n", redisInstance->errstr);
    exit(1);
  }

  int since = calculate_truncation_threshold(argv[2], config->retention);
  if (since == 0) {
    printf("Aggregation specified is invalid. Aborting...\n");
    return 1;
  }

  printf("Starting to truncate the %s level of aggregations since %d.\n", argv[2], since);

  /* PING server */
  printf("Pinging redis...");
  reply = redisCommand(redisInstance,"PING");
  printf("%s\n", reply->str);
  freeReplyObject(reply);

  reply = redisCommand(redisInstance,"SMEMBERS datapoints");
  if (reply->type == REDIS_REPLY_ARRAY) {
    int i;
    for (i=0; i < reply->elements; i++) {
      char metric[256];
      sprintf(metric, "%s:%s", reply->element[i]->str, argv[2]);
      char *filename = calculate_statsd_filename(metric, config->db_path);
      char timestamp[10];
      sprintf(timestamp, "%d", since);
      #ifdef DEBUG
        printf("truncating %s\n", filename);
      #endif
      truncate_file(filename, timestamp);
    }
  }
  freeReplyObject(reply);
  redisFree(redisInstance);
  return 0;
}
コード例 #7
0
ファイル: gemdos.c プロジェクト: denizt/hatari
int main()
{
	clear_screen();

	copy_file(INPUT_FILE, OUTPUT_FILE);
	write2console(OUTPUT_FILE);
	write2printer(OUTPUT_FILE);
	write2serial(OUTPUT_FILE);
	truncate_file(OUTPUT_FILE);

	write_midi();
	wait_enter();
	return 0;
}
コード例 #8
0
ファイル: testtruncate.c プロジェクト: linuxfs/tools
/* main */
int main(int argc, char **argv)
{
	int fd, i;

	i = 0;
	for (i = 0; table[i].title != NULL; i++)
	{
		title(table[i].title);
		if ((fd = open_file(table[i].title)) < 0) {
			continue;
		}
		truncate_file(fd, table[i].size);
	}
	return 0;
}
コード例 #9
0
static char * test_truncating() {
   int i;
   FILE *file = fopen("/tmp/test", "w");
   for(i = 0; i < 10; i++) {
      fprintf(file, "%d 123456\n", 100000 + i * 30);
   }
   fclose(file);
   truncate_file("/tmp/test", "100120");
   file = fopen("/tmp/test", "r");
   char line[256];
   fgets(line, sizeof line, file);
   mu_assert("Truncation leaves the first timestamp greater in the file.", 
            strcmp(line, "100150 123456\n") == 0);  
   return 0;
}
コード例 #10
0
ファイル: vtape_dev.c プロジェクト: prelegalwonder/bacula
/*
 *  +---+---------+---+------------------+---+-------------------+
 *  |00N|  DATA   |0LN|   DATA           |0LC|     DATA          |
 *  +---+---------+---+------------------+---+-------------------+
 *
 *  0 : zero
 *  L : Last FileMark offset
 *  N : Next FileMark offset
 *  C : Current FileMark Offset
 */
int vtape::weof()
{
   ASSERT(online);
   ASSERT(current_file >= 0);

#if 0
   if (atEOT) {
      errno = ENOSPC;
      current_block = -1;
      return -1;
   }
#endif

   if (!atEOD) {
      truncate_file();             /* nothing after this point */
   }

   last_FM = cur_FM;
   cur_FM = lseek(fd, 0, SEEK_CUR); // current position

   /* update previous next_FM  */
   lseek(fd, last_FM + sizeof(uint32_t)+sizeof(boffset_t), SEEK_SET);
   ::write(fd, &cur_FM, sizeof(boffset_t));
   lseek(fd, cur_FM, SEEK_SET);

   next_FM = 0;

   uint32_t c=0;
   ::write(fd, &c,       sizeof(uint32_t)); // EOF
   ::write(fd, &last_FM, sizeof(last_FM));  // F-1
   ::write(fd, &next_FM, sizeof(next_FM));  // F   (will be updated next time)

   current_file++;
   current_block = 0;

   needEOF = false;
   atEOD = false;
   atBOT = false;
   atEOF = true;

   last_file = MAX(current_file, last_file);

   Dmsg4(dbglevel, "Writing EOF %i:%i last=%lli cur=%lli next=0\n",
         current_file, current_block, last_FM, cur_FM);

   return 0;
}
コード例 #11
0
ファイル: vtape.c プロジェクト: AlD/bareos
/*
 * Write a variable block of count size.
 * block = vtape_header + data
 * vtape_header = sizeof(data)
 * if vtape_header == 0, this is a EOF
 */
ssize_t vtape::d_write(int, const void *buffer, size_t count)
{
   ASSERT(online);
   ASSERT(current_file >= 0);
   ASSERT(count > 0);
   ASSERT(buffer);

   ssize_t nb;
   Dmsg3(dbglevel*2, "write len=%i %i:%i\n",
         count, current_file,current_block);

   if (atEOT) {
      Dmsg0(dbglevel, "write nothing, EOT !\n");
      errno = ENOSPC;
      return -1;
   }

   if (!atEOD) {                /* if not at the end of the data */
      truncate_file();
   }

   if (current_block != -1) {
      current_block++;
   }

   atBOT = false;
   atEOF = false;
   atEOD = true;                /* End of data */

   needEOF = true;              /* next operation need EOF mark */

   uint32_t size = count;
   ::write(fd, &size, sizeof(uint32_t));
   nb = ::write(fd, buffer, count);

   if (nb != (ssize_t)count) {
      atEOT = true;
      Dmsg2(dbglevel,
            "Not enough space writing only %i of %i requested\n",
            nb, count);
   }

   update_pos();

   return nb;
}
コード例 #12
0
ファイル: yfs_04_25_3.c プロジェクト: rishika23/lab3
int Server_Create(char *pathname,int inode_num)
{
	
	int i;
	int last_slash=-1;
	int num;
	int res=ROOTINODE;
	int resF;
	int len=strlen(pathname);
	char *dir_path;
	struct inode *curr_inode;
	struct inode *dir_inode;
	
	if(inode_num<1||inode_num>max_inode_num||pathname==NULL)
		return ERROR;

	printf("\nReached Inside Create_file with len=%d\n",len);

	dir_path=(char *)malloc(len+1);

	for(i=0;i<len;i++)
		if(pathname[i]=='/')
			last_slash=i;
	

	printf("\nReached in Create_file 1");


	if(last_slash>0)
	{	
		printf("\nReached in Create_file 2");
		
		strncpy(dir_path,pathname,last_slash);
		
		dir_path[last_slash]='\0';
		
		res=LookUp(dir_path,inode_num,0);
		
		if((res==ERROR)||(res==0))
		{
			printf("\nERROR: Path %s does not exist ",dir_path);
			free(dir_path);
			return ERROR;
		}
	}
	
	printf("\nReached in Create_file 3 with dinode=%d",res);
	
	strncpy(dir_path,&pathname[last_slash+1],len-last_slash-1);
	
	dir_path[len-last_slash-1]=0;
	
	printf("\nValue of dir_path is %s",dir_path);
	
	resF=LookUp(dir_path,res,0);
	
	printf("\nReached in Create_file 4 with resF as %d",resF);
	
	if(resF==ERROR)
	{
		printf("\nERROR: Path %s does not exist ",dir_path);
		free(dir_path);
		return ERROR;
	}

	curr_inode=(struct inode *)malloc(INODESIZE);
	
	if(resF==0)
	{
		inode_num=Get_free_inode();
		
		if(inode_num==0)
		{
			printf("\nERROR: Not enough Inodes left for creating file");
			free(curr_inode);
			return ERROR;
		}
		
		curr_inode->type=INODE_REGULAR;
		curr_inode->size=0;
		curr_inode->nlink=1;

		write_inode(inode_num,curr_inode);
		
		printf("\nValue of inode_num is %d",inode_num);
		printf("\nValue of pathname is %s",pathname);
		
		/*making a directory entry in current directory*/
		PutInDir(res,inode_num,dir_path);	
			
		
		free(curr_inode);
		
		printf("Leaving Create_file\n");

		return inode_num;
	}
	
	GetInode(resF,curr_inode);
	if(curr_inode->type!=INODE_REGULAR)
	{
		free(curr_inode);
		free(dir_path);
		printf("\nval od file type is %d",curr_inode->type);
		printf("\nERROR : Cannot Create the file!!\nLeaving Create File");

		return ERROR;
	
	}
	truncate_file(curr_inode);
	write_inode(resF,curr_inode);
	free(curr_inode);
	free(dir_path);

	printf("Leaving Create_file\n");
	return resF;
	

}
コード例 #13
0
ファイル: ipt_handler.c プロジェクト: jbreiding/eucalyptus
/**
 * Initialize an IP table handler structure
 *
 * @param pIpt [in] pointer to the IP table handler structure
 * @param psCmdPrefix [in] a constant pointer to a string containing the prefix for EUCA commands
 * @param psPreloadPath [in] a constant pointer to a string containing the path to the IP table preload file
 *
 * @return 0 on success or 1 if any failure occurred
 *
 * @see ipt_handler_free()
 *
 * @pre
 *     - The pIpt pointer should not be NULL
 *     - We should be able to create temporary files on the system
 *     - We should be able to execute the iptables commands
 *
 * @post
 *     On success, the IP table structure will be initialized with the following:
 *     - The ipt_file will point to a temporary file under /tmp/ipt_file-XXXXXX
 *     - If psCmdPrefix was provided, the table's cmdprefix field will be set with it
 *     - If psPreloadPath was provided, the structure's preloadPath will be set with it
 *     -
 *
 * @note
 *     - Once temporary file is initialized the filename will be reused throughout the process
 *       lifetime. The file will be truncated/created on each successive calls to the *_handler_init()
 *       method. 
 */
int ipt_handler_init(ipt_handler *pIpt, const char *psCmdPrefix, const char *psPreloadPath) {
    int fd = 0;
    char sTempFileName[EUCA_MAX_PATH] = "";  // Used to temporarily hold name while we zero out the struct

    // Make sure our pointers are valid
    if (!pIpt) {
        return (1);
    }

    //
    // Initialize the temporary file *ONCE* per process execution
    // This handler init function is called many times, but the temporary file
    // will always be the same for the associated handler struct.
    //
    if (pIpt->init) {
        //
        // Copy filename out of the current ipt_handler struct.
        //
        snprintf(sTempFileName, EUCA_MAX_PATH, pIpt->ipt_file);
        
        //  Truncate the file to ensure we're dealing with a clean slate.
        if (truncate_file(sTempFileName)){
           return (1);
        }
        LOGDEBUG("Using already allocated temporary filename: %s\n", sTempFileName);
        
    } else {
        //
        // Initialize a new temporaty file name
        //
        snprintf(sTempFileName, EUCA_MAX_PATH, "/tmp/ipt_file-XXXXXX");
        if ((fd = safe_mkstemp(sTempFileName)) < 0) {
            LOGERROR("cannot create tmpfile '%s': check permissions\n", sTempFileName);
            return (1);
        }

        // Check to see if we can set the permissions to 0600
        if (chmod(sTempFileName, 0600) < 0) {
            LOGWARN("chmod failed: ipt_file '%s' errno: %d\n", sTempFileName, errno);
        }

        LOGDEBUG("Using newly created temporary filename: %s\n", sTempFileName);
        close(fd);
    }

    // Empty this structure
    bzero(pIpt, sizeof(ipt_handler));

    // Populate the temporary filename
    snprintf(pIpt->ipt_file, EUCA_MAX_PATH, sTempFileName);
    
    // If we have a command prefix (like euca_rootwrap) set it
    pIpt->cmdprefix[0] = '\0';
    if (psCmdPrefix) {
        snprintf(pIpt->cmdprefix, EUCA_MAX_PATH, "%s", psCmdPrefix);
    }
    // If we have a preload file path, set it.
    pIpt->preloadPath[0] = '\0';
    if (psPreloadPath) {
        snprintf(pIpt->preloadPath, EUCA_MAX_PATH, "%s", psPreloadPath);
    }
    // test required shell-outs
    if (euca_execlp_redirect(NULL, NULL, "/dev/null", FALSE, "/dev/null", FALSE, pIpt->cmdprefix, "iptables-save", NULL) != EUCA_OK) {
        LOGERROR("could not execute iptables-save. check command/permissions\n");
        return (1);
    }

    pIpt->init = 1;
    return (0);
}
コード例 #14
0
ファイル: vtape.c プロジェクト: AlD/bareos
int vtape::tape_op(struct mtop *mt_com)
{
   int result=0;
   int count = mt_com->mt_count;

   if (!online) {
      errno = ENOMEDIUM;
      return -1;
   }

   switch (mt_com->mt_op)
   {
   case MTRESET:
   case MTNOP:
   case MTSETDRVBUFFER:
      break;

   default:
   case MTRAS1:
   case MTRAS2:
   case MTRAS3:
   case MTSETDENSITY:
      errno = ENOTTY;
      result = -1;
      break;

   case MTFSF:                  /* Forward space over mt_count filemarks. */
      do {
         result = fsf();
      } while (--count > 0 && result == 0);
      break;

   case MTBSF:                  /* Backward space over mt_count filemarks. */
      do {
         result = bsf();
      } while (--count > 0 && result == 0);
      break;

   case MTFSR:      /* Forward space over mt_count records (tape blocks). */
/*
    file number = 1
    block number = 0

    file number = 1
    block number = 1

    mt: /dev/lto2: Erreur d'entree/sortie

    file number = 2
    block number = 0
*/
      /* tester si on se trouve a la fin du fichier */
      result = fsr(mt_com->mt_count);
      break;

   case MTBSR:      /* Backward space over mt_count records (tape blocks). */
      result = bsr(mt_com->mt_count);
      break;

   case MTWEOF:                 /* Write mt_count filemarks. */
      do {
         result = weof();
      } while (result == 0 && --count > 0);
      break;

   case MTREW:                  /* Rewind. */
      Dmsg0(dbglevel, "rewind vtape\n");
      check_eof();
      atEOF = atEOD = false;
      atBOT = true;
      current_file = 0;
      current_block = 0;
      lseek(fd, 0, SEEK_SET);
      result = !read_fm(VT_READ_EOF);
      break;

   case MTOFFL:                 /* put tape offline */
      result = offline();
      break;

   case MTRETEN:                /* Re-tension tape. */
      result = 0;
      break;

   case MTBSFM:                 /* not used by bareos */
      errno = EIO;
      result = -1;
      break;

   case MTFSFM:                 /* not used by bareos */
      errno = EIO;
      result = -1;
      break;

   case MTEOM:/* Go to the end of the recorded media (for appending files). */
      while (next_FM) {
         lseek(fd, next_FM, SEEK_SET);
         if (read_fm(VT_READ_EOF)) {
            current_file++;
         }
      }
      boffset_t l;
      while (::read(fd, &l, sizeof(l)) > 0) {
         if (l) {
            lseek(fd, l, SEEK_CUR);
         } else {
            ASSERT(0);
         }
         Dmsg0(dbglevel, "skip 1 block\n");
      }
      current_block = -1;
      atEOF = false;
      atEOD = true;

/*
   file number = 3
   block number = -1
*/
      /* Can be at EOM */
      break;

   case MTERASE:                /* not used by bareos */
      atEOD = true;
      atEOF = false;
      atEOT = false;

      current_file = 0;
      current_block = -1;
      lseek(fd, 0, SEEK_SET);
      read_fm(VT_READ_EOF);
      truncate_file();
      break;

   case MTSETBLK:
      break;

   case MTSEEK:
      break;

   case MTTELL:
      break;

   case MTFSS:
      break;

   case MTBSS:
      break;

   case MTWSM:
      break;

   case MTLOCK:
      break;

   case MTUNLOCK:
      break;

   case MTLOAD:
      break;

   case MTUNLOAD:
      break;

   case MTCOMPRESSION:
      break;

   case MTSETPART:
      break;

   case MTMKPART:
      break;
   }

   return result == 0 ? 0 : -1;
}
コード例 #15
0
ファイル: demo.c プロジェクト: jspenguin/lab3d-sdl
void demofile_write_frame(demofile_t* d, int timediff) {
    int i, j, zc, writesize;
    Uint8 *ldata, *delta, *rle, *rle_end;
    Uint8 hdr[2];
    vartrack_t* ct;

    ldata = d->cur_data;
    delta = d->delta_buf;

    for (j = 0, ct = d->vars; j < d->nvars; j++, ct++) {
        i = ct->cnt;

        switch(ct->elemsize) {
            ENCODELOOP(1, Uint8, NOSWAP);
            ENCODELOOP(2, Uint16, SDL_SwapLE16);
            ENCODELOOP(4, Uint32, SDL_SwapLE32);
            ENCODELOOP(8, Uint64, SDL_SwapLE64);
        }
    }

    delta = d->delta_buf;
    rle = d->rle_buf;
    rle_end = rle + d->buffer_size - 3;
    i = d->buffer_size;
    zc = 0;
    do {
        Uint8 bv = *delta++;
        if (bv == 0) {
            zc++;
        } else {
            if (zc) {
                *rle++ = 0;
                do {
                    *rle++ = 0x80 | (zc & 0x7f);
                    zc >>= 7;
                } while (zc && rle < rle_end);
                rle[-1] &= 0x7F;
            }
            if (rle >= rle_end)
                break;
            *rle++ = bv;
        }
    } while(--i);

    if (rle >= rle_end) {
        delta = d->delta_buf;
        writesize = d->buffer_size;
    } else {
        delta = d->rle_buf;
        writesize = rle - delta;
    }

    if (timediff >= 0xFFFF) timediff = 0xFFFF;

    demofile_write_hdr(d, writesize + 2, timediff);

    demofile_write(d, delta, writesize);
    if (d->format == 1) {
        set16(hdr, writesize + 2);
        demofile_write(d, hdr, 2);
    }
    if (d->rawfil) {
        truncate_file(d->rawfil);
    }
}
コード例 #16
0
ファイル: ebt_handler.c プロジェクト: jbreiding/eucalyptus
/**
 * Initialize the Ebtables handler structure.
 *
 * @param ebth [in] pointer to the EB table handler structure
 * @param cmdprefix [in] a string pointer to the prefix to use for each system commands
 *
 * @return 0 on success. 1 on failure.
 *
 * @see ipt_handler_init() 
 *
 * @pre
 *     - The ebth pointer should not be NULL
 *     - We should be able to create temporary files on the system
 *     - We should be able to execute ebtables commands.
 *
 * @post
 *     - Temporary files on disk: /tmp/ebt_filter_file-XXXXXX, /tmp/ebt_nat_file-XXXXXX
 *       and /tmp/ebt_asc_file-XXXXXX.
 *     - If cmdprefix was provided, the table's cmdprefix field will be set with it
 *
 * @note
 *     - Once temporary files are initialized the filename will be reused throughout the process
 *       lifetime. The files will be truncated/created on each successive calls to the *_handler_init()
 *       method. 
 */
int ebt_handler_init(ebt_handler *ebth, const char *cmdprefix) {
    int fd;
    char sTempFilterFile[EUCA_MAX_PATH] = "";
    char sTempNatFile[EUCA_MAX_PATH] = "";
    char sTempAscFile[EUCA_MAX_PATH] = "";
    
    if (!ebth) {
        return (1);
    }

    if (ebth->init) {
        snprintf(sTempFilterFile, EUCA_MAX_PATH, ebth->ebt_filter_file);
        snprintf(sTempNatFile, EUCA_MAX_PATH, ebth->ebt_nat_file);
        snprintf(sTempAscFile, EUCA_MAX_PATH, ebth->ebt_asc_file);

        if (truncate_file(sTempFilterFile)) {            
            return (1);
        }

        if (truncate_file(sTempNatFile)) {
            unlink(sTempFilterFile);
            return (1);
        }

        if (truncate_file(sTempAscFile)) {
            unlink(sTempFilterFile);
            unlink(sTempNatFile);
            return (1);
        }
    } else {
        snprintf(sTempFilterFile, EUCA_MAX_PATH, "/tmp/ebt_filter_file-XXXXXX");
        if ((fd = safe_mkstemp(sTempFilterFile))< 0) {
            LOGERROR("cannot create tmpfile '%s': check permissions\n", sTempFilterFile);
            return (1);
        }
        if (chmod(sTempFilterFile, 0600)) {
            LOGWARN("chmod failed: was able to create tmpfile '%s', but could not change file permissions\n", sTempFilterFile);
        }
        close(fd);
        
        snprintf(sTempNatFile, EUCA_MAX_PATH, "/tmp/ebt_nat_file-XXXXXX");
        if ((fd = safe_mkstemp(sTempNatFile)) < 0) {
            LOGERROR("cannot create tmpfile '%s': check permissions\n", sTempNatFile);
            unlink(sTempFilterFile);
            return (1);
        }
        if (chmod(sTempNatFile, 0600)) {
            LOGWARN("chmod failed: was able to create tmpfile '%s', but could not change file permissions\n", sTempNatFile);
        }
        close(fd);
        
        snprintf(sTempAscFile, EUCA_MAX_PATH, "/tmp/ebt_asc_file-XXXXXX");
        if ((fd = safe_mkstemp(sTempAscFile)) < 0) {
            LOGERROR("cannot create tmpfile '%s': check permissions\n", sTempAscFile);
            unlink(sTempFilterFile);
            unlink(sTempNatFile);
            return (1);
        }
        if (chmod(sTempAscFile, 0600)) {
            LOGWARN("chmod failed: was able to create tmpfile '%s', but could not change file permissions\n", sTempAscFile);
        }
        close(fd);
    }
    
    bzero(ebth, sizeof(ebt_handler));

    // Copy names back into handler
    snprintf(ebth->ebt_filter_file, EUCA_MAX_PATH, sTempFilterFile);
    snprintf(ebth->ebt_nat_file, EUCA_MAX_PATH, sTempNatFile);
    snprintf(ebth->ebt_asc_file, EUCA_MAX_PATH, sTempAscFile);

    if (cmdprefix) {
        snprintf(ebth->cmdprefix, EUCA_MAX_PATH, "%s", cmdprefix);
    } else {
        ebth->cmdprefix[0] = '\0';
    }

    // test required shell-outs
    if (euca_execlp_redirect(NULL, NULL, "/dev/null", FALSE, "/dev/null", FALSE, ebth->cmdprefix, "ebtables", "-L", NULL) != EUCA_OK) {
        LOGERROR("could not execute ebtables -L. check command/permissions\n");
        unlink(ebth->ebt_filter_file);
        unlink(ebth->ebt_nat_file);
        unlink(ebth->ebt_asc_file);
        return (1);
    }

    ebth->init = 1;
    return (0);
}
コード例 #17
0
ファイル: gpmondb.c プロジェクト: karthijrk/gpdb
/* truncate tail files */
apr_status_t truncate_tail_file(const char* tbl, apr_pool_t* pool, PGconn* conn)
{
	char fn[PATH_MAX];
	snprintf(fn, PATH_MAX, "%s%s_tail.dat", GPMON_DIR, tbl);
	return truncate_file(fn, pool);
}
コード例 #18
0
ファイル: many.cpp プロジェクト: hsgg/metricofevents
// ********* MAIN ************
int main()
{
	struct initializations init = initials();

	truncate_file();

	// spacetime
	Metric metric(init.m, init.a, init.q);
	cout << endl << metric.name << ":" << endl
		<< "m = " << metric.m << endl
		<< "a = " << metric.a << endl
		<< "q = " << metric.q << endl
		<< endl;

	// emfield
	EMField emfield(metric);

	// particle
	init.u[init.change] = find_u(metric, &init, init.x, init.u);
	Particle initparticle(0, init.teilchen_masse, init.teilchen_ladung,
		init.x, init.u, metric);

	// spectrum
	myfloat emin = 2.0;
	myfloat emax = 13.0;
	myfloat start_u0 = initparticle.u[0];
	vector<myfloat> freqmult(10000);
	for (unsigned i = 0; i < freqmult.size(); i++){
		// E = u0 * freqmult
		myfloat energy = i * (emax - emin) / freqmult.size() + emin;
		freqmult[i] = energy / start_u0;
	}
	Spectrum spec(freqmult, &metric);

	ofstream specfile("globalspec.dat");
	specfile << scientific;


	// per-ray data
	int nrays;
	for (nrays = init.nrays - 1; nrays >= 0; nrays--) {
		cout << "===========================================" << endl
			<< "Ray index: " << nrays << endl
			<< endl;
		// particle
		Particle particle(0, init.teilchen_masse, init.teilchen_ladung,
				init.x, init.u, metric);
		init.change = 1;
		init.umin = init.umin_inc;
		init.umax = init.umax_inc;
		particle.u[0] = start_u0;
		particle.u[3] = init.u[3] + nrays * init.u3_inc;
		particle.u[init.change] = find_u(metric, &init, particle.x,
				particle.u);

		cout << endl;
		for (unsigned mu = 0; mu < metric.dim; mu++) {
			cout << "x" << mu << " = " << particle.x[mu] << '\t';
			cout << "u" << mu << " = " << particle.u[mu] << endl;
		}
		cout << endl;

		stringstream nrays_str;
		nrays_str << nrays;
		string plotfilename;
		plotfilename = "globalplot.dat/plot";
		plotfilename += nrays_str.str();
		plotfilename += ".dat";

		// Iterate
		absorb = 0;
		go_ray(init, spec, metric, particle, emfield, plotfilename);

		cout << endl;
		for (unsigned mu = 0; mu < metric.dim; mu++) {
			cout << "x" << mu << " = " << particle.x[mu] << '\t';
			cout << "u" << mu << " = " << particle.u[mu] << endl;
		}
		cout << endl;

		if (absorb && (absorb != absorb_max)){
			cerr << "Error detected" << endl;
			exit(1);
		}

		cout << endl;
	}


	for (unsigned i = 0; i < spec.cnts.size(); i++){
		specfile << freqmult[i] * start_u0 << '\t'
			<< spec.cnts[i] << endl;
	}
	specfile.close();


	return 0;
}