From d1c3a017e3bc0f8ad991b3959ef34259065ef0d3 Mon Sep 17 00:00:00 2001 From: Thomas Schmitt Date: Sun, 3 Apr 2011 11:02:15 +0200 Subject: [PATCH] Cleaned up use of PATH_MAX (local filesystem) and LIBISOFS_NODE_PATH_MAX (ISO filesystem). --- libisofs/builder.c | 9 ++------- libisofs/filesrc.c | 2 +- libisofs/fs_image.c | 20 ++++++++------------ libisofs/fs_local.c | 11 ++++++++--- libisofs/node.h | 2 +- libisofs/stream.c | 4 ++-- libisofs/stream.h | 3 ++- libisofs/tree.c | 17 +++++++++++------ 8 files changed, 35 insertions(+), 33 deletions(-) diff --git a/libisofs/builder.c b/libisofs/builder.c index 541d26f..26c53d5 100644 --- a/libisofs/builder.c +++ b/libisofs/builder.c @@ -26,10 +26,6 @@ #include #include -#ifndef PATH_MAX -#define PATH_MAX Libisofs_default_path_maX -#endif - void iso_node_builder_ref(IsoNodeBuilder *builder) @@ -161,11 +157,10 @@ int default_create_node(IsoNodeBuilder *builder, IsoImage *image, case S_IFLNK: { /* source is a symbolic link */ - char dest[PATH_MAX + LIBISOFS_NODE_PATH_MAX]; + char dest[LIBISOFS_NODE_PATH_MAX]; IsoSymlink *link; - ret = iso_file_source_readlink(src, dest, - PATH_MAX + LIBISOFS_NODE_PATH_MAX); + ret = iso_file_source_readlink(src, dest, LIBISOFS_NODE_PATH_MAX); if (ret < 0) { break; } diff --git a/libisofs/filesrc.c b/libisofs/filesrc.c index aa663cf..21bbfc5 100644 --- a/libisofs/filesrc.c +++ b/libisofs/filesrc.c @@ -338,7 +338,7 @@ int filesrc_writer_write_data(IsoImageWriter *writer) Ecma119Image *t; IsoFileSrc *file; IsoFileSrc **filelist; - char name[PATH_MAX + LIBISOFS_NODE_PATH_MAX]; + char name[PATH_MAX]; char buffer[BLOCK_SIZE]; off_t file_size; uint32_t nblocks; diff --git a/libisofs/fs_image.c b/libisofs/fs_image.c index b5258c5..9ad2470 100644 --- a/libisofs/fs_image.c +++ b/libisofs/fs_image.c @@ -35,11 +35,6 @@ #include -#ifndef PATH_MAX -#define PATH_MAX Libisofs_default_path_maX -#endif - - /** * Options for image reading. * There are four kind of options: @@ -953,6 +948,7 @@ int ifs_readlink(IsoFileSource *src, char *buf, size_t bufsiz) { char *dest; size_t len; + int ret; ImageFileSourceData *data; if (src == NULL || buf == NULL || src->data == NULL) { @@ -971,14 +967,15 @@ int ifs_readlink(IsoFileSource *src, char *buf, size_t bufsiz) dest = (char*)data->data.content; len = strlen(dest); - if (bufsiz <= len) { + + ret = ISO_SUCCESS; + if (len >= bufsiz) { + ret = ISO_RR_PATH_TOO_LONG; len = bufsiz - 1; } - strncpy(buf, dest, len); buf[len] = '\0'; - - return ISO_SUCCESS; + return ret; } static @@ -2926,11 +2923,10 @@ int image_builder_create_node(IsoNodeBuilder *builder, IsoImage *image, case S_IFLNK: { /* source is a symbolic link */ - char dest[PATH_MAX + LIBISOFS_NODE_PATH_MAX]; + char dest[LIBISOFS_NODE_PATH_MAX]; IsoSymlink *link; - ret = iso_file_source_readlink(src, dest, - PATH_MAX + LIBISOFS_NODE_PATH_MAX); + ret = iso_file_source_readlink(src, dest, LIBISOFS_NODE_PATH_MAX); if (ret < 0) { free(name); return ret; diff --git a/libisofs/fs_local.c b/libisofs/fs_local.c index f90e9c9..275a14e 100644 --- a/libisofs/fs_local.c +++ b/libisofs/fs_local.c @@ -412,7 +412,7 @@ int lfs_readdir(IsoFileSource *src, IsoFileSource **child) static int lfs_readlink(IsoFileSource *src, char *buf, size_t bufsiz) { - int size; + int size, ret; _LocalFsFileSource *data; char *path; @@ -431,7 +431,7 @@ int lfs_readlink(IsoFileSource *src, char *buf, size_t bufsiz) * invoke readlink, with bufsiz -1 to reserve an space for * the NULL character */ - size = readlink(path, buf, bufsiz - 1); + size = readlink(path, buf, bufsiz); free(path); if (size < 0) { /* error */ @@ -455,8 +455,13 @@ int lfs_readlink(IsoFileSource *src, char *buf, size_t bufsiz) } /* NULL-terminate the buf */ + ret = ISO_SUCCESS; + if (size >= bufsiz) { + ret = ISO_RR_PATH_TOO_LONG; + size = bufsiz - 1; + } buf[size] = '\0'; - return ISO_SUCCESS; + return ret; } static diff --git a/libisofs/node.h b/libisofs/node.h index 5996e64..6761143 100644 --- a/libisofs/node.h +++ b/libisofs/node.h @@ -44,7 +44,7 @@ /* Maximum length of a path in the libisofs node tree. - Rock Ridge specs do not impose an explicit limit on name length. + Rock Ridge specs do not impose an explicit limit on path length. http://pubs.opengroup.org/onlinepubs/009695399/basedefs/limits.h.html says diff --git a/libisofs/stream.c b/libisofs/stream.c index b885d4c..9b2f0ed 100644 --- a/libisofs/stream.c +++ b/libisofs/stream.c @@ -869,8 +869,8 @@ void iso_stream_get_file_name(IsoStream *stream, char *name) if (!strncmp(type, "fsrc", 4)) { FSrcStreamData *data = stream->data; char *path = iso_file_source_get_path(data->src); - strncpy(name, path, PATH_MAX + LIBISOFS_NODE_PATH_MAX - 1); - name[PATH_MAX + LIBISOFS_NODE_PATH_MAX - 1] = 0; + strncpy(name, path, PATH_MAX - 1); + name[PATH_MAX - 1] = 0; free(path); } else if (!strncmp(type, "boot", 4)) { strcpy(name, "BOOT CATALOG"); diff --git a/libisofs/stream.h b/libisofs/stream.h index 590a8f2..913bf86 100644 --- a/libisofs/stream.h +++ b/libisofs/stream.h @@ -29,7 +29,8 @@ typedef struct /** * Get an identifier for the file of the source, for debug purposes * @param name - * Should provide at least PATH_MAX bytes + * Must provide at least PATH_MAX bytes. If no PATH_MAX is defined + * then assume PATH_MAX = Libisofs_default_path_maX from libisofs.h */ void iso_stream_get_file_name(IsoStream *stream, char *name); diff --git a/libisofs/tree.c b/libisofs/tree.c index 6cb3144..625e582 100644 --- a/libisofs/tree.c +++ b/libisofs/tree.c @@ -975,19 +975,24 @@ char *iso_tree_get_node_path(IsoNode *node) if ((IsoNode*)node->parent == node) { return strdup("/"); } else { - char path[LIBISOFS_NODE_PATH_MAX]; - char *parent_path = iso_tree_get_node_path((IsoNode*)node->parent); + char *path = NULL, *parent_path; + parent_path = iso_tree_get_node_path((IsoNode*)node->parent); if (parent_path == NULL) { return NULL; } if (strlen(parent_path) == 1) { - snprintf(path, LIBISOFS_NODE_PATH_MAX, "/%s", node->name); + path = calloc(1, strlen(node->name) + 2); + if (path == NULL) + return NULL; + sprintf(path, "/%s", node->name); } else { - snprintf(path, LIBISOFS_NODE_PATH_MAX, "%s/%s", - parent_path, node->name); + path = calloc(1, strlen(parent_path) + strlen(node->name) + 2); + if (path == NULL) + return NULL; + sprintf(path, "%s/%s", parent_path, node->name); } free(parent_path); - return strdup(path); + return path; } }