<commit_before>/*
This is a simple test program to test the facilities of the
libcvmfs C (not C++) library, which is used by Parrot and some other
tools.

The goal here is not so much to build the ultimate testing tool,
but to provide a simple build target which can verify that libcvmfs
is exporting the proper set of symbols to be used by a C program.
*/

#include "libcvmfs.h"

#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <time.h>
#include <stdlib.h>
#include <assert.h>

#define TEST_LINE_MAX 1024

void cvmfs_test_help()
{
	printf("commands are:\n");
	printf("   list <path>\n");
	printf("   cat  <path>\n");
	printf("   quit\n");
}

int cvmfs_test_list( const char *path )
{
	char filepath[TEST_LINE_MAX];
	struct stat info;

	char **buffer=0;
	size_t length=0;
	int i;

	int result = cvmfs_listdir(path,&buffer,&length);
	if(result<0) {
		fprintf(stderr,"%s: %s\n",path,strerror(errno));
		return -1;
	}


	for(i=0;buffer[i];i++) {
		snprintf(filepath,TEST_LINE_MAX,"%s/%s",path,buffer[i]);
		cvmfs_stat(filepath,&info);
		printf("%10llu %s\n",(long long unsigned)info.st_size,buffer[i]);
	}

	free(buffer);

	return 0;
}

int cvmfs_test_cat( const char *path )
{
	char buffer[TEST_LINE_MAX];

	int fd = cvmfs_open(path);
	if(fd<0) {
		fprintf(stderr,"%s: %s\n",path,strerror(errno));
		return fd;
	}

	while(1) {
		int length = read(fd,buffer,sizeof(buffer));
		if(length<=0) break;
		int retval = write(1,buffer,length);
    assert(retval == length);
	}

	cvmfs_close(fd);

	return 0;
}

int main( int argc, char *argv[] )
{
	char line[TEST_LINE_MAX];
	char path[TEST_LINE_MAX];

	const char *options = "repo_name=cms.cern.ch,url=http://cvmfs-stratum-one.cern.ch/opt/cms;http://cernvmfs.gridpp.rl.ac.uk/opt/cms;http://cvmfs.racf.bnl.gov/opt/cms,cachedir=test-libcvmfs-cache,alien_cachedir=alien-libcvmfs-cache,pubkey=cern.pubkey";

	printf("%s: initializing with options: %s\n",argv[0],options);

	if(cvmfs_init(options)!=0) {
		fprintf(stderr,"couldn't initialize cvmfs!\n");
		return -1;
	}

	while(1) {

		printf("cvmfs> ");
		fflush(stdout);

		if(!fgets(line,sizeof(line),stdin)) break;

		line[strlen(line)-1] = 0;

		if(sscanf(line,"list %s",path)==1) {
			cvmfs_test_list(path);
		} else if(sscanf(line,"cat %s",path)==1) {
			cvmfs_test_cat(path);
		} else if(!strcmp(line,"quit")) {
			break;
		} else {
			cvmfs_test_help();
		}
	}

	return 0;
}
<commit_msg>allow multiple (CERN) repos in test_libcvmfs<commit_after>/*
This is a simple test program to test the facilities of the
libcvmfs C (not C++) library, which is used by Parrot and some other
tools.

The goal here is not so much to build the ultimate testing tool,
but to provide a simple build target which can verify that libcvmfs
is exporting the proper set of symbols to be used by a C program.
*/

#include "libcvmfs.h"

#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <time.h>
#include <stdlib.h>
#include <assert.h>

#include <string>
#include <map>

#define TEST_LINE_MAX 1024

void cvmfs_test_help()
{
	printf("commands are:\n");
	printf("   attach <repo name (without .cern.ch)>\n");
	printf("   list <path>\n");
	printf("   cat  <path>\n");
	printf("   quit\n");
}

