~psusi/ubuntu/precise/dmraid/fix-gpt

« back to all changes in this revision

Viewing changes to 1.0.0.rc14/lib/misc/file.c

  • Committer: Bazaar Package Importer
  • Author(s): Giuseppe Iuculano, 6af052c
  • Date: 2009-03-25 22:34:59 UTC
  • mfrom: (2.1.9 sid)
  • mto: (2.4.1 squeeze)
  • mto: This revision was merged to the branch mainline in revision 39.
  • Revision ID: james.westby@ubuntu.com-20090325223459-y54f0rmxem7htn6r
Tags: 1.0.0.rc15-6
[6af052c] Remove 15_isw_incorrect_status_fix.patch, it causes a
segfault. (Closes: #521104)

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
 * Copyright (C) 2004,2005  Heinz Mauelshagen, Red Hat GmbH.
3
 
 *                          All rights reserved.
4
 
 *
5
 
 * See file LICENSE at the top of this source tree for license information.
6
 
 */
7
 
 
8
 
#include "internal.h"
9
 
 
10
 
/* Create directory recusively. */
11
 
static int mk_dir_recursive(struct lib_context *lc, const char *dir)
12
 
{
13
 
        int ret = 1;
14
 
        char *orig, *s;
15
 
        const char delim = '/';
16
 
 
17
 
        if (!(orig = s = dbg_strdup((char*) dir)))
18
 
                return log_alloc_err(lc, __func__);
19
 
 
20
 
        /* Create parent directories */
21
 
        log_notice(lc, "creating directory %s", dir);
22
 
        do {
23
 
                s = remove_delimiter(s + 1, delim);
24
 
                if (mkdir(orig, 0777) && errno != EEXIST) {
25
 
                        log_err(lc, "mkdir %s", orig);
26
 
                        ret = 0;
27
 
                        break;
28
 
                }
29
 
 
30
 
                add_delimiter(&s, delim);
31
 
        } while (s);
32
 
 
33
 
        dbg_free(orig);
34
 
 
35
 
        return ret;
36
 
}
37
 
 
38
 
/* Create directory. */
39
 
int mk_dir(struct lib_context *lc, const char *dir)
40
 
{
41
 
        struct stat info;
42
 
 
43
 
        /* If it doesn't exist yet, make it. */
44
 
        if (stat(dir, &info))
45
 
                return mk_dir_recursive(lc, dir);
46
 
 
47
 
        if (S_ISDIR(info.st_mode))
48
 
                return 1;
49
 
 
50
 
        LOG_ERR(lc, 0, "directory %s not found", dir);
51
 
}
52
 
 
53
 
static int rw_file(struct lib_context *lc, const char *who, int flags,
54
 
                   char *path, void *buffer, size_t size, loff_t offset)
55
 
{
56
 
        int fd, ret = 0;
57
 
        loff_t o;
58
 
        struct {
59
 
                ssize_t (*func)();
60
 
                const char *what;
61
 
        } rw_spec[] = {
62
 
                { read,  "read" },
63
 
                { write, "writ" },
64
 
        }, *rw = rw_spec + ((flags & O_WRONLY) ? 1 : 0);
65
 
 
66
 
        if ((fd = open(path, flags, lc->mode)) == -1)
67
 
                LOG_ERR(lc, 0, "opening \"%s\"", path);
68
 
 
69
 
#ifdef __KLIBC__
70
 
#define DMRAID_LSEEK    lseek
71
 
#else
72
 
#define DMRAID_LSEEK    lseek64
73
 
#endif
74
 
        if (offset && (o = DMRAID_LSEEK(fd, offset, SEEK_SET)) == (loff_t) -1)
75
 
                log_err(lc, "%s: seeking device \"%s\" to %" PRIu64,
76
 
                        who, path, offset);
77
 
        else if (rw->func(fd, buffer, size) != size)
78
 
                log_err(lc, "%s: %sing %s[%s]", who, rw->what,
79
 
                        path, strerror(errno));
80
 
        else
81
 
                ret = 1;
82
 
 
83
 
        close(fd);
84
 
 
85
 
        return ret;
86
 
}
87
 
 
88
 
int read_file(struct lib_context *lc, const char *who, char *path,
89
 
              void *buffer, size_t size, loff_t offset)
90
 
{
91
 
        return rw_file(lc, who, O_RDONLY, path, buffer, size, offset);
92
 
}
93
 
 
94
 
int write_file(struct lib_context *lc, const char *who, char *path,
95
 
               void *buffer, size_t size, loff_t offset)
96
 
{
97
 
        /* O_CREAT|O_TRUNC are noops on a devnode. */
98
 
        return rw_file(lc, who, O_WRONLY|O_CREAT|O_TRUNC, path,
99
 
                       buffer, size, offset);
100
 
}