示例#1
0
pio_status_t
pio_transpose(const char *plink_file_prefix, const char *transposed_file_prefix)
{
    struct pio_file_t plink_file;
    if( pio_open( &plink_file, plink_file_prefix ) != PIO_OK )
    {
        return PIO_ERROR;
    }

    char *bed_path = concatenate( plink_file_prefix, ".bed" );
    char *transposed_bed_path = concatenate( transposed_file_prefix, ".bed" );

    pio_status_t status = bed_transpose( bed_path, transposed_bed_path, pio_num_loci( &plink_file ), pio_num_samples( &plink_file ) );
    if( status == PIO_OK )
    {
        char *fam_path = concatenate( plink_file_prefix, ".fam" );
        char *transposed_fam_path = concatenate( transposed_file_prefix, ".fam" );
        file_copy( fam_path, transposed_fam_path );
        free( fam_path );
        free( transposed_fam_path );
        
        char *bim_path = concatenate( plink_file_prefix, ".bim" );
        char *transposed_bim_path = concatenate( transposed_file_prefix, ".bim" );
        file_copy( bim_path, transposed_bim_path );
        free( bim_path );
        free( transposed_bim_path );
    }

    pio_close( &plink_file );

    free( bed_path );
    free( transposed_bed_path );

    return status;
}
示例#2
0
int main(int argc, char *argv[]){
	FILE *fp;

	if(argc == 1){
		file_copy(stdin,stdout);
	}else{
		while(--argc > 0){
			if((fp = fopen(*++argv, "r")) == NULL){
				printf("can't open file %s \n", *argv);
				return 1;
			}else{
				file_copy(fp, stdout);
				fclose(fp);
			}
		}
	}

	fp = fopen(*argv, "r");
	int max = 1000;
	char *s = (char *)malloc(max);
	s = get_line(s, max, fp);
	fclose(fp);

	printf("\n first line is %s \n", s);
	free(s);

	return 0;
}
示例#3
0
/* Overwrites destfile with temp_file. If forceBakOverWrite = 0, the bakfile will not be overwritten if it exists, else it will be.*/
int32_t safe_overwrite_with_bak(char *destfile, char *temp_file, char *bakfile, int32_t forceBakOverWrite)
{
	int32_t rc;
	if(file_exists(destfile))
	{
		if(forceBakOverWrite != 0 || !file_exists(bakfile))
		{
			if(file_copy(destfile, bakfile) < 0)
			{
				cs_log("Error copying original config file %s to %s. The original config will be left untouched!", destfile, bakfile);
				if(unlink(temp_file) < 0)
					{ cs_log("Error removing temp config file %s (errno=%d %s)!", temp_file, errno, strerror(errno)); }
				return 1;
			}
		}
	}
	rc = file_copy(temp_file, destfile);
	if(rc < 0)
	{
		cs_log("An error occured while writing the new config file %s.", destfile);
		if(rc == -2)
			{ cs_log("The config will be missing or only partly filled upon next startup as this is a non-recoverable error! Please restore from backup or try again."); }
		if(unlink(temp_file) < 0)
			{ cs_log("Error removing temp config file %s (errno=%d %s)!", temp_file, errno, strerror(errno)); }
		return 1;
	}
	if(unlink(temp_file) < 0)
		{ cs_log("Error removing temp config file %s (errno=%d %s)!", temp_file, errno, strerror(errno)); }
	return 0;
}
示例#4
0
int ini_write_string_section(FFILE f_in, FFILE f_out, const char *p_section,
                             const char *p_template, const char *p_value)
{
  long file_start, 
       file_end;
  
  int found = ini_find_string_section(f_in, p_section, p_template, &file_start, &file_end);

  fseek(f_in, 0, SEEK_SET);
  fseek(f_out, 0, SEEK_SET);

  if(found) {
    file_copy(f_in, f_out, file_start);
    fprintf(f_out,"%s=%s\n",p_template, p_value);
    fseek(f_in, file_end, SEEK_SET);
    file_copy(f_in, f_out);
  }
  else {
    file_copy(f_in, f_out);
    fprintf(f_out,"\n[%s]\n",p_section);
    fprintf(f_out,"%s=%s\n",p_template, p_value);
  }

  return(TRUE);
}
示例#5
0
int main(int argc,char **argv)
 {
	FILE* in;
	if(argc == 1)
	{
		file_copy(stdin,stdout);	
		return 0;
	}

	while(--argc > 0)
	{
		if((in = fopen(*++argv,"r")) == NULL )	
		{
			printf("open file %s error\n",*argv);	
			  return 1;
		}
		else 
		{
			file_copy(in,stdout);	
			fclose(in);
		}
	}

	return 0;
 }
