Пример #1
0
  virtual int check_test(unsigned int last_checkpoint,
      DataTestResult *test_result) override {

	init_paths();

	const int fd_bar = open(bar_path.c_str(), O_RDONLY, TEST_FILE_PERMS);
	const int fd_foo = open(foo_path.c_str(), O_RDONLY, TEST_FILE_PERMS);

	if (fd_bar >= 0 && fd_foo >= 0) {
		test_result->SetError(DataTestResult::kOldFilePersisted);
		test_result->error_description = " : Both old and new files present after crash recovery";
		close(fd_bar);
		close(fd_foo);
		return 0;
	}
	if (fd_bar < 0 && fd_foo < 0) {
		test_result->SetError(DataTestResult::kFileMissing);
		test_result->error_description = " : Both old and new files are missing after crash recovery";
		return 0;
	}

	if (fd_foo >= 0) {
		close(fd_foo);
	}

	if (fd_bar < 0 && last_checkpoint >= 1) {
		test_result->SetError(DataTestResult::kFileMissing);
		test_result->error_description = " : Unable to locate bar file after crash recovery";
		return 0;
	}

	string expected_md5sum = get_md5sum(foo_backup_path);
	string actual_md5sum = get_md5sum(bar_path);

	if (expected_md5sum.compare(actual_md5sum) != 0 && last_checkpoint >= 1) {
        test_result->SetError(DataTestResult::kFileDataCorrupted);
        test_result->error_description = " : md5sum of bar does not match with expected md5sum of foo backup";
	}

	if (fd_bar >= 0) {
		close(fd_bar);
	}
	return 0;
  }
Пример #2
0
int main()
{
        unsigned char *md5sum = NULL;
        int ret = 0;
        ret = get_md5sum("./md5.c", &md5sum);

        printf("%s\n%d", md5sum, ret);

        return 0;
}
Пример #3
0
string Util::get_file_signature(string file_path)
{
    int fd;
    long file_size = 0;
    char file_size_char[FILESIZE_CHARLEN];	// 文件大小
    struct stat file_info;
    const char* file_path_char = file_path.c_str();
    string file_signature;
    char md5_result[MD5SUM_CHARLEN];
    char buf[READ_BLOCK];
    if(	stat(file_path_char, &file_info) != -1)
    {
        /**
        	S_ISREG是否是一个常规文件.
        	S_ISLNK(st_mode):是否是一个连接.
        	S_ISDIR是否是一个目录.
        	S_ISCHR是否是一个字符设备.
        	S_ISBLK是否是一个块设备.
        	S_ISFIFO是否是一个FIFO文件.
        	S_ISSOCK是否是一个SOCKET文件.
        */
        if(S_ISREG(file_info.st_mode))
        {
            file_size = (long)file_info.st_size;

            sprintf(file_size_char, "%ld ", file_size);
        }
        else
        {
            file_signature = file_path;
            file_signature.append(" is not a regular file");

            return file_signature;
        }
    }
    else
    {
        return strerror(errno);
    }

    fd = open(file_path_char, O_RDONLY);

    if(file_size <= SMALL_FILE)
    {
        get_md5sum(fd, file_size, md5_result, buf);
        strncat(md5_result, "_", 2);
        file_signature = md5_result;
        file_signature.append(file_size_char);
    }
    else
    {
        get_md5sum(fd, FILE_BLOCK, md5_result, buf);
        strncat(md5_result, "_", 2);
        file_signature = md5_result;

        lseek(fd, file_size / 2, SEEK_SET);
        get_md5sum(fd, FILE_BLOCK, md5_result, buf);
        strncat(md5_result, "_", 2);
        file_signature.append(md5_result);

        lseek(fd, -FILE_BLOCK , SEEK_END);
        get_md5sum(fd, FILE_BLOCK, md5_result, buf);
        strncat(md5_result, "_", 2);
        file_signature.append(md5_result);

        file_signature.append(file_size_char);
    }

    close(fd);
    toLowerString(file_signature);
    return file_signature;
}