[2786] in BarnOwl Developers
Re: [PATCH 1/2] Fixed :unsub to make it work if ~/.zephyr.subs is
daemon@ATHENA.MIT.EDU (David A Benjamin)
Mon Nov 15 16:48:46 2010
Date: Mon, 15 Nov 2010 16:48:43 -0500 (EST)
From: David A Benjamin <davidben@MIT.EDU>
To: Karl Ramm <kcr@1ts.org>
cc: barnowl-dev@mit.edu
In-Reply-To: <uus1v6mz2r5.fsf@1ts.org>
The man pages says it's equivalent to realpath(path, NULL) which...
doesn't appear to be a GNUism?
We can also add another AC_REPLACE_FUNCS, but they're annoying.
David
On Mon, 15 Nov 2010, Karl Ramm wrote:
> I said looks good to me, before, but I notice that canonicalize_file_name
> is a GNUism; do we want to give up and only work on glibc systems?
>
> kcr
>
> (Oh hey, that can has worms crawling out)
>
> Jason Gross <jgross@MIT.EDU> writes:
>
>> From: Jason <jgross@mit.edu>
>>
>> link() fails on AFS if the target is a symlink. Additionally, on
>> systems where link() succeeds, barnowl overwrites the symlink, instead
>> of the actual file. To fix this, I've changed owl_util_file_deleteline
>> to call canonicalize_file_name on the passed file name first. The new
>> (backup) file is written in the resolved directory, not the directory
>> where the symlink itself is found.
>>
>> Additionally, I corrected free(filename) to owl_free(filename) in the
>> two places that it occurred.
>> ---
>> util.c | 34 ++++++++++++++++++++++++----------
>> 1 files changed, 24 insertions(+), 10 deletions(-)
>>
>> diff --git a/util.c b/util.c
>> index 6546c73..cdbfd01 100644
>> --- a/util.c
>> +++ b/util.c
>> @@ -384,38 +384,50 @@ char *owl_util_stripnewlines(const char *in)
>> */
>> int owl_util_file_deleteline(const char *filename, const char *line, int backup)
>> {
>> - char *backupfile, *newfile, *buf = NULL;
>> + char *actual_filename, *backupfile, *newfile, *buf = NULL;
>> FILE *old, *new;
>> struct stat st;
>> int numremoved = 0;
>>
>> + /* canonicalize filename, so that we deal with with symlinks correctly */
>> + actual_filename = canonicalize_file_name(filename);
>> + if (actual_filename == NULL) {
>> + owl_function_error("Cannot canonicalize file name %s: %s",
>> + actual_filename, strerror(errno));
>> + return 0;
>> + }
>> +
>> if ((old = fopen(filename, "r")) == NULL) {
>> owl_function_error("Cannot open %s (for reading): %s",
>> filename, strerror(errno));
>> + free(actual_filename);
>> return 0;
>> }
>>
>> if (fstat(fileno(old), &st) != 0) {
>> owl_function_error("Cannot stat %s: %s", filename, strerror(errno));
>> + free(actual_filename);
>> return 0;
>> }
>>
>> - newfile = owl_sprintf("%s.new", filename);
>> + newfile = owl_sprintf("%s.new", actual_filename);
>> if ((new = fopen(newfile, "w")) == NULL) {
>> owl_function_error("Cannot open %s (for writing): %s",
>> - filename, strerror(errno));
>> - free(newfile);
>> + actual_filename, strerror(errno));
>> + owl_free(newfile);
>> fclose(old);
>> + free(actual_filename);
>> return 0;
>> }
>>
>> if (fchmod(fileno(new), st.st_mode & 0777) != 0) {
>> owl_function_error("Cannot set permissions on %s: %s",
>> - filename, strerror(errno));
>> + actual_filename, strerror(errno));
>> unlink(newfile);
>> fclose(new);
>> - free(newfile);
>> + owl_free(newfile);
>> fclose(old);
>> + free(actual_filename);
>> return 0;
>> }
>>
>> @@ -430,9 +442,9 @@ int owl_util_file_deleteline(const char *filename, const char *line, int backup)
>> fclose(old);
>>
>> if (backup) {
>> - backupfile = owl_sprintf("%s.backup", filename);
>> + backupfile = owl_sprintf("%s.backup", actual_filename);
>> unlink(backupfile);
>> - if (link(filename, backupfile) != 0) {
>> + if (link(actual_filename, backupfile) != 0) {
>> owl_function_error("Cannot link %s: %s", backupfile, strerror(errno));
>> owl_free(backupfile);
>> unlink(newfile);
>> @@ -442,15 +454,17 @@ int owl_util_file_deleteline(const char *filename, const char *line, int backup)
>> owl_free(backupfile);
>> }
>>
>> - if (rename(newfile, filename) != 0) {
>> + if (rename(newfile, actual_filename) != 0) {
>> owl_function_error("Cannot move %s to %s: %s",
>> - newfile, filename, strerror(errno));
>> + newfile, actual_filename, strerror(errno));
>> numremoved = 0;
>> }
>>
>> unlink(newfile);
>> owl_free(newfile);
>>
>> + free(actual_filename);
>> +
>> return numremoved;
>> }
>