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

Commit 0282d55

Browse files
committed
simplify hyper_mkdir_at
modify hyper_path directly to save one strdup, and move common part of caller code inside. Signed-off-by: Peng Tao <bergwolf@gmail.com>
1 parent b4dae32 commit 0282d55

File tree

3 files changed

+20
-26
lines changed

3 files changed

+20
-26
lines changed

src/container.c

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,6 @@ static int container_setup_volume(struct hyper_container *container)
102102
for (i = 0; i < container->vols_num; i++) {
103103
char volume[512];
104104
char mountpoint[512];
105-
char *expath;
106105
char *options = NULL;
107106
const char *filevolume = NULL;
108107
vol = &container->vols[i];
@@ -145,14 +144,9 @@ static int container_setup_volume(struct hyper_container *container)
145144
return -1;
146145

147146
if (filevolume == NULL) {
148-
expath = hyper_mkdir_at(".", mountpoint, 0755);
149-
if (expath == NULL) {
150-
perror("create volume dir failed");
147+
if (hyper_mkdir_at(".", mountpoint, sizeof(mountpoint)) < 0) {
148+
perror("create map dir failed");
151149
return -1;
152-
} else {
153-
/* ensure mountpoint is reachable */
154-
sprintf(mountpoint, "%s", expath);
155-
free(expath);
156150
}
157151
if (vol->docker) {
158152
if (container->initialize &&
@@ -195,7 +189,7 @@ static int container_setup_volume(struct hyper_container *container)
195189

196190
for (i = 0; i < container->maps_num; i++) {
197191
struct stat st;
198-
char *src, *expath, path[512], volume[512];
192+
char *src, path[512], volume[512];
199193
struct fsmap *map = &container->maps[i];
200194
char mountpoint[512];
201195

@@ -207,14 +201,9 @@ static int container_setup_volume(struct hyper_container *container)
207201
stat(src, &st);
208202

209203
if (st.st_mode & S_IFDIR) {
210-
expath = hyper_mkdir_at(".", mountpoint, 0755);
211-
if (expath == NULL) {
204+
if (hyper_mkdir_at(".", mountpoint, sizeof(mountpoint)) < 0) {
212205
perror("create map dir failed");
213206
return -1;
214-
} else {
215-
/* ensure mountpoint is reachable */
216-
sprintf(mountpoint, "%s", expath);
217-
free(expath);
218207
}
219208
if (map->docker) {
220209
/* converted from volume */

src/util.c

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -325,14 +325,14 @@ static char *hyper_resolve_link(char *path)
325325
* as if we were in a chroot jail.
326326
*
327327
* @root: chroot jail path.
328-
* @parent: what's already created to the target directory.
329328
* @path: target directory. It is always relative to @root even if it starts with /.
329+
* @parent: what's already created to the target directory.
330330
* @mode: directory mode.
331331
* @link_max: max number of symlinks to follow.
332332
*
333333
* Upon success, @parent is changed to point to resolved path name.
334334
*/
335-
static int hyper_mkdir_follow_link(char *root, char *parent, char *path,
335+
static int hyper_mkdir_follow_link(const char *root, const char *path, char *parent,
336336
mode_t mode, int *link_max)
337337
{
338338
char *comp, *prev, *link, *dummy, *npath, *delim = "/";
@@ -396,7 +396,7 @@ static int hyper_mkdir_follow_link(char *root, char *parent, char *path,
396396
} else {
397397
*prev = '\0'; /* drop current comp */
398398
}
399-
if (hyper_mkdir_follow_link(root, parent, link, mode, link_max) < 0) {
399+
if (hyper_mkdir_follow_link(root, link, parent, mode, link_max) < 0) {
400400
free(link);
401401
goto out;
402402
}
@@ -425,21 +425,26 @@ static int hyper_mkdir_follow_link(char *root, char *parent, char *path,
425425
/*
426426
* hyper_mkdir_at() is similar to hyper_mkdir() with the exception that
427427
* when there are symlinks in the path components, it acts as if we created
428-
* directories in a chroot jail. @path is always considered relative to root
429-
* even if it starts with a leading stash ('/').
428+
* directories in a chroot jail. @hyper_path is always considered relative
429+
* to root even if it starts with a leading stash ('/').
430430
*
431-
* Upon success, return symlink expanded result.
431+
* Upon success, @hyper_path is modified to save resolved path in chroot jail.
432432
*/
433-
char *hyper_mkdir_at(char *root, char *path, mode_t mode)
433+
int hyper_mkdir_at(const char *root, char *hyper_path, int size)
434434
{
435435
char result[512];
436436
int max_link = 40;
437437

438438
sprintf(result, "%s", root);
439-
if (hyper_mkdir_follow_link(root, result, path, mode, &max_link) < 0)
440-
return NULL;
439+
if (hyper_mkdir_follow_link(root, hyper_path, result, 0755, &max_link) < 0)
440+
return -1;
441441

442-
return strdup(result);
442+
if (strlen(result) + 1 > size) {
443+
errno = ENAMETOOLONG;
444+
return -1;
445+
}
446+
sprintf(hyper_path, "%s", result);
447+
return 0;
443448
}
444449

445450
int hyper_mkdir(char *path, mode_t mode)

src/util.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ int hyper_cmd(char *cmd);
2929
int hyper_create_file(const char *hyper_path);
3030
void hyper_filize(char *hyper_path);
3131
int hyper_mkdir(char *path, mode_t mode);
32-
char *hyper_mkdir_at(char *root, char *path, mode_t mode);
32+
int hyper_mkdir_at(const char *root, char *path, int size);
3333
int hyper_write_file(const char *path, const char *value, size_t len);
3434
int hyper_open_channel(char *channel, int mode);
3535
int hyper_setfd_cloexec(int fd);

0 commit comments

Comments
 (0)