/**
 * e_db3_utils_upgrade_format:
 * @filename: path to a Berkeley DB file
 *
 * Upgrades the file format of a Berkeley DB file from a previous version.
 *
 * Returns: 0 if successful, -1 on failure
 **/
gint
e_db3_utils_upgrade_format (const gchar *filename)
{
	gchar *copy_filename;
	gchar *check_filename;
	DB *db;
	gint ret_val;

	ret_val = db_create (&db, NULL, 0);
	if (ret_val != 0)
		return ret_val;

	copy_filename = get_copy_filename (filename);
	check_filename = get_check_filename (filename);

	ret_val = cp_file (filename, copy_filename);

	if (ret_val == 0)
		ret_val = touch_file (check_filename);
	if (ret_val == 0)
		ret_val = db->upgrade (db, filename, 0);
	if (ret_val == 0)
		ret_val = g_unlink (check_filename);

	if (ret_val == 0)
		ret_val = g_unlink (copy_filename);

	db->close (db, 0);

	g_free (check_filename);
	g_free (copy_filename);
	return ret_val;
}
static gint
resume_upgrade (const gchar *filename,
                const gchar *copy_filename,
                const gchar *check_filename)
{
	DB *db;
	gint ret_val;

	ret_val = db_create (&db, NULL, 0);

	if (ret_val == 0)
		ret_val = cp_file (copy_filename, filename);

	if (ret_val == 0)
		ret_val = db->upgrade (db, filename, 0);

	if (ret_val == 0)
		ret_val = g_unlink (check_filename);
	if (ret_val == 0)
		ret_val = g_unlink (copy_filename);

	db->close (db, 0);

	return ret_val;
}
示例#3
0
void cp(char * filename, char * path)
{
	int i, name_length, type, ret, j;
	filename[str_len(filename) - 1] = 0;
	iNode * path_inode = current;
	path_inode = parser_path(path, path_inode);
	iNode * filename_inode = current;
	filename_inode = parser_path(filename, filename_inode);

	if(filename_inode->gid < currentUsr.group)
	{
		printf("\nCan not copy '%s'. Permission denied.", filename);
		return ;
	}

	if(filename_inode == NULL)
	{
		printf("\nCan not copy '%s'. File doesn't exist.", filename);
		return ;
	}

	if(path_inode == NULL)
	{
		name_length = str_len(path);
		for(i = 0; i < name_length; i++)
			if(path[i] == '/')
			{
				printf("\nCan not copy '%s' to '%s'. Directory doesn't exist.", filename, path);
				return;
			}
		rename_file(filename_inode->iNode_number, path);
		return ;
	}

	int init_block = current->data.direct_blocks[0];
	directoryEntry * dr = (directoryEntry*)calloc(64 * 96, 1);
	read_disk(0, init_block, dr, (BLOCK_SIZE * 12), 0);
	for(i = 1; i < 96; i++){
		if( strcmp(filename, dr[i].name) == 1){
			type = dr[i].type;
			break;
		}
	}

	if(type == FILE)
	{
		cp_file(filename, filename_inode, path_inode);
	}
	else if(type == DIRECTORY)
	{
		recursive_cp(filename, filename_inode, path_inode);
	}

	return ;
}
示例#4
0
文件: lib.c 项目: willcast/nsboot
void system_logged(char *cmd) {
	pid_t code;
	uint8_t exit_status;
	int sig_num;
	char argv_zero[PATH_MAX];
	char core_path[PATH_MAX];
	char *exe_name;
	char *space;

	assert(cmd != NULL);
	strncpy(argv_zero, cmd, sizeof(argv_zero));
	argv_zero[PATH_MAX-1] = '\0';
	space = strchr(argv_zero, ' ');
	if (space != NULL)
		*space = '\0';

	exe_name = basename(argv_zero);
	
	code = system(cmd);

	if (WIFEXITED(code)) {
		exit_status = WEXITSTATUS(code);

		if (exit_status) {
			logprintf("2command %s died with code %d\n", 
				exe_name, exit_status);
			return;
		}
	} else if (WIFSIGNALED(code)) {
		sig_num = WTERMSIG(code);
		if (WCOREDUMP(code)) {
			logprintf("2command %s died from signal %d and dumped core\n",
				exe_name, sig_num);

			snprintf(core_path, sizeof(core_path), "/var/core-%s",
				exe_name);
			cp_file(core_path, "/mnt/media/nsboot/logs");
			logprintf("0the core dump was copied to /mnt/media/nsboot/logs/core-%s",
				exe_name);
			return;
		} else {
			logprintf("2command %s died from signal %d (but no core was produced)",
				exe_name, sig_num);
			return;
		}
	} else {
		logprintf("2something evil happened to command %s", exe_name);
	}
}
示例#5
0
文件: h3-2.c 项目: psc0606/embedded
int cp_dir(char *src, char *dst)
{
	DIR *dir;
	char path_src[256] = {0}, path_dst[256] = {0};
	struct stat buf;
	struct dirent *dp;
	strcpy(path_src,src);
	strcpy(path_dst,dst);
	if (stat(src, &buf) < 0)
	{
		perror("stat failed");
		return -1;
	}
	if ((dir = opendir(path_src)) == NULL)
	{
		perror("open err");
		return -1;
	}
	umask(0000);
	if (mkdir(path_dst,buf.st_mode & 0777) < 0)
	{
		perror("mkdir err");
		return -1;
	}
	while ((dp = readdir(dir)) != NULL)
	{
		strcpy(path_src,src);
		strcpy(path_dst,dst);
		if ((strcmp(dp->d_name,".") == 0) || (strcmp(dp->d_name,"..") == 0))
		{
			continue;
		}
		strcat(path_src,"/");
		strcat(path_dst,"/");
		strcat(path_src,dp->d_name);
		strcat(path_dst,dp->d_name);
		
		if (DT_REG == dp->d_type)
		{
			cp_file(path_src, path_dst);
		}
		else if (DT_DIR == dp->d_type)
		{
			cp_dir(path_src, path_dst);
		}
	}
}
示例#6
0
文件: cp_dir.c 项目: songpku/linuxPD
int cp_dir(const char *src,const char *dst)
{
	DIR *dirp = NULL;
	
	if(NULL== (dirp=opendir(src)))
	{
		perror("opendir");exit(EXIT_FAILURE);
	}	
	
	struct dirent *entp = NULL;
	while ( NULL != (entp =readdir(dirp)))
	{
		if (strcmp(entp->d_name, "..")==0 || strcmp(entp->d_name, ".")==0)	//ignore ./ ../
        {
          continue;
        }
		
		char *name_src =(char *) malloc( strlen(src) + 1 + strlen(entp->d_name) + 1 );
		sprintf(name_src,"%s/%s\0",src,entp->d_name);
		char *name_dst =(char *) malloc( strlen(dst) + 1 + strlen(entp->d_name) + 1 );
		sprintf(name_dst,"%s/%s\0",dst,entp->d_name);

		struct stat stat_src;
		if (stat(name_src, &stat_src) == -1) 
		{
			fprintf(stderr, "%s(%d): stat error(%s)!\n", __FILE__, __LINE__, strerror(errno));
			exit(EXIT_FAILURE);
		}	
	
		if (S_ISREG(stat_src.st_mode))		//regular file
		{
			cp_file(name_src,name_dst,stat_src.st_mode);
			free(name_src);
			free(name_dst);
		}	
		else if ( S_ISDIR( stat_src.st_mode ))
		{		
			if( -1 ==mkdir(name_dst,stat_src.st_mode))
			{
				perror("mkdir");exit(EXIT_FAILURE);
			}
			cp_dir( name_src, name_dst);		
			free(name_src);
			free(name_dst);
		}
	}
}
示例#7
0
文件: lpcp.c 项目: JamesLinus/vsta
int
main(int argc, char **argv)
{
	off_t skip = 0;
	int x;

	/*
	 * Get an I/O buffer
	 */
	buf = malloc(BUFSIZE);
	if (buf == 0) {
		perror("lpcp");
		exit(1);
	}

	if ((argc > 4) && (argv[1][0] == '+')) {
		/*
		 * skip only allowed with exactly one src file
		 */
		usage();
	}

	if ((argc == 4) && (argv[1][0] == '+')) {
		skip = atoi(argv[1] + 1);
		argc--;
		argv++;
	}

	/*
	 * Sanity check argument count
	 */
	if (argc < 3) {
		usage();
	}

	if (isdir(argv[argc-1])) {
		fprintf(stderr, "lpcp: %s is a directory\n", argv[argc-1]);
		exit(1);
	}

	for (x = 1; x < argc-1; ++x)  {
		cp_file(argv[x], argv[argc-1], skip);
	}
	return (0);
}
示例#8
0
文件: core.c 项目: yoannsculo/ucblog
// Relative path
static int process_image(char *filename)
{
	char dir[PATH_MAX];
	
	char source[PATH_MAX];
	char dest[PATH_MAX];

	if (get_current_dir(filename, dir) != 0)
		return -1;

	sprintf(source,  "%s/%s", config_site.source_directory, filename);
	sprintf(dest, "%s/%s", config_site.dest_directory, dir);

	if (cp_file(source, dest) == 0) {
		// printf("Processing IMG file : %s\n", filename);
		return 0;
	} else {
		return -1;
	}
}
示例#9
0
void recursive_cp(char * filename, iNode * origin, iNode * destination)
{
	int i;
	iNode * path;

	if(origin->gid < currentUsr.group)
		return ;

	cp_dir(filename, destination);

	//get new path
	int init_block = destination->data.direct_blocks[0];
	directoryEntry * dr = (directoryEntry*)calloc(64 * 96, 1);
	read_disk(0, init_block, dr, (BLOCK_SIZE * 12), 0);
	for(i = 2; i < 96; i++){
		if( strcmp(filename, dr[i].name)){
			path = fs_get_inode(dr[i].inode);
			break;
		}
	}

	//search for files and folders
	init_block = origin->data.direct_blocks[0];
	dr = (directoryEntry*)calloc(sizeof(directoryEntry), 96);
	read_disk(0, init_block, dr, BLOCK_SIZE * 12, 0);
	for ( i = 2; i < 96; i++)
	{
		if (dr[i].type == FILE)
		{
			cp_file(dr[i].name, origin, path);
		}
		else if(dr[i].type == DIRECTORY)
		{
			recursive_cp(dr[i].name, fs_get_inode(dr[i].inode), path);
		}
	}
	write_disk(0, init_block, dr, BLOCK_SIZE * 12, 0);

	return ;
}
示例#10
0
文件: h3-2.c 项目: psc0606/embedded
int main(int argc, char **argv)
{
	struct stat buf;
	if (argc != 3)
	{
		perror("format err");
		return -1;
	}
	if (stat(argv[1],&buf) < 0)
	{
		perror("input err");
		return -1;
	}
	if (S_ISREG(buf.st_mode))
	{
		cp_file(argv[1],argv[2]);
	}
	else if (S_ISDIR(buf.st_mode))
	{
		cp_dir(argv[1],argv[2]);
	}
}
示例#11
0
文件: tests.c 项目: dbhoekman/CS360
//This tests the cp function
//It does so by calling cp on two files and checks their INODES size
//If the INODES sizes do not match it will return 0.
int cpTest()
{
  MINODE *test_mip = running->cwd;
  INODE *ip = NULL;
  int ino = 0;
  int tinysize = 37;
  int bigsize = 12713;
  int newsize = 0;

  strcpy(pathname, "");
  cd(pathname);
  test_mip = running->cwd;

  printf("\n\n-------- TESTING CP FUNCTION --------\n\n");
  strcpy(pathname, "tiny");
  strcpy(third, "newtiny");
  printf("Testing >cp tiny newtiny\n");
  cp_file(pathname);
  strcpy(pathname, "newtiny");
  ino = getino(test_mip, pathname);
  if(ino != 0)
    test_mip = iget(dev, ino);

  ip = &test_mip->INODE;
  newsize = ip->i_size;

  printf("Size of tiny: %d\n", tinysize);
  printf("Size of newtiny: %d\n", newsize);

  if(tinysize != newsize)
  {
    printf("TEST FAILED\n\n");
    return 0;
  }
  else
    printf("TEST PASSED\n\n");
  newsize = 0;



  strcpy(pathname, "/Y/bigfile");
  strcpy(third, "/newbig");
  printf("Testing >cp /Y/bigfile newbig\n");
  cp_file(pathname);
  strcpy(pathname, "newbig");
  ino =  my_search(pathname);
  if(ino != 0)
    test_mip = iget(dev, ino);

  ip = &test_mip->INODE;
  newsize = ip->i_size;

  printf("Size of bigfile: %d\n", bigsize);
  printf("Size of newbig: %d\n", newsize);

  if(bigsize != newsize)
  {
    printf("TEST FAILED\n\n");
    return 0;
  }
  else
    printf("TEST PASSED\n\n");


  printf("ALL CP TESTS PASSED\n\n\n");
  return 1;
}
示例#12
0
文件: cp_dir.c 项目: songpku/linuxPD
int main(int argc,char *argv[])
{
	if (argc < 3)
	{
		fprintf(stderr,"usage %s src_dir dst_src\n",argv[0]);exit(EXIT_FAILURE);
	}
	
	struct stat stat_src;
	if (stat(argv[1], &stat_src) != 0) 
	{
      fprintf(stderr, "%s(%d): stat error(%s)!\n", __FILE__, __LINE__, strerror(errno));
      exit(EXIT_FAILURE);
    }	
	
	umask(0000);
	if (S_ISREG(stat_src.st_mode))		//regular file
    {
		struct stat stat_dst;
		if (stat(argv[2], &stat_dst) == -1) 
		{
			if(errno != ENOENT)			//if errno not cause by file/dir not exist
			{
				fprintf(stderr, "%s(%d): stat error(%s)!\n", __FILE__, __LINE__, strerror(errno));
				exit(EXIT_FAILURE);
			}
			else						//if dst_flie not exist.
			{
				cp_file(argv[1],argv[2],stat_src.st_mode);
			}
		}	
		else		//dst file exist.
		{
			if(S_ISDIR(stat_dst.st_mode))	//cp a file to a exist dir
			{				
				char *ptr=(char *)malloc(strlen(argv[2])+1+strlen(argv[1])+1);
				sprintf(ptr,"%s/%s\0",argv[2],argv[1]);			
				cp_file(argv[1],ptr,stat_src.st_mode);
			}
			else						//cp file to a exist file
			{
				printf("file %s exist, do you want overwrite it[y/n]:",argv[2]);
				char ch;
				while(!scanf("%c",&ch))
				{
					getchar();
				}
				if(ch =='Y' || ch == 'y' )
				{
					unlink(argv[2]);
					cp_file(argv[1],argv[2],stat_src.st_mode);	
				}
				else
					return 1;	
			}
		}
    }	
	
	else if (S_ISDIR(stat_src.st_mode))	//dir
    {
		struct stat stat_dst;
		if (stat(argv[2], &stat_dst) == -1) 
		{
			if(errno != ENOENT)			//if errno not cause by file/dir not exist
			{
				fprintf(stderr, "%s(%d): stat error(%s)!\n", __FILE__, __LINE__, strerror(errno));
				exit(EXIT_FAILURE);
			}
			else						//file/dir not exist
			{
				errno=0;
				if(-1 == mkdir(argv[2],stat_src.st_mode))
				{
					perror("mkdir");exit(EXIT_FAILURE);
				}
				cp_dir(argv[1],argv[2]);
			}
		}
		else if(S_ISREG(stat_dst.st_mode))		//can't copy a dir to a file
		{
			fprintf(stderr,"can't copy a dir to a file\n");exit(EXIT_FAILURE);
		}
		else								//copy a dir to a exsit dir ,
		{			
			char *ptr=(char *)malloc(strlen(argv[1])+1+strlen(argv[2])+1);
			sprintf(ptr,"%s/%s\0",argv[2],argv[1]);
			if(-1 == mkdir(ptr,stat_src.st_mode))
			{
				perror("mkdir");exit(EXIT_FAILURE);
			}			
			cp_dir(argv[1],ptr);
			free(ptr);
		}
	}
	
}	
示例#13
0
int
copy(void)
{
	ARCHD *arcn;
	int res;
	int fddest;
	char *dest_pt;
	int dlen;
	int drem;
	int fdsrc = -1;
	struct stat sb;
	char dirbuf[PAXPATHLEN+1];

	arcn = &archd;
	/*
	 * set up the destination dir path and make sure it is a directory. We
	 * make sure we have a trailing / on the destination
	 */
	dlen = strlcpy(dirbuf, dirptr, sizeof(dirbuf));
	if (dlen >= sizeof(dirbuf) ||
	    (dlen == sizeof(dirbuf) - 1 && dirbuf[dlen - 1] != '/')) {
		tty_warn(1, "directory name is too long %s", dirptr);
		return 1;
	}
	dest_pt = dirbuf + dlen;
	if (*(dest_pt-1) != '/') {
		*dest_pt++ = '/';
		++dlen;
	}
	*dest_pt = '\0';
	drem = PAXPATHLEN - dlen;

	if (stat(dirptr, &sb) < 0) {
		syswarn(1, errno, "Cannot access destination directory %s",
			dirptr);
		return 1;
	}
	if (!S_ISDIR(sb.st_mode)) {
		tty_warn(1, "Destination is not a directory %s", dirptr);
		return 1;
	}

	/*
	 * start up the hard link table; file traversal routines and the
	 * modification time and access mode database
	 */
	if ((lnk_start() < 0) || (ftree_start() < 0) || (dir_start() < 0))
		return 1;

	/*
	 * When we are doing interactive rename, we store the mapping of names
	 * so we can fix up hard links files later in the archive.
	 */
	if (iflag && (name_start() < 0))
		return 1;

	/*
	 * set up to cp file trees
	 */
	cp_start();

	/*
	 * while there are files to archive, process them
	 */
	while (next_file(arcn) == 0) {
		fdsrc = -1;

		/*
		 * check if this file meets user specified options
		 */
		if (sel_chk(arcn) != 0)
			continue;

		/*
		 * if there is already a file in the destination directory with
		 * the same name and it is newer, skip the one stored on the
		 * archive.
		 * NOTE: this test is done BEFORE name modifications as
		 * specified by pax. this can be confusing to the user who
		 * might expect the test to be done on an existing file AFTER
		 * the name mod. In honesty the pax spec is probably flawed in
		 * this respect
		 */
		if (uflag || Dflag) {
			/*
			 * create the destination name
			 */
			if (strlcpy(dest_pt, arcn->name + (*arcn->name == '/'),
			    drem + 1) > drem) {
				tty_warn(1, "Destination pathname too long %s",
					arcn->name);
				continue;
			}

			/*
			 * if existing file is same age or newer skip
			 */
			res = lstat(dirbuf, &sb);
			*dest_pt = '\0';

			if (res == 0) {
				if (uflag && Dflag) {
					if ((arcn->sb.st_mtime<=sb.st_mtime) &&
					    (arcn->sb.st_ctime<=sb.st_ctime))
						continue;
				} else if (Dflag) {
					if (arcn->sb.st_ctime <= sb.st_ctime)
						continue;
				} else if (arcn->sb.st_mtime <= sb.st_mtime)
					continue;
			}
		}

		/*
		 * this file is considered selected. See if this is a hard link
		 * to a previous file; modify the name as requested by the
		 * user; set the final destination.
		 */
		ftree_sel(arcn);
		if ((chk_lnk(arcn) < 0) || ((res = mod_name(arcn, RENM)) < 0))
			break;
		if ((res > 0) || (set_dest(arcn, dirbuf, dlen) < 0)) {
			/*
			 * skip file, purge from link table
			 */
			purg_lnk(arcn);
			continue;
		}

		/*
		 * Non standard -Y and -Z flag. When the exisiting file is
		 * same age or newer skip
		 */
		if ((Yflag || Zflag) && ((lstat(arcn->name, &sb) == 0))) {
			if (Yflag && Zflag) {
				if ((arcn->sb.st_mtime <= sb.st_mtime) &&
				    (arcn->sb.st_ctime <= sb.st_ctime))
					continue;
			} else if (Yflag) {
				if (arcn->sb.st_ctime <= sb.st_ctime)
					continue;
			} else if (arcn->sb.st_mtime <= sb.st_mtime)
				continue;
		}

		if (vflag) {
			(void)safe_print(arcn->name, listf);
			vfpart = 1;
		}
		++flcnt;

		/*
		 * try to create a hard link to the src file if requested
		 * but make sure we are not trying to overwrite ourselves.
		 */
		if (lflag)
			res = cross_lnk(arcn);
		else
			res = chk_same(arcn);
		if (res <= 0) {
			if (vflag && vfpart) {
				(void)putc('\n', listf);
				vfpart = 0;
			}
			continue;
		}

		/*
		 * have to create a new file
		 */
		if ((arcn->type != PAX_REG) && (arcn->type != PAX_CTG)) {
			/*
			 * create a link or special file
			 */
			if ((arcn->type == PAX_HLK) ||
			    (arcn->type == PAX_HRG)) {
				int payload;

				res = lnk_creat(arcn, &payload);
			} else {
				res = node_creat(arcn);
			}
			if (res < 0)
				purg_lnk(arcn);
			if (vflag && vfpart) {
				(void)putc('\n', listf);
				vfpart = 0;
			}
			continue;
		}

		/*
		 * have to copy a regular file to the destination directory.
		 * first open source file and then create the destination file
		 */
		if ((fdsrc = open(arcn->org_name, O_RDONLY, 0)) < 0) {
			syswarn(1, errno, "Unable to open %s to read",
			    arcn->org_name);
			purg_lnk(arcn);
			continue;
		}
		if ((fddest = file_creat(arcn, 0)) < 0) {
			rdfile_close(arcn, &fdsrc);
			purg_lnk(arcn);
			continue;
		}

		/*
		 * copy source file data to the destination file
		 */
		cp_file(arcn, fdsrc, fddest);
		file_close(arcn, fddest);
		rdfile_close(arcn, &fdsrc);

		if (vflag && vfpart) {
			(void)putc('\n', listf);
			vfpart = 0;
		}
	}

	/*
	 * restore directory modes and times as required; make sure all
	 * patterns were selected block off signals to avoid chance for
	 * multiple entry into the cleanup code.
	 */
	(void)sigprocmask(SIG_BLOCK, &s_mask, (sigset_t *)NULL);
	ar_close();
	proc_dir();
	ftree_chk();

	return 0;
}
示例#14
0
void main(int argc, char *argv[])
{
	int i = 0, cmd = -1, fd = 0; 
	char line[128], cname[64], temp_pathname[124], pathname[124] = "NULL", basename[124] = "NULL",
	     dirname[124] = "NULL", parameter[124] = "NULL", *device;

	//GETS DISK NAME TO MOUNT
	/*
	printf("Enter the name of your disk image\n");
	fgets(device, 128, stdin);
	device[strlen(device)-1] = 0;  // kill the \n char at end	
	*/
	device = "mydisk";
	
	mount_root(device, &fd);

	while(1)
	{
		//printf("P%d running: ", running.uid);
		printf("nick's@linux:");
		pwd(P0.cwd);
		printf("$ ");
		fgets(line, 128, stdin);
		line[strlen(line)-1] = 0;  // kill the \n char at end
		if (line[0]==0) continue;

		sscanf(line, "%s %s %s", cname, pathname, parameter);

		if(strcmp(pathname,"NULL") != 0)		
			strcpy(PATHNAME, pathname);
		
		if(strcmp(parameter,"NULL") != 0)		
			strcpy(PARAMETER, parameter);

		cmd = findCmd(cname); // map cname to an index
		switch(cmd)
		{
			case 0 : menu();		break;
			case 1 : pwd(P0.cwd);printf("\n");			break;
			case 2 : ls();			break;
			case 3 : cd();			break;
			case 4 : make_dir();		break;
			case 5 : rmdir();               break;
			case 6 : creat_file();          break;
			case 7 : link();                break;
			case 8 : unlink();              break;
			case 9 : symlink();             break;
			case 10: rm_file();             break;
			case 11: chmod_file();          break;
			case 12: chown_file();          break;
			case 13: stat_file();           break;
			case 14: touch_file();          break;
			case 20: open_file();           break;		//LEVEL 2
			case 21: close_file((int)PATHNAME);          break;
			case 22: pfd();                 break;
			case 23: lseek_file();          break;
			case 24: //access_file();         break;
				break;
			case 25: //read_file();           
				break;
			case 26: write_file();          break;
			case 27: cat_file();            break;
			case 28: cp_file();             break;
			case 29: mv_file();             break;
			case 30: mount();               break;		//LEVLEL 3
			case 31: //umount();              break;
			case 32: //cs();                  break;
			case 33: //do_fork();             break;
			case 34: //do_ps();               break;
			case 40: //sync();                break; 
			case 41: quit();              	 break;
			case 42 : system("clear");	break; 
			default: printf("invalid command\n");
			break;
			
		}

		//RESET NAMES
		strcpy(parameter, "NULL");
		strcpy(pathname, "NULL");
		strcpy(PATHNAME, "NULL");
	}
	
  }
