Exemplo n.º 1
0
/**
 * Calculate hash sums simultaneously, according to the info->sums_flags.
 * Calculated hashes are stored in info->rctx.
 *
 * @param info file data. The info->full_path can be "-" to denote stdin
 * @return 0 on success, -1 on fail with error code stored in errno
 */
static int calc_sums(struct file_info *info)
{
	FILE* fd = stdin; /* stdin */
	int res;
	uint64_t initial_size;

	if(IS_DASH_STR(info->full_path)) {
		info->print_path = "(stdin)";

#ifdef _WIN32
		/* using 0 instead of _fileno(stdin). _fileno() is undefined under 'gcc -ansi' */
		if(setmode(0, _O_BINARY) < 0) {
			return -1;
		}
#endif
	} else {
		struct rsh_stat_struct stat_buf;
		/* skip non-existing files */
		if(rsh_stat(info->full_path, &stat_buf) < 0) {
			return -1;
		}

		if((opt.mode & (MODE_CHECK | MODE_CHECK_EMBEDDED)) && S_ISDIR(stat_buf.st_mode)) {
			errno = EISDIR;
			return -1;
		}

		info->size = stat_buf.st_size; /* total size, in bytes */
		IF_WINDOWS(win32_set_filesize64(info->full_path, &info->size)); /* set correct filesize for large files under win32 */

		if(!info->sums_flags) return 0;

		/* skip files opened with exclusive rights without reporting an error */
		fd = rsh_fopen_bin(info->full_path, "rb");
		if(!fd) {
			return -1;
		}
	}

	re_init_rhash_context(info);
	initial_size = info->rctx->msg_size;

	if(percents_output->update != 0) {
		rhash_set_callback(info->rctx, (rhash_callback_t)percents_output->update, info);
	}

	/* read and hash file content */
	if((res = rhash_file_update(info->rctx, fd)) != -1) {
		if(!opt.bt_batch_file) {
			rhash_final(info->rctx, 0); /* finalize hashing */
		}
	}
	info->size = info->rctx->msg_size - initial_size;
	rhash_data.total_size += info->size;

	if(fd != stdin) fclose(fd);
	return res;
}
Exemplo n.º 2
0
/**
 * Fill file information in the file_t structure.
 *
 * @param file the file information
 * @param use_lstat nonzero if lstat() shall be used
 * @return 0 on success, -1 on error
 */
int rsh_file_stat2(file_t* file, int use_lstat)
{
	struct rsh_stat_struct st;
	int res = -1;

#ifdef _WIN32
	int i;
	(void)use_lstat; /* ignore on windows */

	if(file->wpath) {
		free(file->wpath);
		file->wpath = NULL;
	}

	for(i = 0; i < 2; i++) {
		wchar_t* wpath = c2w(file->path, i);
		if(wpath == NULL) continue;
		res = clib_wstat(wpath, &st);
		if(res == 0 || errno != ENOENT) {
			file->wpath = wpath;
			file->size  = st.st_size;

			/* set correct filesize for large files under win32 */
			win32_set_filesize64(file->path, &file->size);
			break;
		}
		free(wpath);
	}
#else
	res = (use_lstat ? lstat(file->path, &st) :
		rsh_stat(file->path, &st));
	file->size  = st.st_size;
#endif /* _WIN32 */

	file->mtime = st.st_mtime;
	return res;
}