示例#6
0
int securesoho_copy(const char *src, const char *dst){
	char dirname[256], basename[256];
	DIR *sdir;
	struct dirent *sdirent;

	if(!src || !dst) {
		fprintf(stderr, "%s:%d The src:0x%p or dst:%p is NULL\n", __FUNCTION__, __LINE__, src, dst);
		return -1;
	}
	
	if (path_split(src, dirname, basename) < 0){
		fprintf(stderr, "%s:%d, Failed to get the dirname and basename\n", __FUNCTION__, __LINE__);
		return -1;
	}

	if (is_wildcard(dirname)){
		fprintf(stderr, "%s:%d Don't support wildcard for directory\n", __FUNCTION__, __LINE__);
		return -1;
	}
	//FIXME: to make simple, we just consider following situaltion
	//The source file are wildcard string
	//The dest are directory
	if (!is_wildcard(src)){
		if (is_dir(dst)){
			char fullpath[512];
			sprintf(fullpath, "%s/%s", dst, basename);
			return file_copy(src, fullpath);
		}else{
			return file_copy(src, dst);	
		}
	}


	sdir = opendir(dirname);
	if(NULL == sdir) {
		fprintf(stderr, "%s:%d Failed to open dir:%s\n", __FUNCTION__, __LINE__, dirname);
		return -1;	
	}
	while((sdirent = readdir(sdir)) != NULL){
		if (is_dir(sdirent->d_name)) {
			continue;
		}
		if (wild_match(basename, sdirent->d_name)){
			char sfile[512], dfile[512];
			sprintf(dfile, "%s/%s", dst, sdirent->d_name);
			sprintf(sfile, "%s/%s", dirname, sdirent->d_name);
			file_copy(sfile, dfile);
		}
	}
	if(sdir) 
		closedir(sdir);

	return 0;
}
示例#7
0
bool ini_write_string_section(const char *p_file, const char *p_section, 
                              const char *p_template, const char *p_value)
{
  int ret;

  FFILE f_orig(NULL, p_file, "r", FALSE);
  if (!f_orig)
    return(FALSE);
    
  FFILE f_new(tmpfile());
  if (!f_new)
    return(FALSE);
  
  ret = file_copy(f_orig, f_new);
  if(!ret) {
    fclose(f_orig);
    fclose(f_new);
    return(FALSE);  
  }
    
  f_orig.close();
  
  if(f_orig.open(NULL, p_file, "w", FALSE)) {
    ret = ini_write_string_section(f_new, f_orig, p_section, p_template, p_value);
    fclose(f_orig);
  }
  
  fclose(f_new);
  
  return (ret);
}
示例#8
0
int file_link(const char *src, const char *dest)
{
    struct stat dest_stat;
    int r;

    r = stat(dest, &dest_stat);
    if (r == 0) {
        r = unlink(dest);
        if (r < 0) {
            opkg_perror(ERROR, "unable to remove `%s'", dest);
            return -1;
        }
    } else if (errno != ENOENT) {
        opkg_perror(ERROR, "unable to stat `%s'", dest);
        return -1;
    }

    r = symlink(src, dest);
    if (r < 0) {
        opkg_perror(DEBUG, "unable to create symlink '%s', falling back to copy",
                    dest);
        return file_copy(src, dest);
    }

    return r;
}
/*
  Insert file "insertname" between lines "start_line" and "end_line" of file "filename"
*/
int insert_into_file(const char *filename, const char *insertname, const char *start_line, const char *end_line)
{
  int ch;
  static char line[1024*4];
  const char *tmpname = "tmp.h";
  FILE *source_fp;
  FILE *dest_fp;
  FILE *insert_fp;
  
  if ( file_copy(filename, tmpname) == 0 )
    return 0;

  source_fp = fopen(tmpname, "r");
  dest_fp = fopen(filename, "w");
  insert_fp = fopen(insertname, "r");

  if ( source_fp == NULL || dest_fp == NULL || insert_fp == NULL )
    return 0;
  
  for(;;)
  {
    if ( fgets(line, 1024*4, source_fp) == NULL )
      break;
    if ( strncmp(line, start_line, strlen(start_line)) == 0 )
    {
      fputs(line, dest_fp);

      while( ( ch = fgetc(insert_fp) ) != EOF )
	fputc(ch, dest_fp);
      
      fputs("\n", dest_fp);
      
      for(;;)
      {
	if ( fgets(line, 1024*4, source_fp) == NULL )
	  break;
	if ( strncmp(line, end_line, strlen(end_line)) == 0 )
	{
	  fputs(line, dest_fp);
	  break;
	}
      }
    }
    else
    {
      fputs(line, dest_fp);
    }
  }

  fclose(insert_fp);
  fclose(source_fp);
  fclose(dest_fp);
  
  unlink(tmpname);
  
  printf("patched %s\n", filename);

  return 1;
}
示例#10
0
int main(int argc, char *argv[])
{
#ifdef HAVE_MPI
  MPI_Init(&argc, &argv);
  MPI_Comm_rank(MPI_COMM_WORLD, &rank);
#endif
  
  IOShell::Interface interface;
  bool success = interface.parse_options(argc, argv);
  if (!success) {
    exit(EXIT_FAILURE);
  }
  
  Ioss::Init::Initializer io;
#ifndef NO_XDMF_SUPPORT
  Ioxf::Initializer ioxf;
#endif

#ifdef USE_CGNS
  Iocgns::Initializer iocgns;
#endif

  std::string in_file   = interface.inputFile[0];
  std::string out_file  = interface.outputFile;

  OUTPUT << "Input:    '" << in_file  << "', Type: " << interface.inFiletype  << '\n';
  OUTPUT << "Output:   '" << out_file << "', Type: " << interface.outFiletype << '\n';
  OUTPUT << '\n';
  
  double begin = timer();
  file_copy(interface);
  double end = timer();

#ifdef HAVE_MPI
  // Get total data read/written over all processors, and the max time..
  Ioss::ParallelUtils parallel(MPI_COMM_WORLD);

  // Combine these some time...
  time_read  = parallel.global_minmax(time_read,  Ioss::ParallelUtils::DO_MAX);
  time_write = parallel.global_minmax(time_write, Ioss::ParallelUtils::DO_MAX);
  data_read  = parallel.global_minmax(data_read,  Ioss::ParallelUtils::DO_SUM);
  data_write = parallel.global_minmax(data_write, Ioss::ParallelUtils::DO_SUM);
#endif

  if (interface.statistics) {
    OUTPUT << "\n\tElapsed time = " << end - begin << " seconds."
	   << " (read = " << time_read << ", write = " << time_write << ")\n"
	   << "\tTotal data read  = " << data_read  << " bytes; "
	   << data_read  / time_read  <<" bytes/second.\n"
	   << "\tTotal data write = " << data_write << " bytes; "
	   << data_write / time_write << " bytes/second.\n";
  }

  OUTPUT << "\n" << codename << " execution successful.\n";
#ifdef HAVE_MPI
  MPI_Finalize();
#endif
  return EXIT_SUCCESS;
}
示例#11
0
static int
opkg_download_cache(const char *src, const char *dest_file_name,
	curl_progress_func cb, void *data)
{
    char *cache_name = xstrdup(src);
    char *cache_location, *p;
    int err = 0;

    if (!conf->cache || str_starts_with(src, "file:")) {
	err = opkg_download(src, dest_file_name, cb, data, 0);
	goto out1;
    }

    if(!file_is_dir(conf->cache)){
	    opkg_msg(ERROR, "%s is not a directory.\n",
			    conf->cache);
	    err = 1;
	    goto out1;
    }

    for (p = cache_name; *p; p++)
	if (*p == '/')
	    *p = ',';	/* looks nicer than | or # */

    sprintf_alloc(&cache_location, "%s/%s", conf->cache, cache_name);
    if (file_exists(cache_location))
	opkg_msg(NOTICE, "Copying %s.\n", cache_location);
    else {
       /* cache file with funky name not found, try simple name */
        free(cache_name);
        char *filename = strrchr(dest_file_name,'/');
        if (filename)
           cache_name = xstrdup(filename+1); // strip leading '/'
        else
           cache_name = xstrdup(dest_file_name);
        free(cache_location);
        sprintf_alloc(&cache_location, "%s/%s", conf->cache, cache_name);
        if (file_exists(cache_location))
           opkg_msg(NOTICE, "Copying %s.\n", cache_location);
        else  {
 	    err = opkg_download(src, cache_location, cb, data, 0);
	    if (err) {
	       (void) unlink(cache_location);
	       goto out2;
	  }
	}
    }

    err = file_copy(cache_location, dest_file_name);


out2:
    free(cache_location);
out1:
    free(cache_name);
    return err;
}
示例#12
0
size_t CFileUtils::file_copy(int src_fd, const char* dst_filename) throw (CSyscallException)
{    
    int dst_fd = open(dst_filename, O_WRONLY|O_CREAT|O_EXCL);
    if (-1 == src_fd)
        THROW_SYSCALL_EXCEPTION(NULL, errno, "open");

    sys::CloseHelper<int> ch(dst_fd);
    return file_copy(src_fd, dst_fd);
}
示例#13
0
size_t CFileUtils::file_copy(const char* src_filename, int dst_fd) throw (CSyscallException)
{
    int src_fd = open(src_filename, O_RDONLY);
    if (-1 == src_fd)
        THROW_SYSCALL_EXCEPTION(NULL, errno, "open");

    sys::CloseHelper<int> ch(src_fd);
    return file_copy(src_fd, dst_fd);
}
示例#14
0
int main(void) {

    FILE *stream;
    long index;
    char c;
    char path[255], line[MAX_LINE];
    DWORD flags;

    if ((stream = tmpfile()) == NULL) { // create temp file
        printf("temp file could not be created\n");
    } else {
        printf("temp file created\n");
    }

    for (index = 0; index < CHAR_COUNT; index++) {
        fputc(index, stream);
    }

    fseek(stream, SEEK_POS, SEEK_SET);

    for (index = SEEK_POS; index < CHAR_COUNT; index++) {
        c = fgetc(stream);
        printf("%c", c);
    }
    printf("\n");
    if (fclose(stream) == 0) { // close temp file
        printf("successfully closed temp file\n");
    } else {
        printf("failed to close temp file\n");
    }
    tmpnam(path);
    printf("temp file name %s\n", path);
    /* read_line("main.c", line);
    while (line) {
        for (index = 0; index < 80; index++) {
            printf("%s\n", line);
            read_line("main.c", line);
        }
    } */
    strcpy(path, "c:\\b.xml");
    if (file_exists(path)) {
        printf("%s exists\n", path);
    } else {
        printf("%s does not exists\n", path);
    }
    file_copy(path, "c:\\c.xml", 0);

    flags = (/*FILE_ATTRIBUTE_ARCHIVE |*/ FILE_ATTRIBUTE_ENCRYPTED | FILE_ATTRIBUTE_HIDDEN);


    if (flags & FILE_ATTRIBUTE_ARCHIVE) {
        printf("FILE_ATTRIBUTE_ARCHIVE exists\n");
    }
    printf("FILE_ATTRIBUTE_ARCHIVE=%d, FILE_ATTRIBUTE_ENCRYPTED=%d, FILE_ATTRIBUTE_HIDDEN=%d",
           FILE_ATTRIBUTE_ARCHIVE, FILE_ATTRIBUTE_ENCRYPTED, FILE_ATTRIBUTE_HIDDEN);
}
示例#15
0
shared_ptr<temp_dir> testcase::_prepare_dir(judge::pool &pool, const compiler::result &cr)
{
	shared_ptr<temp_dir> dir = pool.create_temp_dir("test_");
	path_a in_path(cr.dir->path());
	in_path.push(cr.compiler->target_filename().c_str());
	path_a out_path(dir->path());
	out_path.push(cr.compiler->target_filename().c_str());
	file_copy(in_path, out_path);
	return dir;
}
示例#16
0
文件: media.c 项目: zhilongC/MyButler
static BU_UINT8 media_fcopy_msg_proc(media_handle_t mhandle, prot_handle_t phandle, void* msg, BU_UINT32 msg_len)
{
	char* sendBuf = NULL;
	BU_UINT32 ulOffSet = 0;
	BU_UINT32 src_path_len = 0;
	char* src_path = NULL;
	BU_UINT32 dst_path_len = 0;
	char* dst_path = NULL;

	pkt_mhead_t mhead;
	BU_UINT32 ulResult = BU_OK;
	BU_UINT32 ulFileType = 0;

	UNPKG_UINT32_MSG(msg, ulOffSet, ulFileType);
	UNPKG_UINT32_MSG(msg, ulOffSet, src_path_len);
	if((src_path = (char*)calloc(1, src_path_len)) == NULL){
		E_LOG("calloc error\n");
		return BU_ERROR;
	}
	UNPKG_BYTES_MSG(msg,ulOffSet, src_path_len, src_path);

	UNPKG_UINT32_MSG(msg, ulOffSet, dst_path_len);
	if((dst_path = (char*)calloc(1, dst_path_len)) == NULL){
		E_LOG("calloc error\n");
		free(src_path);
		return BU_ERROR;
	}
	UNPKG_BYTES_MSG(msg, ulOffSet, dst_path_len, dst_path);


	ulResult = file_copy(src_path, dst_path);


	/* ****************pkg prot PKG_PROT_MEDIA_FCOPY_ACK************** 
		+4B: result code 
		
	*********************************************************** */
	ulOffSet = 0;
	mhead.media_handle = mhandle;
	mhead.prot_handle = phandle;
	mhead.type = PKG_PROT_MEDIA_FCOPY_ACK;
	
	PKG_INIT_BUFF(sendBuf, sizeof(pkt_mhead_t) + 4);
	PKG_BYTES_MSG(sendBuf, ulOffSet, &mhead, sizeof(pkt_mhead_t));
	PKG_UINT32_MSG(sendBuf, ulOffSet, ulResult);
	msg_list_push(sendBuf, ulOffSet, PROT_TASK_ID, MEDIA_TASK_ID); 
	PKG_FREE_BUFF(sendBuf);
	free(src_path);
	free(dst_path);
	
	
	return BU_OK;

}
示例#17
0
文件: la_process.c 项目: Laukien/la-C
void process_setInputFromFile(PROCESS *self, const char *file) {
	assert(self);
	assert(file);

	if (!file_exists(file)) {
		message_error("file not found");
		return;
	}

	file_copy(file, self->file_input, NULL, NULL);
}
示例#18
0
文件: ex8-1.c 项目: haimoz/HelloC
int main(int argc, char **argv)
{
	int fd;
	
	if (argc == 1) {
		file_copy(0, 1); /* stdin to stdout */
	} else {
		for (--argc, ++argv; argc > 0; --argc, ++argv) {
			if ((fd = open(*argv, O_RDONLY, 0)) == -1) {
				fprintf(stderr, "Failed to open \"%s\" (ignored): %s\n", *argv, strerror(errno));
			} else {
				file_copy(fd, 1);
				if (close(fd)) {
					fprintf(stderr, "Failed to close \"%s\": %s\n", *argv, strerror(errno));
				}
			}
		}
	}
	return 0;
}
示例#19
0
/******************************************************
 Create a new character based on the specified template
 return the id of the newly created character
 the returned string MUST BE FREED by caller
 return nullptr if fails
 *******************************************************/
