From de0c2c97e46489e18e272539de7532988bf43f03 Mon Sep 17 00:00:00 2001 From: Peng Tao Date: Thu, 11 Aug 2016 18:54:32 +0800 Subject: [PATCH] do not create _data dir without checking first container_populate_volume() checks _data existance to determine if it should populate old data. We only need to create new _data with 0777 mode if there is no existing one. That includes two cases: 1. before populating old data in container_populate_volume() 2. mount an empty volume for the fist time For file volume case, we need to chmod it instead, to make sure any user is able to read/write the file. Signed-off-by: Peng Tao --- src/container.c | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/src/container.c b/src/container.c index 269b3bc..5043d66 100644 --- a/src/container.c +++ b/src/container.c @@ -41,7 +41,7 @@ static int container_populate_volume(char *src, char *dest) return -1; } - if (hyper_mkdir(dest, 0755) < 0) { + if (hyper_mkdir(dest, 0777) < 0) { fprintf(stderr, "fail to create directroy %s\n", dest); return -1; } @@ -61,6 +61,10 @@ static int container_check_file_volume(char *hyper_path, const char **filename) *filename = NULL; num = scandir(hyper_path, &list, NULL, NULL); if (num < 0) { + /* No data in the volume yet, treat as non-file-volume */ + if (errno == ENOENT) { + return 0; + } perror("scan path failed"); return -1; } else if (num != 3) { @@ -122,12 +126,6 @@ static int container_setup_volume(struct hyper_container *container) } sprintf(volume, "/%s/_data", path); - /* 0777 so that any user can write to new volumes */ - if (hyper_mkdir(volume, 0777) < 0) { - fprintf(stderr, "fail to create directroy %s\n", volume); - return -1; - } - if (container_check_file_volume(volume, &filevolume) < 0) return -1; @@ -142,6 +140,10 @@ static int container_setup_volume(struct hyper_container *container) fprintf(stderr, "fail to populate volume %s\n", mountpoint); return -1; } + } else if (hyper_mkdir(volume, 0777) < 0) { + /* First time mounting an empty volume */ + perror("create _data dir failed"); + return -1; } } else { hyper_filize(mountpoint); @@ -150,6 +152,11 @@ static int container_setup_volume(struct hyper_container *container) return -1; } sprintf(volume, "/%s/_data/%s", path, filevolume); + /* 0777 so that any user can read/write the new file volume */ + if (chmod(volume, 0777) < 0) { + fprintf(stderr, "fail to chmod directroy %s\n", volume); + return -1; + } } if (mount(volume, mountpoint, NULL, MS_BIND, NULL) < 0) {