Exemple #1
0
isc_result_t
isc_file_safecreate(const char *filename, FILE **fp) {
	isc_result_t result;
	int flags;
	struct stat sb;
	FILE *f;
	int fd;

	REQUIRE(filename != NULL);
	REQUIRE(fp != NULL && *fp == NULL);

	result = file_stats(filename, &sb);
	if (result == ISC_R_SUCCESS) {
		if ((sb.st_mode & S_IFREG) == 0)
			return (ISC_R_INVALIDFILE);
		flags = O_WRONLY | O_TRUNC;
	} else if (result == ISC_R_FILENOTFOUND) {
		flags = O_WRONLY | O_CREAT | O_EXCL;
	} else
		return (result);

	fd = open(filename, flags, S_IRUSR | S_IWUSR);
	if (fd == -1)
		return (isc__errno2result(errno));

	f = fdopen(fd, "w");
	if (f == NULL) {
		result = isc__errno2result(errno);
		close(fd);
		return (result);
	}

	*fp = f;
	return (ISC_R_SUCCESS);
}
Exemple #2
0
isc_boolean_t
isc_file_exists(const char *pathname) {
	struct stat stats;

	REQUIRE(pathname != NULL);

	return (ISC_TF(file_stats(pathname, &stats) == ISC_R_SUCCESS));
}
Exemple #3
0
isc_result_t
isc_file_mode(const char *file, mode_t *modep) {
	isc_result_t result;
	struct stat stats;

	REQUIRE(modep != NULL);

	result = file_stats(file, &stats);
	if (result == ISC_R_SUCCESS)
		*modep = (stats.st_mode & 07777);
	return (result);
}
Exemple #4
0
static void put_icacheR(PCStr(path))
{	const char *dp;
	char dc;
	int pdev,pino;
	FileStat st;
	CStr(xpath,1024);

	strcpy(xpath,path);
	pdev = pino = 0;

	for( dp = strchr(xpath,'/'); dp; dp = strchr(dp+1,'/') ){
		dc = dp[1];
		((char*)dp)[1] = 0;
		if( file_stats(pdev,pino,AVStr(xpath),xpath,&st,NULL) < 0 ){
			((char*)dp)[1] = dc;
			break;
		}
		((char*)dp)[1] = dc;
		pdev = st.st_dev;
		pino = st.st_ino;
	}
	file_stats(pdev,pino,AVStr(xpath),xpath,&st,NULL);
}
Exemple #5
0
isc_result_t
isc_file_getsize(const char *file, off_t *size) {
	isc_result_t result;
	struct stat stats;

	REQUIRE(file != NULL);
	REQUIRE(size != NULL);

	result = file_stats(file, &stats);

	if (result == ISC_R_SUCCESS)
		*size = stats.st_size;

	return (result);
}
Exemple #6
0
isc_result_t
isc_file_getmodtime(const char *file, isc_time_t *time) {
	isc_result_t result;
	struct stat stats;

	REQUIRE(file != NULL);
	REQUIRE(time != NULL);

	result = file_stats(file, &stats);

	if (result == ISC_R_SUCCESS)
		/*
		 * XXXDCL some operating systems provide nanoseconds, too,
		 * such as BSD/OS via st_mtimespec.
		 */
		isc_time_set(time, stats.st_mtime, 0);

	return (result);
}
Exemple #7
0
isc_result_t
isc_file_getmodtime(const char *file, isc_time_t *modtime) {
	isc_result_t result;
	struct stat stats;

	REQUIRE(file != NULL);
	REQUIRE(modtime != NULL);

	result = file_stats(file, &stats);

	if (result == ISC_R_SUCCESS)
#ifdef ISC_PLATFORM_HAVESTATNSEC
		isc_time_set(modtime, stats.st_mtime, stats.st_mtim.tv_nsec);
#else
		isc_time_set(modtime, stats.st_mtime, 0);
#endif

	return (result);
}
Exemple #8
0
static scanDirFunc find1(PCStr(file),DA *da)
{	int isdir,mtime,atime,size,blocks,dev,ino;
	int cnt;
	int match,rcode,cblocks;
	CStr(path,1024);
	FILE *out = da->d_out;
	FileStat st;
	const char *ftype;

	if( !da->d_top && file[0] == '.' )
		if( file[1] == 0 || file[1] == '.' && file[2] == 0 )
			return 0;
	da->d_nch++;
	mkabspath(AVStr(path),da->d_cwd,file);

	CNT++;
	match = 0;
	rcode = 0;

	if( da->d_Root ){
	isdir = file_stats(da->d_dev,da->d_ino,AVStr(path),path,&st,&blocks);
	}else
	isdir = file_stats(da->d_dev,da->d_ino,AVStr(path),file,&st,&blocks);
	dev = st.st_dev;
	ino = st.st_ino;
	mtime = st.st_mtime;
	atime = st.st_atime;
	size = st.st_size;
	
	if( isdir < 0 ){
		errlog("can't access: %s\n",path);
		return 0;
	}

	if( !F_FOLLOW_MOUNT && dev != ROOT_DEV ){
		if( ROOT_DEV == 0 )
			ROOT_DEV = dev;
		else	return 0;
	}

	N_BLK += blocks;

	if( isdir ){
		N_DIR++;
		/*
		match = 0 < dir1(da->d_cwd,path,file,dev,ino,out,&cblocks);
		*/
		match = 0 < dir1(da,da->d_cwd,path,file,dev,ino,out,&cblocks);
		if( F_REMOVE ){
			match = match && (0 < N_REM);
		}else
		if( (A_FROM==0||A_FROM<=atime) && (A_TILL==0||atime<=A_TILL) )
		if( (M_FROM==0||M_FROM<=mtime) && (M_TILL==0||mtime<=M_TILL) )
			match = 1;
		else	match = 0;
	}else{
		N_REG++;
		if( (A_FROM==0||A_FROM<=atime) && (A_TILL==0||atime<=A_TILL) )
		if( (M_FROM==0||M_FROM<=mtime) && (M_TILL==0||mtime<=M_TILL) )
			match = 1;
	}

	if( F_TYPE ){
		switch( *F_TYPE ){
			case 'f': if(  isdir ){ match = 0; } break;
			case 'd': if( !isdir ){ match = 0; } break;
		}
	}
	if( F_NAME ){
		CStr(fnam,1024);
		const char *np;
		if( np = strrpbrk(file,"/") ){
			np++;
		}else{
			np = file;
		}
		if( rexpmatch(F_NAME,np) ){
			if(  F_NAME_NOT ) match = 0;
		}else{
			if( !F_NAME_NOT ) match = 0;
		}
	}

	if( match && F_REMOVE && !da->d_top ){
		if( isdir )
			rcode = rmdir(file);
		else	rcode = unlink(file);
		if( rcode == 0 ){
			da->d_nrm++;
			N_BLKREM += blocks;
			N_REM++;
		}else	N_ERR++;
	}
	if( match || F_DU ){
		da->d_blocks += blocks;
		if( isdir ) da->d_blocks += cblocks;
	}

	if( !match && (F_TYPE || F_NAME) ){
	}else
	if( match && (F_PRINT || F_LSFORM)
	 || da->d_top && !F_REMOVE
	 || isdir && F_DIR
	/* || isdir && match_in_child */
	){
		fseek(out,0,2);
		if( F_DU ){
			if( isdir )
				fprintf(out,"%-7d ",(blocks+cblocks)/2);
			else	fprintf(out,"%-7d ",blocks/2);
		}
		if( F_LSFORM ){
			ls_unix(out,ls_opts,CVStr(NULL),path,&st);
		}else{
			ftype = "";
			if( isdir && path[strlen(path)-1] != '/' )
				ftype = "/";
			if( da->d_Root
			 && strneq(path,da->d_Root,da->d_Rlen) ){
				fprintf(out,"%s%s\n",path+da->d_Rlen,ftype);
			}else
			fprintf(out,"%s%s\n",path,ftype);
		}
	}
	return 0;
}