From 5ba9b2550da4ad57f68f5300b119d6f27b573c36 Mon Sep 17 00:00:00 2001 From: Peng Tao Date: Wed, 10 Aug 2016 18:03:52 +0800 Subject: [PATCH] handle directory destination for a file volume binding If the destination directoroy does not exist in the base image, create it as a file instead, following docker's practice. [hyperpublic@hyperhq]$docker run -t -i -v /home/bergwolf/bin/procmuttmail:/vol/data/ busybox / # ls -l /vol/data -rwxr-xr-x 1 1000 1000 28 Apr 17 05:38 /vol/data / # Signed-off-by: Peng Tao --- src/container.c | 1 + src/util.c | 16 ++++++++++++++++ src/util.h | 1 + 3 files changed, 18 insertions(+) diff --git a/src/container.c b/src/container.c index 02e6283..269b3bc 100644 --- a/src/container.c +++ b/src/container.c @@ -144,6 +144,7 @@ static int container_setup_volume(struct hyper_container *container) } } } else { + hyper_filize(mountpoint); if (hyper_create_file(mountpoint) < 0) { perror("create volume file failed"); return -1; diff --git a/src/util.c b/src/util.c index 2308c68..2256fd8 100644 --- a/src/util.c +++ b/src/util.c @@ -208,6 +208,21 @@ int hyper_getgrouplist(const char *user, gid_t group, gid_t *groups, int *ngroup return ret; } +/* Trim all trailing '/' of a hyper_path except for the prefix one. */ +void hyper_filize(char *hyper_path) +{ + char *p; + + if (strlen(hyper_path) == 0) + return; + + p = &hyper_path[strlen(hyper_path) - 1]; + + for (; *p == '/' && p != hyper_path; p--) { + *p = '\0'; + } +} + static int hyper_create_parent_dir(const char *hyper_path) { char *p, *path = strdup(hyper_path); @@ -225,6 +240,7 @@ static int hyper_create_parent_dir(const char *hyper_path) return ret; } +/* hyper_path must point to a file rather than a directory, e.g., having trailing '/' */ int hyper_create_file(const char *hyper_path) { int fd; diff --git a/src/util.h b/src/util.h index 7a1eeb1..18ab657 100644 --- a/src/util.h +++ b/src/util.h @@ -26,6 +26,7 @@ void online_cpu(void); void online_memory(void); int hyper_cmd(char *cmd); int hyper_create_file(const char *hyper_path); +void hyper_filize(char *hyper_path); int hyper_mkdir(char *path, mode_t mode); int hyper_open_channel(char *channel, int mode); int hyper_open_serial_dev(char *tty);