int cvmfs_test_list(cvmfs_context *ctx, const char *path)
{
	if (ctx == NULL) {
		fprintf(stderr, "%s\n", "please attach a repo first!");
		return -1;
	}

	char filepath[TEST_LINE_MAX];
	struct stat info;

	char **buffer=0;
	size_t length=0;
	int i;

	int result = cvmfs_listdir(ctx, path, &buffer, &length);
	if(result<0) {
		fprintf(stderr,"%s: %s\n",path,strerror(errno));
		return -1;
	}


	for(i=0;buffer[i];i++) {
		snprintf(filepath,TEST_LINE_MAX,"%s/%s",path,buffer[i]);
		cvmfs_stat(ctx, filepath, &info);
		printf("%10llu %s\n",(long long unsigned)info.st_size,buffer[i]);
	}

	free(buffer);

	return 0;
}

int cvmfs_test_cat(cvmfs_context *ctx, const char *path)
{
	if (ctx == NULL) {
		fprintf(stderr, "%s\n", "please attach a repo first!");
		return -1;
	}

	char buffer[TEST_LINE_MAX];

	int fd = cvmfs_open(ctx, path);
	if(fd<0) {
		fprintf(stderr,"%s: %s\n",path,strerror(errno));
		return fd;
	}

	while(1) {
		int length = read(fd,buffer,sizeof(buffer));
		if(length<=0) break;
		int retval = write(1,buffer,length);
    assert(retval == length);
	}

	cvmfs_close(ctx, fd);

	return 0;
}

cvmfs_context* cvmfs_test_attach(const char *repo_name)
{
	typedef std::map<std::string, cvmfs_context*> RepoMap;
	static RepoMap attached_repos;
	cvmfs_context *ctx = NULL;

	RepoMap::const_iterator i = attached_repos.find(repo_name);
	if (i == attached_repos.end()) {
		const char *repo_options = "repo_name=%s.cern.ch,url=http://cvmfs-stratum-one.cern.ch/opt/%s;http://cernvmfs.gridpp.rl.ac.uk/opt/%s;http://cvmfs.racf.bnl.gov/opt/%s,pubkey=/etc/cvmfs/keys/cern.ch.pub";
		char options[TEST_LINE_MAX];
		sprintf(options, repo_options, repo_name, repo_name, repo_name, repo_name);

		printf("attaching repo with options: %s\n", options);
		ctx = cvmfs_attach_repo(options);
		if (ctx == NULL) {
			fprintf(stderr,"couldn't initialize cvmfs!\n");
		} else {
			attached_repos[repo_name] = ctx;
		}
	} else {
		printf("switching to previously attached repo: %s\n", repo_name);
		ctx = i->second;
	}

	return ctx;
}


int main( int argc, char *argv[] )
{
	char line[TEST_LINE_MAX];
	char path[TEST_LINE_MAX];
	char repo_name[TEST_LINE_MAX];

	const char *global_options = "cache_directory=/tmp/test-libcvmfs-cache";

	printf("%s: initializing with options: %s\n",argv[0],global_options);
	int retval = cvmfs_init(global_options);
	if (retval != 0) {
		fprintf(stderr, "couldn't initialize libcvmfs!\n");
		return -1;
	}

	cvmfs_context *ctx = NULL;

	while(1) {

		printf("cvmfs> ");
		fflush(stdout);

		if(!fgets(line,sizeof(line),stdin)) break;

		line[strlen(line)-1] = 0;

		if(sscanf(line,"list %s",path)==1) {
			cvmfs_test_list(ctx, path);
		} else if(sscanf(line,"cat %s",path)==1) {
			cvmfs_test_cat(ctx, path);
		} else if(sscanf(line,"attach %s",repo_name)==1) {
			ctx = cvmfs_test_attach(repo_name);
		} else if(!strcmp(line,"quit")) {
			break;
		} else {
			cvmfs_test_help();
		}
	}

	cvmfs_fini(ctx);

	return 0;
}

{'fraction_non_alphanumeric': 0.16707416462917685, 'fraction_numerical': 0.008964955175224124, 'mean_word_length': 4.629357798165137, 'pattern_counts': {'":': 0, '<': 29, '<?xml version=': 0, '>': 26, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2060799', 'n_tokens_mistral': 2616, 'n_tokens_neox': 2444, 'n_words': 536}