Example #1
0
static int file_tree_callback(const char *fpath, const struct stat *sb, int typeflag, __unused__ struct FTW *ftwbuf)
{
	if (typeflag == FTW_DNR)
		return FTW_CONTINUE;

	if (typeflag == FTW_NS)
		return FTW_CONTINUE;

	if (ignore_files(fpath))
		return FTW_SKIP_SUBTREE;

	// Check we can read it.
	if (check_stat_file(sb) == -1)
		return FTW_CONTINUE;

	if (shm->exit_reason != STILL_RUNNING)
		return FTW_STOP;

	add_to_namelist(fpath);
	files_in_index++;

	return FTW_CONTINUE;
}
Example #2
0
void open_fds(char *dir)
{
	char b[4096];
	int openflag, fd, r;
	DIR *d = opendir(dir);
	struct dirent *de;
	struct stat buf;
	char *modestr;
	unsigned int chance;

	if (!d) {
		printf("can't open %s\n", dir);
		return;
	}
	while ((de = readdir(d))) {

		memset(&buf, 0, sizeof(struct stat));
		snprintf(b, sizeof(b), "%s/%s", dir, de->d_name);
		if (ignore_files(de->d_name))
			continue; /*".", "..", everything that's not a regular file or directory !*/
		r = lstat(b,&buf);
		if (r == -1)
			continue;
		openflag = 0;
		if (S_ISLNK(buf.st_mode))
			continue;
		if (S_ISFIFO(buf.st_mode))
			continue;
		//if (S_ISREG(buf.st_mode))
		//	continue;
		if (S_ISDIR(buf.st_mode)) {
			/* probability of adding a directory to the list. */
			chance = 5;
			openflag = O_RDONLY;
			if (buf.st_uid != getuid()) {
				/* We don't own the dir, is it group/other readable ? */
				if (buf.st_mode & (S_IRGRP|S_IROTH)) {
					open_fds(b);
					goto openit;
				}
			} else {
				/* We own this dir. */
				open_fds(b);
				goto openit;
			}
		} else {
			int mode_was_set = 0;

			/* if we own the file, unlikely, since you should NOT run this thing as root */
			if (buf.st_uid == getuid()) {
				if (buf.st_mode & S_IRUSR) {
					openflag &= O_RDONLY;
					mode_was_set = 1;
				}
				if (buf.st_mode & S_IWUSR) {
					openflag |= O_WRONLY;
					mode_was_set = 1;
				}
			} else if (buf.st_gid == getgid()) {
				if (buf.st_mode & S_IRGRP) {
					openflag &= O_RDONLY;
					mode_was_set = 1;
				}
				if (buf.st_mode & S_IWGRP) {
					openflag |= O_WRONLY;
					mode_was_set = 1;
				}
			} else {
				if (buf.st_mode & S_IROTH) {
					openflag &= O_RDONLY;
					mode_was_set = 1;
				}
				if (buf.st_mode & S_IWOTH) {
					openflag |= O_WRONLY;
					mode_was_set = 1;
				}
			}
			//if (strcmp(de->d_name, "sr0") == 0) {
			//	printf("sr0 mode = %o\n", buf.st_mode);
			//}

			if (!mode_was_set) {
				//printf("couldn't find a mode to open %s\n", b);
				continue;
			}

			if ((openflag & O_RDONLY) && (openflag & O_WRONLY))
				openflag = O_RDWR;

			/* files have a higher probability of success than directories
			 * also, writable files are probably more 'fun' */
			switch (openflag) {
			case O_RDONLY:	chance = 10; break;
			case O_WRONLY:	chance = 100; break;
			case O_RDWR:	chance = 100; break;
			}
openit:
			if (fds_left_to_create == 0)
				break;

			fd = add_fd(chance, b, openflag);
			if (fd == -1)
				continue;

			switch (openflag) {
			case O_RDONLY:	modestr = "read-only";	break;
			case O_WRONLY:	modestr = "write-only";	break;
			case O_RDWR:	modestr = "read-write";	break;
			}
			output("fd[%i] = %s (%s)\n", fd, b, modestr);
			fds[fd_idx++] = fd;
			fds_left_to_create--;
		}
	}
	closedir(d);
}