Add "mkpath()" helper function
authorLinus Torvalds <torvalds@g5.osdl.org>
Fri, 8 Jul 2005 23:20:59 +0000 (16:20 -0700)
committerLinus Torvalds <torvalds@g5.osdl.org>
Fri, 8 Jul 2005 23:20:59 +0000 (16:20 -0700)
I'm bored with doing it by hand all the time.

Makefile
cache.h
path.c [new file with mode: 0644]
sha1_file.c
index e142c2c4533441b14ba3cf856139409a0bbacb67..1add2ae6f5a23111cb23f2ea9d0e0e865270240c 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -56,7 +56,7 @@ install: $(PROG) $(SCRIPTS)
        $(INSTALL) $(PROG) $(SCRIPTS) $(dest)$(bin)
 
 LIB_OBJS=read-cache.o sha1_file.o usage.o object.o commit.o tree.o blob.o \
-        tag.o date.o index.o diff-delta.o patch-delta.o entry.o \
+        tag.o date.o index.o diff-delta.o patch-delta.o entry.o path.o \
         epoch.o refs.o csum-file.o pack-check.o pkt-line.o connect.o
 LIB_FILE=libgit.a
 LIB_H=cache.h object.h blob.h tree.h commit.h tag.h delta.h epoch.h csum-file.h \
diff --git a/cache.h b/cache.h
index 18584c99cf5b04345c9cc1f5015153a23197dccb..ff0321341d99c233e788f9d55fab88182fd981de 100644 (file)
--- a/cache.h
+++ b/cache.h
@@ -158,6 +158,7 @@ extern void rollback_index_file(struct cache_file *);
 #define TYPE_CHANGED    0x0040
 
 /* Return a statically allocated filename matching the sha1 signature */
+extern char *mkpath(const char *fmt, ...);
 extern char *git_path(const char *fmt, ...);
 extern char *sha1_file_name(const unsigned char *sha1);
 
diff --git a/path.c b/path.c
new file mode 100644 (file)
index 0000000..d217ef0
--- /dev/null
+++ b/path.c
@@ -0,0 +1,60 @@
+/*
+ * I'm tired of doing "vsnprintf()" etc just to open a
+ * file, so here's a "return static buffer with printf"
+ * interface for paths.
+ *
+ * It's obviously not thread-safe. Sue me. But it's quite
+ * useful for doing things like
+ *
+ *   f = open(mkpath("%s/%s.git", base, name), O_RDONLY);
+ *
+ * which is what it's designed for.
+ */
+#include "cache.h"
+
+static char pathname[PATH_MAX];
+static char bad_path[] = "/bad-path/";
+
+static char *cleanup_path(char *path)
+{
+       /* Clean it up */
+       if (!memcmp(path, "./", 2)) {
+               path += 2;
+               while (*path == '/')
+                       path++;
+       }
+       return path;
+}
+
+char *mkpath(const char *fmt, ...)
+{
+       va_list args;
+       unsigned len;
+
+       va_start(args, fmt);
+       len = vsnprintf(pathname, PATH_MAX, fmt, args);
+       va_end(args);
+       if (len >= PATH_MAX)
+               return bad_path;
+       return cleanup_path(pathname);
+}
+
+char *git_path(const char *fmt, ...)
+{
+       const char *git_dir = gitenv(GIT_DIR_ENVIRONMENT) ? : DEFAULT_GIT_DIR_ENVIRONMENT;
+       va_list args;
+       unsigned len;
+
+       len = strlen(git_dir);
+       if (len > PATH_MAX-100)
+               return bad_path;
+       memcpy(pathname, git_dir, len);
+       if (len && git_dir[len-1] != '/')
+               pathname[len++] = '/';
+       va_start(args, fmt);
+       len += vsnprintf(pathname + len, PATH_MAX - len, fmt, args);
+       va_end(args);
+       if (len >= PATH_MAX)
+               return bad_path;
+       return cleanup_path(pathname);
+}
index fd6096f4170f2db58320dc7d7e16f25b6c7fd0c9..fc4e6bf91f1b402f2e69c1521498551d5e82acb4 100644 (file)
@@ -102,34 +102,6 @@ char *get_index_file(void)
        return git_index_file;
 }
 
-char *git_path(const char *fmt, ...)
-{
-       static char pathname[PATH_MAX], *ret;
-       va_list args;
-       int len;
-
-       if (!git_dir)
-               setup_git_env();
-       len = strlen(git_dir);
-       if (len > PATH_MAX-100)
-               return "pad-path";
-       memcpy(pathname, git_dir, len);
-       if (len && git_dir[len-1] != '/')
-               pathname[len++] = '/';
-       va_start(args, fmt);
-       vsnprintf(pathname + len, sizeof(pathname) - len, fmt, args);
-       va_end(args);
-       ret = pathname;
-
-       /* Clean it up */
-       if (!memcmp(pathname, "./", 2)) {
-               ret += 2;
-               while (*ret == '/')
-                       ret++;
-       }
-       return ret;
-}
-
 int safe_create_leading_directories(char *path)
 {
        char *pos = path;