Skip to content
This repository was archived by the owner on Feb 8, 2021. It is now read-only.

Commit 8a86dd1

Browse files
authored
Merge pull request #144 from bergwolf/file-vol
fix up file volume binding handling
2 parents 2f4626d + 5ba9b25 commit 8a86dd1

File tree

3 files changed

+38
-0
lines changed

3 files changed

+38
-0
lines changed

src/container.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,7 @@ static int container_setup_volume(struct hyper_container *container)
144144
}
145145
}
146146
} else {
147+
hyper_filize(mountpoint);
147148
if (hyper_create_file(mountpoint) < 0) {
148149
perror("create volume file failed");
149150
return -1;

src/util.c

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,39 @@ int hyper_getgrouplist(const char *user, gid_t group, gid_t *groups, int *ngroup
208208
return ret;
209209
}
210210

211+
/* Trim all trailing '/' of a hyper_path except for the prefix one. */
212+
void hyper_filize(char *hyper_path)
213+
{
214+
char *p;
215+
216+
if (strlen(hyper_path) == 0)
217+
return;
218+
219+
p = &hyper_path[strlen(hyper_path) - 1];
220+
221+
for (; *p == '/' && p != hyper_path; p--) {
222+
*p = '\0';
223+
}
224+
}
225+
226+
static int hyper_create_parent_dir(const char *hyper_path)
227+
{
228+
char *p, *path = strdup(hyper_path);
229+
int ret = 0;
230+
231+
if (path == NULL)
232+
return -1;
233+
p = strrchr(path, '/');
234+
if (p != NULL && p != path) {
235+
*p = '\0';
236+
ret = hyper_mkdir(path, 0777);
237+
}
238+
free(path);
239+
240+
return ret;
241+
}
242+
243+
/* hyper_path must point to a file rather than a directory, e.g., having trailing '/' */
211244
int hyper_create_file(const char *hyper_path)
212245
{
213246
int fd;
@@ -220,6 +253,9 @@ int hyper_create_file(const char *hyper_path)
220253
return -1;
221254
}
222255

256+
if (hyper_create_parent_dir(hyper_path) < 0)
257+
return -1;
258+
223259
fd = open(hyper_path, O_CREAT|O_WRONLY, 0666);
224260
if (fd < 0)
225261
return -1;

src/util.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ void online_cpu(void);
2626
void online_memory(void);
2727
int hyper_cmd(char *cmd);
2828
int hyper_create_file(const char *hyper_path);
29+
void hyper_filize(char *hyper_path);
2930
int hyper_mkdir(char *path, mode_t mode);
3031
int hyper_open_channel(char *channel, int mode);
3132
int hyper_open_serial_dev(char *tty);

0 commit comments

Comments
 (0)