char * character_create_from_template(context_t * ctx, const char * my_template,
		const char * map, int layer, int x, int y)
{
	char * new_id;

	new_id = file_new(CHARACTER_TABLE, nullptr);
	if (file_copy(CHARACTER_TEMPLATE_TABLE, my_template, CHARACTER_TABLE,
			new_id) == false)
	{
		file_delete(CHARACTER_TABLE, new_id);
		return nullptr;
	}

	// Check if new character is allowed to be created here
	if (map_check_tile(ctx, new_id, map, layer, x, y) == 0)
	{
		entry_destroy(CHARACTER_TABLE, new_id);
		file_delete(CHARACTER_TABLE, new_id);
		free(new_id);
		return nullptr;
	}

	// Write position
	if (entry_write_string(CHARACTER_TABLE, new_id, map, CHARACTER_KEY_MAP,
			nullptr) == RET_NOK)
	{
		entry_destroy(CHARACTER_TABLE, new_id);
		file_delete(CHARACTER_TABLE, new_id);
		free(new_id);
		return nullptr;
	}

	if (entry_write_int(CHARACTER_TABLE, new_id, x, CHARACTER_KEY_POS_X,
			nullptr) == RET_NOK)
	{
		entry_destroy(CHARACTER_TABLE, new_id);
		file_delete(CHARACTER_TABLE, new_id);
		free(new_id);
		return nullptr;
	}

	if (entry_write_int(CHARACTER_TABLE, new_id, y, CHARACTER_KEY_POS_Y,
			nullptr) == RET_NOK)
	{
		entry_destroy(CHARACTER_TABLE, new_id);
		file_delete(CHARACTER_TABLE, new_id);
		free(new_id);
		return nullptr;
	}

	return new_id;
}
示例#20
0
/**
 *  Copy a file.
 *
 *  This function will copy a file and use MD5 checking to validate the
 *  file copy was successful. It will also add a "copying file" message
 *  to the log file.
 *
 *  If an error occurs in this function it will be appended to the log and
 *  error mail messages, and the process status will be set appropriately.
 *
 *  @param  src_file  - full path to the source file
 *  @param  dest_file - full path to the destination file
 *
 *  @return
 *    - 1 if successfull
 *    - 0 if an error occurred
 */
