mirror of
https://github.com/projectatomic/bubblewrap.git
synced 2026-02-06 00:45:49 +01:00
Support --make-file
This commit is contained in:
39
build-root.c
39
build-root.c
@@ -63,6 +63,7 @@ usage ()
|
||||
" --mount-proc DEST Mount procfs on DEST in the sandbox\n"
|
||||
" --mount-dev DEST Mount new dev on DEST in the sandbox\n"
|
||||
" --make-dir DEST Create dir at DEST in the sandbox\n"
|
||||
" --make-file FD DEST Copy from FD to dest DEST in the sandbox\n"
|
||||
" --make-symlink SRC DEST Create symlink at DEST in the sandbox with target SRC\n"
|
||||
" --make-passwd DEST Create trivial /etc/passwd file at DEST in the sandbox\n"
|
||||
" --make-group DEST Create trivial /etc/group file at DEST in the sandbox\n"
|
||||
@@ -296,6 +297,7 @@ typedef enum {
|
||||
SETUP_MOUNT_PROC,
|
||||
SETUP_MOUNT_DEV,
|
||||
SETUP_MAKE_DIR,
|
||||
SETUP_MAKE_FILE,
|
||||
SETUP_MAKE_SYMLINK,
|
||||
SETUP_MAKE_PASSWD,
|
||||
SETUP_MAKE_GROUP,
|
||||
@@ -307,6 +309,7 @@ struct _SetupOp {
|
||||
SetupOpType type;
|
||||
const char *source;
|
||||
const char *dest;
|
||||
int fd;
|
||||
SetupOp *next;
|
||||
};
|
||||
|
||||
@@ -319,6 +322,7 @@ setup_op_new (SetupOpType type)
|
||||
SetupOp *op = xcalloc (sizeof (SetupOp));
|
||||
|
||||
op->type = type;
|
||||
op->fd = -1;
|
||||
if (last_op != NULL)
|
||||
last_op->next = op;
|
||||
else
|
||||
@@ -518,6 +522,26 @@ main (int argc,
|
||||
argv += 1;
|
||||
argc -= 1;
|
||||
}
|
||||
else if (strcmp (arg, "--make-file") == 0)
|
||||
{
|
||||
SetupOp *op;
|
||||
int file_fd;
|
||||
char *endptr;
|
||||
|
||||
if (argc < 3)
|
||||
die ("--make-file takes two arguments");
|
||||
|
||||
file_fd = strtol (argv[1], &endptr, 10);
|
||||
if (argv[1][0] == 0 || endptr[0] != 0 || file_fd < 0)
|
||||
die ("Invalid fd: %s", argv[1]);
|
||||
|
||||
op = setup_op_new (SETUP_MAKE_FILE);
|
||||
op->fd = file_fd;
|
||||
op->dest = argv[2];
|
||||
|
||||
argv += 2;
|
||||
argc -= 2;
|
||||
}
|
||||
else if (strcmp (arg, "--make-symlink") == 0)
|
||||
{
|
||||
SetupOp *op;
|
||||
@@ -840,6 +864,21 @@ main (int argc,
|
||||
|
||||
break;
|
||||
|
||||
case SETUP_MAKE_FILE:
|
||||
{
|
||||
cleanup_fd int dest_fd = -1;
|
||||
|
||||
dest_fd = creat (dest, 0666);
|
||||
if (dest_fd == -1)
|
||||
die_with_error ("Can't create file %s", op->dest);
|
||||
|
||||
if (copy_file_data (op->fd, dest_fd) != 0)
|
||||
die_with_error ("Can't write data to file %s", op->dest);
|
||||
|
||||
close (op->fd);
|
||||
}
|
||||
break;
|
||||
|
||||
case SETUP_MAKE_SYMLINK:
|
||||
if (symlink (op->source, dest) != 0)
|
||||
die_with_error ("Can't make symlink at %s", op->dest);
|
||||
|
||||
65
utils.c
65
utils.c
@@ -330,6 +330,71 @@ create_file (const char *path,
|
||||
return res;
|
||||
}
|
||||
|
||||
#define BUFSIZE 8192
|
||||
/* Sets errno on error (!= 0), ENOSPC on short write */
|
||||
int
|
||||
copy_file_data (int sfd,
|
||||
int dfd)
|
||||
{
|
||||
char buffer[BUFSIZE];
|
||||
ssize_t bytes_read;
|
||||
|
||||
while (TRUE)
|
||||
{
|
||||
bytes_read = read (sfd, buffer, BUFSIZE);
|
||||
if (bytes_read == -1)
|
||||
{
|
||||
if (errno == EINTR)
|
||||
continue;
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (bytes_read == 0)
|
||||
break;
|
||||
|
||||
if (write_to_fd (dfd, buffer, bytes_read) != 0)
|
||||
return -1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* Sets errno on error (!= 0), ENOSPC on short write */
|
||||
int
|
||||
copy_file (const char *src_path,
|
||||
const char *dst_path,
|
||||
mode_t mode)
|
||||
{
|
||||
int sfd;
|
||||
int dfd;
|
||||
int res;
|
||||
int errsv;
|
||||
|
||||
sfd = open (src_path, O_CLOEXEC | O_RDONLY);
|
||||
if (sfd == -1)
|
||||
return -1;
|
||||
|
||||
dfd = creat (dst_path, mode);
|
||||
if (dfd == -1)
|
||||
{
|
||||
errsv = errno;
|
||||
close (sfd);
|
||||
errno = errsv;
|
||||
return -1;
|
||||
}
|
||||
|
||||
res = copy_file_data (sfd, dfd);
|
||||
|
||||
errsv = errno;
|
||||
close (sfd);
|
||||
close (dfd);
|
||||
errno = errsv;
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
|
||||
/* Sets errno on error (== NULL) */
|
||||
char *
|
||||
load_file_at (int dirfd,
|
||||
|
||||
5
utils.h
5
utils.h
@@ -81,6 +81,11 @@ int write_file_at (int dirfd,
|
||||
int write_to_fd (int fd,
|
||||
const char *content,
|
||||
ssize_t len);
|
||||
int copy_file_data (int sfd,
|
||||
int dfd);
|
||||
int copy_file (const char *src_path,
|
||||
const char *dst_path,
|
||||
mode_t mode);
|
||||
int create_file (const char *path,
|
||||
mode_t mode,
|
||||
const char *content);
|
||||
|
||||
Reference in New Issue
Block a user