Ejemplo n.º 1
0
static int db_readdir(backend_store_interface* i, const char* rel_path,
		void* buf, fuse_fill_dir_t filler, off_t offset, DIR* dir) {
	size_t j;
	size_t num_files = 0;
	char** files;

	logger_logf(LOG(i), "rel_path = %s", rel_path);

	ERR_IF_DOESNT_EXIST(i, rel_path);

	logger_logf(LOG(i), "passing rel_path = %s to get_directory_files", rel_path);
	files = get_directory_files(rel_path, &num_files);

	for (j = 0; j < num_files; ++j) {
		logger_logf(LOG(i), "iterating over %s", files[j]);

		/* Need to determine full path to file (not path relative to current
		 * working directory) to decide if user can ascertain its existence */
		size_t rel_path_len = strlen(rel_path);
		size_t full_path_len = rel_path_len + strlen(files[j]) + 2;
		char full_path[full_path_len];
		full_path[0] = '\0';
		strcat(full_path, rel_path);
		if (rel_path_len != 1) { //if not slash (root directory)
			strcat(full_path, "/");
		}
		strcat(full_path, files[j]);

		logger_logf(LOG(i), "full path = %s", full_path);

		/* Only show user files they are allowed to ascertain the existence of */
		if (!db_can_ascertain_existence(i, full_path)) {
			continue;
		}

		if (filler(buf, files[j], NULL, 0) != 0) {
			logger_logf(LOG(i), "filled failed");
			return -ENOMEM;
		}
	}

	for (j = 0; j < num_files; ++j) {
		free(files[j]);
	}
	free(files);

    return 0;
}
Ejemplo n.º 2
0
int main(int argc, char * argv[])
{
	char opt;
	opt = getopt(argc, argv, optstring);
	int l_flag;

	while (opt!=-1) 
	{
		switch(opt) {
			case 'l':
				l_flag=1;
				break;
			}
		opt = getopt(argc, argv, optstring);
	} 

	const char * pathname;
	// Get the last argument - the pathname
	if (argc >= 3)
		pathname = argv[argc-1];
	else if ( (argc == 2) & l_flag )
		pathname = getenv("PWD");
	else if (argc == 1)
		pathname = getenv("PWD");
	else
		pathname = argv[argc-1];

	struct dirent * directory_files = 
		get_directory_files(pathname);

	if (!directory_files)
	{
		perror("Error opening directory");
		return -1;
	}

	if (l_flag)
	{
		for (int j=0; j< CURRENT_DIRECTORY_SIZE; j++)
		{
			fflush(stdout);
			if ( print_file_information_long(pathname, directory_files[j].d_name) < 0)
			{
				perror("Error reading file");
				printf("File name: %s%c%s\n", pathname, '/', directory_files[j].d_name);
				continue;
			}
		}
	}
	else
	{
		for (int j=0; j < CURRENT_DIRECTORY_SIZE; j++)
		{
			printf("%s\n",directory_files[j].d_name);
		}
	}

	free(directory_files);

	return (0);
}