From: Nguyen Thai Ngoc Duy <pclouds@gmail.com>
To: Jonathan Nieder <jrnieder@gmail.com>
Cc: Ramkumar Ramachandra <artagnon@gmail.com>,
Junio C Hamano <gitster@pobox.com>,
Git List <git@vger.kernel.org>
Subject: Re: [PATCH/RFC] introduce strbuf_addpath()
Date: Fri, 18 Nov 2011 08:42:35 +0700 [thread overview]
Message-ID: <20111118014235.GA10917@tre> (raw)
In-Reply-To: <20111116215004.GA29872@elie.hsd1.il.comcast.net>
On Wed, Nov 16, 2011 at 03:50:04PM -0600, Jonathan Nieder wrote:
> strbuf_addpath() is like git_path_unsafe(), except instead of
> returning its own buffer, it appends its result to a buffer provided
> by the caller.
>
> Benefits:
>
> - Since it uses a caller-supplied buffer, unlike git_path_unsafe(),
> there is no risk that one call will clobber the result from
> another.
>
> - Unlike git_pathdup(), it does not need to waste time allocating
> memory in the middle of your tight loop over refs.
>
> - The size of the result is not limited to PATH_MAX.
>
> Caveat: the size of its result is not limited to PATH_MAX. Existing
> code might be relying on git_path*() to produce a result that is safe
> to copy to a PATH_MAX-sized buffer. Be careful.
>
> This patch introduces the strbuf_addpath() function and converts a few
> existing users of the strbuf_addstr(git_path(...)) idiom to
> demonstrate the API.
>
> Signed-off-by: Jonathan Nieder <jrnieder@gmail.com>
> ---
> Jonathan Nieder wrote:
>
> > I think if I ran the world, the fundamental operation would be
> > strbuf_addpath().
>
> Like this, maybe.
>
If I ran the world, I would change get_pathname() to return "struct
strbuf *" instead and change "char *git_path(..)" to "const char *git_path(...)".
Code paths that want to modify git_path() return value could
just use strbuf_addpath()
I did try to turn git_path to return const char *, the following patch
seems to make it build without any warnings
-- 8< --
diff --git a/builtin/fetch-pack.c b/builtin/fetch-pack.c
index c6bc8eb..fd7c682 100644
--- a/builtin/fetch-pack.c
+++ b/builtin/fetch-pack.c
@@ -1041,7 +1041,7 @@ struct ref *fetch_pack(struct fetch_pack_args *my_args,
if (args.depth > 0) {
struct cache_time mtime;
struct strbuf sb = STRBUF_INIT;
- char *shallow = git_path("shallow");
+ const char *shallow = git_path("shallow");
int fd;
mtime.sec = st.st_mtime;
diff --git a/builtin/fetch.c b/builtin/fetch.c
index 91731b9..261f1a0 100644
--- a/builtin/fetch.c
+++ b/builtin/fetch.c
@@ -367,7 +367,8 @@ static int store_updated_refs(const char *raw_url, const char *remote_name,
char note[1024];
const char *what, *kind;
struct ref *rm;
- char *url, *filename = dry_run ? "/dev/null" : git_path("FETCH_HEAD");
+ const char *filename = dry_run ? "/dev/null" : git_path("FETCH_HEAD");
+ char *url;
fp = fopen(filename, "a");
if (!fp)
@@ -647,7 +648,7 @@ static void check_not_current_branch(struct ref *ref_map)
static int truncate_fetch_head(void)
{
- char *filename = git_path("FETCH_HEAD");
+ const char *filename = git_path("FETCH_HEAD");
FILE *fp = fopen(filename, "w");
if (!fp)
diff --git a/builtin/fsck.c b/builtin/fsck.c
index 0e0e17a..f9624d7 100644
--- a/builtin/fsck.c
+++ b/builtin/fsck.c
@@ -215,12 +215,12 @@ static void check_unreachable_object(struct object *obj)
printf("dangling %s %s\n", typename(obj->type),
sha1_to_hex(obj->sha1));
if (write_lost_and_found) {
- char *filename = git_path("lost-found/%s/%s",
+ const char *filename = git_path("lost-found/%s/%s",
obj->type == OBJ_COMMIT ? "commit" : "other",
sha1_to_hex(obj->sha1));
FILE *f;
- if (safe_create_leading_directories(filename)) {
+ if (safe_create_leading_directories_const(filename)) {
error("Could not create lost-found");
return;
}
diff --git a/builtin/remote.c b/builtin/remote.c
index 583eec9..0662d37 100644
--- a/builtin/remote.c
+++ b/builtin/remote.c
@@ -587,7 +587,7 @@ static int migrate_file(struct remote *remote)
{
struct strbuf buf = STRBUF_INIT;
int i;
- char *path = NULL;
+ const char *path = NULL;
strbuf_addf(&buf, "remote.%s.url", remote->name);
for (i = 0; i < remote->url_nr; i++)
diff --git a/fast-import.c b/fast-import.c
index 8d8ea3c..4293a9f 100644
--- a/fast-import.c
+++ b/fast-import.c
@@ -403,7 +403,7 @@ static void dump_marks_helper(FILE *, uintmax_t, struct mark_set *);
static void write_crash_report(const char *err)
{
- char *loc = git_path("fast_import_crash_%"PRIuMAX, (uintmax_t) getpid());
+ const char *loc = git_path("fast_import_crash_%"PRIuMAX, (uintmax_t) getpid());
FILE *rpt = fopen(loc, "w");
struct branch *b;
unsigned long lu;
diff --git a/notes-merge.c b/notes-merge.c
index e33c2c9..8da2e0a 100644
--- a/notes-merge.c
+++ b/notes-merge.c
@@ -288,7 +288,7 @@ static void check_notes_merge_worktree(struct notes_merge_options *o)
"(%s exists).", git_path("NOTES_MERGE_*"));
}
- if (safe_create_leading_directories(git_path(
+ if (safe_create_leading_directories_const(git_path(
NOTES_MERGE_WORKTREE "/.test")))
die_errno("unable to create directory %s",
git_path(NOTES_MERGE_WORKTREE));
@@ -303,8 +303,8 @@ static void write_buf_to_worktree(const unsigned char *obj,
const char *buf, unsigned long size)
{
int fd;
- char *path = git_path(NOTES_MERGE_WORKTREE "/%s", sha1_to_hex(obj));
- if (safe_create_leading_directories(path))
+ const char *path = git_path(NOTES_MERGE_WORKTREE "/%s", sha1_to_hex(obj));
+ if (safe_create_leading_directories_const(path))
die_errno("unable to create directory for '%s'", path);
if (file_exists(path))
die("found existing file at '%s'", path);
diff --git a/refs.c b/refs.c
index 62d8a37..7469cf1 100644
--- a/refs.c
+++ b/refs.c
@@ -1189,7 +1189,7 @@ int dwim_log(const char *str, int len, unsigned char *sha1, char **log)
static struct ref_lock *lock_ref_sha1_basic(const char *ref, const unsigned char *old_sha1, int flags, int *type_p)
{
- char *ref_file;
+ const char *ref_file;
const char *orig_ref = ref;
struct ref_lock *lock;
int last_errno = 0;
@@ -1250,7 +1250,7 @@ static struct ref_lock *lock_ref_sha1_basic(const char *ref, const unsigned char
if ((flags & REF_NODEREF) && (type & REF_ISSYMREF))
lock->force_write = 1;
- if (safe_create_leading_directories(ref_file)) {
+ if (safe_create_leading_directories_const(ref_file)) {
last_errno = errno;
error("unable to create directory for %s", ref_file);
goto error_return;
@@ -1411,7 +1411,7 @@ int rename_ref(const char *oldref, const char *newref, const char *logmsg)
}
}
- if (log && safe_create_leading_directories(git_path("logs/%s", newref))) {
+ if (log && safe_create_leading_directories_const(git_path("logs/%s", newref))) {
error("unable to create directory for %s", newref);
goto rollback;
}
-- 8< --
next prev parent reply other threads:[~2011-11-18 1:38 UTC|newest]
Thread overview: 47+ messages / expand[flat|nested] mbox.gz Atom feed top
2011-11-05 16:29 [PATCH 0/5] Sequencer: working around historical mistakes Ramkumar Ramachandra
2011-11-05 16:29 ` [PATCH 1/5] sequencer: factor code out of revert builtin Ramkumar Ramachandra
2011-11-06 0:12 ` Jonathan Nieder
2011-11-13 10:40 ` Ramkumar Ramachandra
2011-11-13 23:10 ` Junio C Hamano
2011-11-15 9:00 ` Ramkumar Ramachandra
2011-11-15 9:18 ` Miles Bader
2011-11-15 9:47 ` Jonathan Nieder
2011-11-05 16:29 ` [PATCH 2/5] sequencer: remove CHERRY_PICK_HEAD with sequencer state Ramkumar Ramachandra
2011-11-06 0:15 ` Jonathan Nieder
2011-11-05 16:29 ` [PATCH 3/5] sequencer: sequencer state is useless without todo Ramkumar Ramachandra
2011-11-06 0:26 ` Jonathan Nieder
2011-11-13 10:44 ` Ramkumar Ramachandra
2011-11-13 20:50 ` Junio C Hamano
2011-11-15 9:13 ` Ramkumar Ramachandra
2011-11-15 9:52 ` Jonathan Nieder
2011-11-15 16:27 ` Junio C Hamano
2011-11-16 6:17 ` Ramkumar Ramachandra
2011-11-16 7:38 ` Junio C Hamano
2011-11-16 7:59 ` [PATCH 0/3] avoiding unintended consequences of git_path() usage Jonathan Nieder
2011-11-16 8:03 ` [PATCH 1/3] do not let git_path clobber errno when reporting errors Jonathan Nieder
2011-11-16 8:04 ` [PATCH 2/3] Bigfile: dynamically allocate buffer for marks file name Jonathan Nieder
2011-11-16 8:07 ` [PATCH 3/3] rename git_path() to git_path_unsafe() Jonathan Nieder
2011-11-17 1:20 ` Junio C Hamano
2011-11-17 7:03 ` Jonathan Nieder
2011-11-16 8:37 ` [PATCH 0/3] avoiding unintended consequences of git_path() usage Nguyen Thai Ngoc Duy
2011-11-16 8:42 ` Nguyen Thai Ngoc Duy
2011-11-16 8:59 ` Jonathan Nieder
2011-11-16 9:31 ` Nguyen Thai Ngoc Duy
2011-11-19 19:25 ` Ramsay Jones
2011-11-16 21:50 ` [PATCH/RFC] introduce strbuf_addpath() Jonathan Nieder
2011-11-18 1:42 ` Nguyen Thai Ngoc Duy [this message]
2011-11-16 22:04 ` [PATCH 0/3] avoiding unintended consequences of git_path() usage Junio C Hamano
2011-11-16 8:51 ` Ramkumar Ramachandra
2011-11-16 13:33 ` Nguyen Thai Ngoc Duy
2011-11-16 13:44 ` Michael Haggerty
2011-11-18 3:33 ` Nguyen Thai Ngoc Duy
2011-11-05 16:29 ` [PATCH 4/5] sequencer: handle single commit pick separately Ramkumar Ramachandra
2011-11-06 0:35 ` Jonathan Nieder
2011-11-05 16:29 ` [PATCH 5/5] sequencer: revert d3f4628e Ramkumar Ramachandra
2011-11-06 0:42 ` Jonathan Nieder
2011-11-06 19:10 ` Junio C Hamano
2011-11-07 6:06 ` Ramkumar Ramachandra
2011-11-12 16:13 ` Ramkumar Ramachandra
2011-11-12 22:40 ` Jonathan Nieder
2011-11-05 23:43 ` [PATCH 0/5] Sequencer: working around historical mistakes Jonathan Nieder
2011-11-13 10:42 ` Ramkumar Ramachandra
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20111118014235.GA10917@tre \
--to=pclouds@gmail.com \
--cc=artagnon@gmail.com \
--cc=git@vger.kernel.org \
--cc=gitster@pobox.com \
--cc=jrnieder@gmail.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).