int dsproc_copy_file(const char *src_file, const char *dest_file)
{
    LOG( DSPROC_LIB_NAME,
        "Copying:  %s\n"
        " -> to:   %s\n", src_file, dest_file);

    if (!file_copy(src_file, dest_file, FC_CHECK_MD5)) {
        dsproc_set_status(DSPROC_EFILECOPY);
        return(0);
    }

    return(1);
}
示例#21
0
bool file_tmpl::files_copy(const char* name, const FILE_FROM_TO* tab)
{
	string from, to;

	from = "tmpl/Makefile.in";
	to.format("%s/Makefile.in", path_to_.c_str());
	if (file_copy(from.c_str(), to.c_str()) == false)
		return false;

	for (size_t i = 0; tab[i].from != NULL; i++)
	{
		from.format("%s/%s", path_from_.c_str(), tab[i].from);
		to.format("%s/%s", path_to_.c_str(), tab[i].to);
		if (file_copy(from, to) == false)
		{
			printf("create %s failed!\r\n", name);
			return false;
		}
	}

	printf("create %s ok!\r\n", name);
	return true;
}
bool file_move (const std::string& old_filespec, const std::string& new_filespec)
{
  // try to move the file by renaming - if that fails then do a copy and delete the original
  if (file_rename(old_filespec, new_filespec))
    return true;
  if (!file_copy(old_filespec, new_filespec))
    return false;
  // I'm not sure what to do if the delete fails - is that an error?
  // I've made it an error and then delete the copy so that the original state is recovered
  if (file_delete(old_filespec))
    return true;
  file_delete(new_filespec);
  return false;
}
示例#23
0
int create_logfolder(char *folder_path)
{
	/* make folder on sdcard */
	uint16_t foldernumber = 1; // start with folder 0001
	int mkdir_ret;

	/* look for the next folder that does not exist */
	while (foldernumber < MAX_NO_LOGFOLDER) {
		/* set up file path: e.g. /mnt/sdcard/sensorfile0001.txt */
		sprintf(folder_path, "%s/session%04u", mountpoint, foldernumber);
		mkdir_ret = mkdir(folder_path, S_IRWXU | S_IRWXG | S_IRWXO);
		/* the result is -1 if the folder exists */

		if (mkdir_ret == 0) {
			/* folder does not exist, success */

			/* now copy the Matlab/Octave file */
			char mfile_out[100];
			sprintf(mfile_out, "%s/session%04u/run_to_plot_data.m", mountpoint, foldernumber);
			int ret = file_copy(mfile_in, mfile_out);

			if (!ret) {
				warnx("copied m file to %s", mfile_out);

			} else {
				warnx("failed copying m file from %s to\n %s", mfile_in, mfile_out);
			}

			break;

		} else if (mkdir_ret == -1) {
			/* folder exists already */
			foldernumber++;
			continue;

		} else {
			warn("failed creating new folder");
			return -1;
		}
	}

	if (foldernumber >= MAX_NO_LOGFOLDER) {
		/* we should not end up here, either we have more than MAX_NO_LOGFOLDER on the SD card, or another problem */
		warn("all %d possible folders exist already", MAX_NO_LOGFOLDER);
		return -1;
	}

	return 0;
}
int
file_move(const char *src, const char *dst, size_t bs)
{
	if (rename(src, dst) == 0)
		return 0;
	if (bs == 0)
		return -1;
	if (file_is_dir(src)) {
		WARN("Cannot move directory by copying");
		return -1;
	}
	if (file_copy(src, dst, -1, bs, 0) < 0)
		return -1;
	return unlink(src);
}
示例#25
0
int file_move(const char *src, const char *dest)
{
    int err;

    err = rename(src, dest);

    if (err && errno == EXDEV) {
	err = file_copy(src, dest);
	unlink(src);
    } else if (err) {
	fprintf(stderr, "%s: ERROR: failed to rename %s to %s: %s\n",
		__FUNCTION__, src, dest, strerror(errno));
    }

    return err;
}
示例#26
0
/******************************************************
Create a new character based on the specified template
return the id of the newly created character
the returned string MUST BE FREED by caller
return NULL if fails
*******************************************************/
char * character_create_from_template(context_t * ctx,const char * my_template,const char * map, int layer, int x, int y)
{
	char * new_id;
	char * templatename;
	char * fullname;

	new_id = file_new(CHARACTER_TABLE,NULL);

	templatename = strconcat(base_directory,"/",CHARACTER_TEMPLATE_TABLE,"/",my_template,NULL);
	fullname = strconcat(base_directory,"/",CHARACTER_TABLE,"/",new_id,NULL);
	file_copy(templatename,fullname);
	free(templatename);
	free(fullname);

	/* Check if new character is allowed to be created here */
	if(map_check_tile(ctx,new_id,map,layer,x,y) == 0) {
		entry_destroy(CHARACTER_TABLE,new_id);
		file_delete(CHARACTER_TABLE,new_id);
		free(new_id);
		return NULL;
	}

	/* Write position */
	if(entry_write_string(CHARACTER_TABLE,new_id,map,CHARACTER_KEY_MAP,NULL) == RET_NOK ) {
		entry_destroy(CHARACTER_TABLE,new_id);
		file_delete(CHARACTER_TABLE,new_id);
		free(new_id);
		return NULL;
	}

	if(entry_write_int(CHARACTER_TABLE,new_id,x,CHARACTER_KEY_POS_X,NULL) == RET_NOK ) {
		entry_destroy(CHARACTER_TABLE,new_id);
		file_delete(CHARACTER_TABLE,new_id);
		free(new_id);
		return NULL;
	}

	if(entry_write_int(CHARACTER_TABLE,new_id,y,CHARACTER_KEY_POS_Y,NULL) == RET_NOK ) {
		entry_destroy(CHARACTER_TABLE,new_id);
		file_delete(CHARACTER_TABLE,new_id);
		free(new_id);
		return NULL;
	}

	return new_id;
}
示例#27
0
/**************************************************
 Create a new item based on the specified template
 return the id of the newly created item
 the returned string must be freed by caller
 return NULL if fails
**************************************************/
char * item_create_from_template(const char * my_template)
{
	char * new_name;
	char * templatename;
	char * newfilename;

	new_name = file_new(ITEM_TABLE,NULL);

	templatename = strconcat(base_directory,"/",ITEM_TEMPLATE_TABLE,"/",my_template,NULL);
	newfilename = strconcat(base_directory,"/",ITEM_TABLE,"/",new_name,NULL);

	file_copy(templatename,newfilename);
	free(newfilename);
	free(templatename);

	return new_name;
}
示例#28
0
文件: main.c 项目: ChingezKhan/gthumb
static void
convert_old_comment (char     *real_file,
		     char     *rc_file,
		     gpointer  data)
{
	char *comment_file;
	char *comment_dir;

	comment_file = comments_get_comment_filename (real_file, TRUE);
	comment_dir = remove_level_from_path (comment_file);
	ensure_dir_exists (comment_dir, 0755);

	file_copy (rc_file, comment_file);

	g_free (comment_dir);
	g_free (comment_file);
}
示例#29
0
int main()
{
    int ret;
#ifdef HELLO_WORLD
    ret = print_hellword();
 #endif // HELLO_WORLD

 #ifdef FAHR_TO_CELSIUS
    ret = fahr_to_celsius();
 #endif // FAHR_TO_CELSIUS

 #ifdef FILE_COPY
    ret = file_copy();
 #endif // FILE_COPY

#ifdef TEST_CHAR
    ret = test_char();
#endif // TEST_CHAR

#ifdef CHAR_COUNTING
    ret = char_counting();
#endif // CHAR_COUNTING

#ifdef LINE_COUNTING
    ret = line_counting();
#endif // LINE_COUNTING

#ifdef FUNC_PL
    struct pl_ops *func_pl;
    func_pl = pl_setup();
    func_pl->arrays();
#endif // FUNC_PL

#ifdef FUNC_PL_TEST
    char s1[] = "hello world";
    char *s2   = "hello world";

    printf("s1 is %d\n", sizeof(s1));
    printf("s2 is %d\n", sizeof(s2));
#endif // FUNC_PL_TEST
     ret = 0;
     if (ret > 0){
        printf("\nprint successfully!, result is 0x%x.\n", ret);
    }
}
示例#30
0
/**
 * @name : directory copy
 * @comment : use like (cp -r)
 * @return : success 0 , fail -1
 */