示例#15
0
// function to check if files have been created, modified or deleted and applies the actions to backup
int cp_reg_files(char *src,char *dest)
{
        DIR *dirp;
        struct dirent *direntp;
        struct stat stat_buf;
	char nomedir[256];
	char novodest[256];
	char nomefile[256];
	time_t t=time(NULL);
	struct tm mytime=*localtime(&t);
	time_t tmodificado=time(NULL);
	struct tm tmod;
	int num;
	int cont=0;
	int cont2=0;
	int dif=0;
	int create=0;
	int apagou=0;
	FILE *bk,*stream;
	chdir("..");
    dirp = opendir(src);
	chdir(src);
    while ((direntp = readdir(dirp)) != NULL)
    {
	//printf("entrou while\n");
        stat(direntp->d_name, &stat_buf);
        if (S_ISREG(stat_buf.st_mode))
        {
		//printf("entrouif\n");
		int exc=0;
		char src1[100];
		strcpy(src1,src);
		strcat(src1,"/");
		strcat(src1,direntp->d_name);
		if(S_IXUSR & stat_buf.st_mode) {/*printf("entrou\n");*/ exc=1;}
		chdir("..");
		if(firsttime==1)
		{
		  chdir(dest);
		  sprintf(nomedir,"%d_%d_%d_%d_%d_%d",mytime.tm_year+1900,mytime.tm_mon+1,mytime.tm_mday,mytime.tm_hour,mytime.tm_min,mytime.tm_sec);
		  mkdir(nomedir,0777);
		  strcpy(novodest,dest);
		  strcat(novodest,"/");
		  strcat(novodest,nomedir);
		  strcpy(nomefile,nomedir);
		  strcat(nomefile,"/");
		  strcat(nomefile,"__bckinfo__.txt");
		  //printf("%s\n",nomefile);
		  if(stream = fopen(nomefile,"r") != NULL)
		  {
			bk=fopen(nomefile,"a");
		  }
		  else
		  {
			bk=fopen(nomefile,"w");
		  }
		  fprintf(bk,"%s - %d_%d_%d_%d_%d_%d \n",direntp->d_name,mytime.tm_year+1900,mytime.tm_mon+1,mytime.tm_mday,mytime.tm_hour,mytime.tm_min,mytime.tm_sec);
		  fclose(bk);
		  guardar_vec(direntp->d_name,mytime.tm_year+1900,mytime.tm_mon+1,mytime.tm_mday,mytime.tm_hour,mytime.tm_min,mytime.tm_sec);
		  chdir("..");
		  cp_file(src1, novodest, direntp->d_name,exc);
		  //listar_vec(elem,nelem);
		}
		else
		{
			printf("Nome Ficheiro : %s\n",direntp->d_name);
			printf("\n");
			dif=check_vector_files(src,elem,nelem);
			printf("VEC APOS\n");
			listar_vec(elem,nelem);
			printf("DIF - %d\n",dif);
			if(dif>0)
			{
				apagou=1;
				chdir(dest);
				sprintf(nomedir,"%d_%d_%d_%d_%d_%d",mytime.tm_year+1900,mytime.tm_mon+1,mytime.tm_mday,mytime.tm_hour,mytime.tm_min,mytime.tm_sec);
				mkdir(nomedir,0777);
		  		strcpy(novodest,dest);
		  		strcat(novodest,"/");
		  		strcat(novodest,nomedir);
				strcpy(nomefile,nomedir);
		  		strcat(nomefile,"/");
		  		strcat(nomefile,"__bckinfo__.txt");
				if(stream = fopen(nomefile,"r") != NULL)
		  		{
					bk=fopen(nomefile,"w");
		  		}
				chdir("..");
			}
		  if(verifica_existe(elem,nelem,direntp->d_name)==0)
		  {
			printf("entrouverifica_existe\n");
			cont++;
			create=1;
			apagoutudo=0;
			chdir(dest);
		  	sprintf(nomedir,"%d_%d_%d_%d_%d_%d",mytime.tm_year+1900,mytime.tm_mon+1,mytime.tm_mday,mytime.tm_hour,mytime.tm_min,mytime.tm_sec);
		  	mkdir(nomedir,0777);
		  	strcpy(novodest,dest);
		  	strcat(novodest,"/");
		  	strcat(novodest,nomedir);
			strcpy(nomefile,nomedir);
		  	strcat(nomefile,"/");
		  	strcat(nomefile,"__bckinfo__.txt");
			if(stream = fopen(nomefile,"r") != NULL)
		  	{
				bk=fopen(nomefile,"a");
		  	}
		  	else
		  	{
				bk=fopen(nomefile,"w");
		  	}
		  	//printf("%s\n",novodest);
			//fprintf(bk,"%s - %d / %d / %d - %d:%d:%d \n",direntp->d_name,mytime.tm_year+1900,mytime.tm_mon+1,mytime.tm_mday,mytime.tm_hour,mytime.tm_min,mytime.tm_sec);
			fclose(bk);
			//listar_vec(elem,nelem);
		  	guardar_vec(direntp->d_name,mytime.tm_year+1900,mytime.tm_mon+1,mytime.tm_mday,mytime.tm_hour,mytime.tm_min,mytime.tm_sec);
			//listar_vec(elem,nelem);
		  	chdir("..");
		  	cp_file(src1, novodest, direntp->d_name,exc);
			chdir(src);
			printf("entrourewind\n");
			rewinddir(dirp);
			chdir("..");
			//verifica_anteriores(elem,nelem,nomefile,direntp->d_name,src,dest);
			//chdir("..");
		  }
		  else
		  {
			//printf("entrou se ja existe\n");
			cont++;
			apagoutudo=0;
		  tmodificado=stat_buf.st_mtime;
		  tmod=*localtime(&tmodificado);
		  //printf("Verifica Tempo : %d\n",verifica_tempo(elem,nelem,direntp->d_name,tmod.tm_year+1900,tmod.tm_mon+1,tmod.tm_mday,tmod.tm_hour,tmod.tm_min,tmod.tm_sec));
		  if(verifica_tempo(elem,nelem,direntp->d_name,tmod.tm_year+1900,tmod.tm_mon+1,tmod.tm_mday,tmod.tm_hour,tmod.tm_min,tmod.tm_sec)==1)
		  {
			cont2++;
		      chdir(dest);
		      sprintf(nomedir,"%d_%d_%d_%d_%d_%d",mytime.tm_year+1900,mytime.tm_mon+1,mytime.tm_mday,mytime.tm_hour,mytime.tm_min,mytime.tm_sec);
		      mkdir(nomedir,0777);
		      strcpy(novodest,dest);
		      strcat(novodest,"/");
		      strcat(novodest,nomedir);
		      strcpy(nomefile,nomedir);
		      strcat(nomefile,"/");
		      strcat(nomefile,"__bckinfo__.txt");
		      if(stream = fopen(nomefile,"r") != NULL)
		      {
				bk=fopen(nomefile,"a");
		      }
		      else
		      {
				bk=fopen(nomefile,"w+");
		      }
		      num = get_i(elem,nelem,direntp->d_name);
		      fprintf(bk,"%s - %d_%d_%d_%d_%d_%d \n",direntp->d_name,elem[num].ano,elem[num].mes,elem[num].dia,elem[num].horas,elem[num].minutos,elem[num].segundos);
		      fclose(bk);
		      verifica_anteriores(elem,nelem,nomefile,direntp->d_name,src,dest);
		      //printf("%s\n",novodest);
		      altera_vec(elem,nelem,direntp->d_name,mytime.tm_year+1900,mytime.tm_mon+1,mytime.tm_mday,mytime.tm_hour,mytime.tm_min,mytime.tm_sec);
		      chdir("..");
		      cp_file(src1, novodest, direntp->d_name,exc);
		      //listar_vec(elem,nelem);
		  }
		  else
		  {
			//printf("entrou se nao modificou o tempo\n");
			if(cont2>0 || apagou==1 || create==1)
			{
				printf("entrou onde nao devia\n");
			chdir(dest);
		      	if(stream = fopen(nomefile,"r") != NULL)
		      	{	
				bk=fopen(nomefile,"a");
		      	}
		      	else
		      	{
				bk=fopen(nomefile,"w");
		      	}
			num = get_i(elem,nelem,direntp->d_name);
			printf("num - %d\n",num);
		      fprintf(bk,"%s - %d_%d_%d_%d_%d_%d \n",direntp->d_name,elem[num].ano,elem[num].mes,elem[num].dia,elem[num].horas,elem[num].minutos,elem[num].segundos);
			fclose(bk);
			chdir("..");
			}
		  }
		 }
		}
		//printf("depoiscpfile\n");
		chdir(src);
        }
    }
    if(cont==0 && firsttime==0 && apagoutudo==0)
    {
	//printf("entrou cont\n");
	chdir("..");
	chdir(dest);
	apagoutudo=1;
	//printf("entrou\n");
	sprintf(nomedir,"%d_%d_%d_%d_%d_%d",mytime.tm_year+1900,mytime.tm_mon+1,mytime.tm_mday,mytime.tm_hour,mytime.tm_min,mytime.tm_sec);
	mkdir(nomedir,0777);
	strcpy(nomefile,nomedir);
	strcat(nomefile,"/");
	strcat(nomefile,"__bckinfo__.txt");
	//printf("%s\n", nomefile);
	bk=fopen(nomefile,"w+");
	fclose(bk);
	chdir("..");
	chdir(src);
    }
    if(firsttime==1)
    {
      firsttime=0;
    }
    closedir( dirp ) ;
    return 0;
}