<commit_before>package main

import (
	"fmt"

	"github.com/spf13/cobra"
)

/*
#define _GNU_SOURCE
#include <dirent.h>
#include <errno.h>
#include <fcntl.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <unistd.h>
#include <limits.h>

extern char* advance_arg(bool required);
extern void error(char *msg);
extern void attach_userns(int pid);
extern int dosetns(int pid, char *nstype);

int copy(int target, int source, bool append)
{
	ssize_t n;
	char buf[1024];

	if (!append && ftruncate(target, 0) < 0) {
		error("error: truncate");
		return -1;
	}

	if (append && lseek(target, 0, SEEK_END) < 0) {
		error("error: seek");
		return -1;
	}

	while ((n = read(source, buf, 1024)) > 0) {
		if (write(target, buf, n) != n) {
			error("error: write");
			return -1;
		}
	}

	if (n < 0) {
		error("error: read");
		return -1;
	}

	return 0;
}

int manip_file_in_ns(char *rootfs, int pid, char *host, char *container, bool is_put, char *type, uid_t uid, gid_t gid, mode_t mode, uid_t defaultUid, gid_t defaultGid, mode_t defaultMode, bool append) {
	int host_fd = -1, container_fd = -1;
	int ret = -1;
	int container_open_flags;
	struct stat st;
	int exists = 1;
	bool is_dir_manip = type != NULL && !strcmp(type, "directory");
	bool is_symlink_manip = type != NULL && !strcmp(type, "symlink");
	char link_target[PATH_MAX];
	ssize_t link_length;

	if (!is_dir_manip && !is_symlink_manip) {
		host_fd = open(host, O_RDWR);
		if (host_fd < 0) {
			error("error: open");
			return -1;
		}
	}

	if (pid > 0) {
		attach_userns(pid);

		if (dosetns(pid, "mnt") < 0) {
			error("error: setns");
			goto close_host;
		}
	} else {
		if (chroot(rootfs) < 0) {
			error("error: chroot");
			goto close_host;
		}

		if (chdir("/") < 0) {
			error("error: chdir");
			goto close_host;
		}
	}

	if (is_put && is_dir_manip) {
		if (mode == -1) {
			mode = defaultMode;
		}

		if (uid == -1) {
			uid = defaultUid;
		}

		if (gid == -1) {
			gid = defaultGid;
		}

		if (mkdir(container, mode) < 0 && errno != EEXIST) {
			error("error: mkdir");
			return -1;
		}

		if (chown(container, uid, gid) < 0) {
			error("error: chown");
			return -1;
		}

		return 0;
	}

	if (is_put && is_symlink_manip) {
		if (mode == -1) {
			mode = defaultMode;
		}

		if (uid == -1) {
			uid = defaultUid;
		}

		if (gid == -1) {
			gid = defaultGid;
		}

		if (symlink(host, container) < 0 && errno != EEXIST) {
			error("error: symlink");
			return -1;
		}

		if (fchownat(0, container, uid, gid, AT_SYMLINK_NOFOLLOW) < 0) {
			error("error: chown");
			return -1;
		}

		return 0;
	}

	if (fstatat(AT_FDCWD, container, &st, AT_SYMLINK_NOFOLLOW) < 0)
		exists = 0;

	container_open_flags = O_RDWR;
	if (is_put)
		container_open_flags |= O_CREAT;

	if (is_put && !is_dir_manip && exists && S_ISDIR(st.st_mode)) {
		error("error: Path already exists as a directory");
		goto close_host;
	}

	if (exists && S_ISDIR(st.st_mode))
		container_open_flags = O_DIRECTORY;

	if (!is_put && exists && S_ISLNK(st.st_mode)) {
		fprintf(stderr, "uid: %ld\n", (long)st.st_uid);
		fprintf(stderr, "gid: %ld\n", (long)st.st_gid);
		fprintf(stderr, "mode: %ld\n", (unsigned long)st.st_mode & (S_IRWXU | S_IRWXG | S_IRWXO));
		fprintf(stderr, "type: symlink\n");

		link_length = readlink(container, link_target, PATH_MAX);
		if (link_length < 0 || link_length >= PATH_MAX) {
			error("error: readlink");
			goto close_host;
		}
		link_target[link_length] = '\0';

		dprintf(host_fd, "%s\n", link_target);
		goto close_container;
	}

	umask(0);
	container_fd = open(container, container_open_flags, 0);
	if (container_fd < 0) {<commit_msg>forkfile: only open O_RDWR if necessary<commit_after>package main

import (
	"fmt"

	"github.com/spf13/cobra"
)

/*
#define _GNU_SOURCE
#include <dirent.h>
#include <errno.h>
#include <fcntl.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <unistd.h>
#include <limits.h>

extern char* advance_arg(bool required);
extern void error(char *msg);
extern void attach_userns(int pid);
extern int dosetns(int pid, char *nstype);

int copy(int target, int source, bool append)
{
	ssize_t n;
	char buf[1024];

	if (!append && ftruncate(target, 0) < 0) {
		error("error: truncate");
		return -1;
	}

	if (append && lseek(target, 0, SEEK_END) < 0) {
		error("error: seek");
		return -1;
	}

	while ((n = read(source, buf, 1024)) > 0) {
		if (write(target, buf, n) != n) {
			error("error: write");
			return -1;
		}
	}

	if (n < 0) {
		error("error: read");
		return -1;
	}

	return 0;
}

int manip_file_in_ns(char *rootfs, int pid, char *host, char *container, bool is_put, char *type, uid_t uid, gid_t gid, mode_t mode, uid_t defaultUid, gid_t defaultGid, mode_t defaultMode, bool append) {
	int host_fd = -1, container_fd = -1;
	int ret = -1;
	int container_open_flags;
	struct stat st;
	int exists = 1;
	bool is_dir_manip = type != NULL && !strcmp(type, "directory");
	bool is_symlink_manip = type != NULL && !strcmp(type, "symlink");
	char link_target[PATH_MAX];
	ssize_t link_length;

	if (!is_dir_manip && !is_symlink_manip) {
		if (is_put)
			host_fd = open(host, O_RDONLY);
		else
			host_fd = open(host, O_RDWR);
		if (host_fd < 0) {
			error("error: open");
			return -1;
		}
	}

	if (pid > 0) {
		attach_userns(pid);

		if (dosetns(pid, "mnt") < 0) {
			error("error: setns");
			goto close_host;
		}
	} else {
		if (chroot(rootfs) < 0) {
			error("error: chroot");
			goto close_host;
		}

		if (chdir("/") < 0) {
			error("error: chdir");
			goto close_host;
		}
	}

	if (is_put && is_dir_manip) {
		if (mode == -1) {
			mode = defaultMode;
		}

		if (uid == -1) {
			uid = defaultUid;
		}

		if (gid == -1) {
			gid = defaultGid;
		}

		if (mkdir(container, mode) < 0 && errno != EEXIST) {
			error("error: mkdir");
			return -1;
		}

		if (chown(container, uid, gid) < 0) {
			error("error: chown");
			return -1;
		}

		return 0;
	}

	if (is_put && is_symlink_manip) {
		if (mode == -1) {
			mode = defaultMode;
		}

		if (uid == -1) {
			uid = defaultUid;
		}

		if (gid == -1) {
			gid = defaultGid;
		}

		if (symlink(host, container) < 0 && errno != EEXIST) {
			error("error: symlink");
			return -1;
		}

		if (fchownat(0, container, uid, gid, AT_SYMLINK_NOFOLLOW) < 0) {
			error("error: chown");
			return -1;
		}

		return 0;
	}

	if (fstatat(AT_FDCWD, container, &st, AT_SYMLINK_NOFOLLOW) < 0)
		exists = 0;

	if (is_put)
		container_open_flags = O_RDWR | O_CREAT;
	else
		container_open_flags = O_RDONLY;

	if (is_put && !is_dir_manip && exists && S_ISDIR(st.st_mode)) {
		error("error: Path already exists as a directory");
		goto close_host;
	}

	if (exists && S_ISDIR(st.st_mode))
		container_open_flags = O_DIRECTORY;

	if (!is_put && exists && S_ISLNK(st.st_mode)) {
		fprintf(stderr, "uid: %ld\n", (long)st.st_uid);
		fprintf(stderr, "gid: %ld\n", (long)st.st_gid);
		fprintf(stderr, "mode: %ld\n", (unsigned long)st.st_mode & (S_IRWXU | S_IRWXG | S_IRWXO));
		fprintf(stderr, "type: symlink\n");

		link_length = readlink(container, link_target, PATH_MAX);
		if (link_length < 0 || link_length >= PATH_MAX) {
			error("error: readlink");
			goto close_host;
		}
		link_target[link_length] = '\0';

		dprintf(host_fd, "%s\n", link_target);
		goto close_container;
	}

	umask(0);
	container_fd = open(container, container_open_flags, 0);
	if (container_fd < 0) {
{'fraction_non_alphanumeric': 0.16761286834279535, 'fraction_numerical': 0.014868883482022168, 'mean_word_length': 3.544840294840295, 'pattern_counts': {'":': 0, '<': 51, '<?xml version=': 0, '>': 29, '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': '5246128', 'n_tokens_mistral': 3476, 'n_tokens_neox': 3098, 'n_words': 737}