Whamcloud - gitweb
po: update sr.po (from translationproject.org)
[tools/e2fsprogs.git] / e2fsck / logfile.c
index 76ae52d..63e9a12 100644 (file)
 #ifdef HAVE_ERRNO_H
 #include <errno.h>
 #endif
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <fcntl.h>
 
 #include "e2fsck.h"
 #include <pwd.h>
 
+extern e2fsck_t e2fsck_global_ctx;   /* Try your very best not to use this! */
+
 struct string {
        char    *s;
        int     len;
@@ -33,19 +38,25 @@ static void alloc_string(struct string *s, int len)
 
 static void append_string(struct string *s, const char *a, int len)
 {
+       int needlen;
+
        if (!len)
                len = strlen(a);
 
-       if (s->end + len >= s->len) {
-               char *n = realloc(s, s->len * 2);
+       needlen = s->end + len + 1;
+       if (needlen > s->len) {
+               char *n;
+
+               if (s->len * 2 > needlen)
+                       needlen = s->len * 2;
+               n = realloc(s->s, needlen);
 
                if (n) {
                        s->s = n;
-                       s->len = s->len * 2;
+                       s->len = needlen;
                } else {
-                       len = s->len - s->end - 1;
-                       if (len <= 0)
-                               return;
+                       /* Don't append if we ran out of memory */
+                       return;
                }
        }
        memcpy(s->s + s->end, a, len);
@@ -126,7 +137,11 @@ static void expand_percent_expression(e2fsck_t ctx, char ch,
                strcpy(buf, "tytso");
                break;
 #else
+#ifdef HAVE_GETPWUID_R
                getpwuid_r(getuid(), &pw_struct, buf, sizeof(buf), &pw);
+#else
+               pw = getpwuid(getuid());
+#endif
                if (pw)
                        append_string(s, pw->pw_name, 0);
                return;
@@ -220,6 +235,8 @@ static FILE *save_output(const char *s0, const char *s1, const char *s2)
        }
 
        if (pid == 0) {
+               if (e2fsck_global_ctx && e2fsck_global_ctx->progress_fd)
+                       close(e2fsck_global_ctx->progress_fd);
                if (daemon(0, 0) < 0) {
                        perror("daemon");
                        exit(1);
@@ -268,8 +285,9 @@ static FILE *save_output(const char *s0, const char *s1, const char *s2)
 }
 
 #ifndef TEST_PROGRAM
-void set_up_logging(e2fsck_t ctx)
+static FILE *set_up_log_file(e2fsck_t ctx, const char *key, const char *fn)
 {
+       FILE *f = NULL;
        struct string s, s1, s2;
        char *s0 = 0, *log_dir = 0, *log_fn = 0;
        int log_dir_wait = 0;
@@ -278,10 +296,10 @@ void set_up_logging(e2fsck_t ctx)
 
        profile_get_boolean(ctx->profile, "options", "log_dir_wait", 0, 0,
                            &log_dir_wait);
-       if (ctx->log_fn)
-               log_fn = string_copy(ctx, ctx->log_fn, 0);
+       if (fn)
+               log_fn = string_copy(ctx, fn, 0);
        else
-               profile_get_string(ctx->profile, "options", "log_filename",
+               profile_get_string(ctx->profile, "options", key,
                                   0, 0, &log_fn);
        profile_get_string(ctx->profile, "options", "log_dir", 0, 0, &log_dir);
 
@@ -311,13 +329,13 @@ void set_up_logging(e2fsck_t ctx)
        }
 
        if (s0)
-               ctx->logf = fopen(s0, "w");
-       if (!ctx->logf && s1.s)
-               ctx->logf = fopen(s1.s, "w");
-       if (!ctx->logf && s2.s)
-               ctx->logf = fopen(s2.s, "w");
-       if (!ctx->logf && log_dir_wait)
-               ctx->logf = save_output(s0, s1.s, s2.s);
+               f = fopen(s0, "w");
+       if (!f && s1.s)
+               f = fopen(s1.s, "w");
+       if (!f && s2.s)
+               f = fopen(s2.s, "w");
+       if (!f && log_dir_wait)
+               f = save_output(s0, s1.s, s2.s);
 
 out:
        free(s.s);
@@ -325,10 +343,17 @@ out:
        free(s2.s);
        free(log_fn);
        free(log_dir);
-       return;
+       return f;
+}
+
+void set_up_logging(e2fsck_t ctx)
+{
+       ctx->logf = set_up_log_file(ctx, "log_filename", ctx->log_fn);
+       ctx->problem_logf = set_up_log_file(ctx, "problem_log_filename",
+                                           ctx->problem_log_fn);
 }
 #else
-void *e2fsck_allocate_memory(e2fsck_t ctx, unsigned int size,
+void *e2fsck_allocate_memory(e2fsck_t ctx, unsigned long size,
                             const char *description)
 {
        void *ret;