2
* Copyright (C) 2004,2005 Heinz Mauelshagen, Red Hat GmbH.
5
* See file LICENSE at the top of this source tree for license information.
10
/* Create directory recusively. */
11
static int mk_dir_recursive(struct lib_context *lc, const char *dir)
15
const char delim = '/';
17
if (!(orig = s = dbg_strdup((char*) dir)))
18
return log_alloc_err(lc, __func__);
20
/* Create parent directories */
21
log_notice(lc, "creating directory %s", dir);
23
s = remove_delimiter(s + 1, delim);
24
if (mkdir(orig, 0777) && errno != EEXIST) {
25
log_err(lc, "mkdir %s", orig);
30
add_delimiter(&s, delim);
38
/* Create directory. */
39
int mk_dir(struct lib_context *lc, const char *dir)
43
/* If it doesn't exist yet, make it. */
45
return mk_dir_recursive(lc, dir);
47
if (S_ISDIR(info.st_mode))
50
LOG_ERR(lc, 0, "directory %s not found", dir);
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)
64
}, *rw = rw_spec + ((flags & O_WRONLY) ? 1 : 0);
66
if ((fd = open(path, flags, lc->mode)) == -1)
67
LOG_ERR(lc, 0, "opening \"%s\"", path);
70
#define DMRAID_LSEEK lseek
72
#define DMRAID_LSEEK lseek64
74
if (offset && (o = DMRAID_LSEEK(fd, offset, SEEK_SET)) == (loff_t) -1)
75
log_err(lc, "%s: seeking device \"%s\" to %" PRIu64,
77
else if (rw->func(fd, buffer, size) != size)
78
log_err(lc, "%s: %sing %s[%s]", who, rw->what,
79
path, strerror(errno));
88
int read_file(struct lib_context *lc, const char *who, char *path,
89
void *buffer, size_t size, loff_t offset)
91
return rw_file(lc, who, O_RDONLY, path, buffer, size, offset);
94
int write_file(struct lib_context *lc, const char *who, char *path,
95
void *buffer, size_t size, loff_t offset)
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);