int dir_copy(PATH_TYPE src,PATH_TYPE dst,int option){
	dirent_stack* stk=(dirent_stack*)malloc(sizeof(dirent_stack));
	char cdst[PATH_MAX]={0};
	char csrc[PATH_MAX]={0};
	char edst[PATH_MAX]={0};
	char esrc[PATH_MAX]={0};
	char tmp[PATH_MAX]={0};
#ifdef __linux__
	struct stat statue;
	DIR* d=NULL;
	struct dirent* dir;
	int fp;
	sprintf(tmp,"%s/%s",dst,leaf_dir(src));
	mkdir(tmp,ACCESSPERMS);
	stat(src,&statue);
	chmod(tmp,statue.st_mode);
	push_dirent_stack(stk,src,tmp);
	while(stk->top>0){
		d=opendir(top_dirent_stack_src(stk));
		strcpy(csrc,top_dirent_stack_src(stk));
		strcpy(cdst,top_dirent_stack_dst(stk));
		pop_dirent_stack(stk);
		if(d){
			while((dir=readdir(d))!=NULL){
				if(strcmp(dir->d_name,".") && strcmp(dir->d_name,"..")){
					sprintf(esrc,"%s/%s",csrc,dir->d_name);
					sprintf(edst,"%s/%s",cdst,dir->d_name);
					stat(esrc,&statue);
					if(S_ISDIR(statue.st_mode)){
						push_dirent_stack(stk,esrc,edst);
						mkdir(edst,ACCESSPERMS);
						stat(esrc,&statue);
						chmod(edst,statue.st_mode);
					}else{
						if(file_copy(esrc,edst)==-1)puts("fail");
					}
				}
			}
			closedir(d);
		}
	}
#endif
	free(stk);
	return 0;
}