示例#1
0
void
process_file(const char* path)
{
	struct stat stat;
	if (::lstat(path, &stat) != 0) {
		fprintf(stderr, "Could not stat file \"%s\": %s\n", path,
			strerror(errno));
		return;
	}

	if (S_ISDIR(stat.st_mode)) {
		process_directory(path);
		return;
	}
	if (S_ISLNK(stat.st_mode))
		return;

	int file = open(path, O_RDONLY);
	if (file < 0) {
		fprintf(stderr, "Could not open file \"%s\": %s\n", path,
			strerror(errno));
		return;
	}

	status_t status = gSHA.Process(file);
	if (status != B_OK) {
		fprintf(stderr, "Computing SHA failed \"%s\": %s\n", path,
			strerror(status));
		return;
	}

	file_entry entry;
	memcpy(entry.hash, gSHA.Digest(), SHA_DIGEST_LENGTH);
	entry.node = stat.st_ino;
	entry.path = path;

	//printf("%s  %s\n", entry.HashString().c_str(), path);

	gFiles.push_back(entry);

	static bigtime_t sLastUpdate = -1;
	if (system_time() - sLastUpdate > 500000) {
		printf("%ld files scanned\33[1A\n", gFiles.size());
		sLastUpdate = system_time();
	}
}
void
process_file(const file_entry& entry, int number)
{
	struct stat stat;
	if (::stat(entry.path.c_str(), &stat) != 0) {
		fprintf(stderr, "Could not stat file \"%s\": %s\n", entry.path.c_str(),
			strerror(errno));
		return;
	}

	if (stat.st_ino != entry.node) {
		fprintf(stderr, "\"%s\": inode changed from %Ld to %Ld\n",
			entry.path.c_str(), entry.node, stat.st_ino);
	}

	int file = open(entry.path.c_str(), O_RDONLY);
	if (file < 0) {
		fprintf(stderr, "Could not open file \"%s\": %s\n", entry.path.c_str(),
			strerror(errno));
		return;
	}

	status_t status = gSHA.Process(file);
	if (status != B_OK) {
		fprintf(stderr, "Computing SHA failed \"%s\": %s\n", entry.path.c_str(),
			strerror(status));
		return;
	}

	if (memcmp(entry.hash, gSHA.Digest(), SHA_DIGEST_LENGTH))
		fprintf(stderr, "\"%s\": Contents differ!\n", entry.path.c_str());

	static bigtime_t sLastUpdate = -1;
	if (system_time() - sLastUpdate > 500000) {
		printf("%ld files scanned\33[1A\n", number);
		sLastUpdate = system_time();
	}
}