Do not export para_lseek() and para_write_file().
[osl.git] / fd.c
1 /*
2  * Copyright (C) 2006-2008 Andre Noll <maan@systemlinux.org>
3  *
4  * Licensed under the GPL v2. For licencing details see COPYING.
5  */
6
7 /** \file fd.c Helper functions for file descriptor handling. */
8
9 #include <sys/types.h>
10 #include <dirent.h>
11 #include <sys/mman.h>
12 #include <fcntl.h>
13 #include <sys/select.h>
14
15 #include "log.h"
16 #include "error.h"
17
18 /**
19  * Write a buffer to a file descriptor, re-write on short writes.
20  *
21  * \param fd The file descriptor.
22  * \param buf The buffer to be sent.
23  * \param len The length of \a buf.
24  *
25  * \return Standard. In any case, the number of bytes that have been written is
26  * stored in \a len.
27  */
28 int write_all(int fd, const char *buf, size_t *len)
29 {
30         size_t total = *len;
31
32         assert(total);
33         *len = 0;
34         while (*len < total) {
35                 int ret = write(fd, buf + *len, total - *len);
36                 if (ret == -1)
37                         return -ERRNO_TO_ERROR(errno);
38                 *len += ret;
39         }
40         return 1;
41 }
42
43 /**
44  * Wrapper for the open(2) system call.
45  *
46  * \param path The filename.
47  * \param flags The usual open(2) flags.
48  * \param mode Specifies the permissions to use.
49  *
50  * The mode parameter must be specified when O_CREAT is in the flags, and is
51  * ignored otherwise.
52  *
53  * \return The file descriptor on success, negative on errors.
54  *
55  * \sa open(2).
56  */
57 int para_open(const char *path, int flags, mode_t mode)
58 {
59         int ret = open(path, flags, mode);
60
61         if (ret >= 0)
62                 return ret;
63         return -ERRNO_TO_ERROR(errno);
64 }
65
66 /**
67  * Wrapper for chdir(2).
68  *
69  * \param path The specified directory.
70  *
71  * \return Standard.
72  */
73 static int para_chdir(const char *path)
74 {
75         int ret = chdir(path);
76
77         if (ret >= 0)
78                 return 1;
79         return -ERRNO_TO_ERROR(errno);
80 }
81
82 /**
83  * Save the cwd and open a given directory.
84  *
85  * \param dirname Path to the directory to open.
86  * \param dir Result pointer.
87  * \param cwd File descriptor of the current working directory.
88  *
89  * \return Standard.
90  *
91  * Opening the current directory (".") and calling fchdir() to return is
92  * usually faster and more reliable than saving cwd in some buffer and calling
93  * chdir() afterwards.
94  *
95  * If \a cwd is not \p NULL "." is opened and the resulting file descriptor is
96  * stored in \a cwd. If the function returns success, and \a cwd is not \p
97  * NULL, the caller must close this file descriptor (probably after calling
98  * fchdir(*cwd)).
99  *
100  * On errors, the function undos everything, so the caller needs neither close
101  * any files, nor change back to the original working directory.
102  *
103  * \sa getcwd(3).
104  *
105  */
106 int para_opendir(const char *dirname, DIR **dir, int *cwd)
107 {
108         int ret;
109
110         if (cwd) {
111                 ret = para_open(".", O_RDONLY, 0);
112                 if (ret < 0)
113                         return ret;
114                 *cwd = ret;
115         }
116         ret = para_chdir(dirname);
117         if (ret < 0)
118                 goto close_cwd;
119         *dir = opendir(".");
120         if (*dir)
121                 return 1;
122         ret = -ERRNO_TO_ERROR(errno);
123 /* Ignore return value of fchdir() and close(). We're busted anyway. */
124         if (cwd)
125                 fchdir(*cwd);
126 close_cwd:
127         if (cwd)
128                 close(*cwd);
129         return ret;
130 }
131
132 /**
133  * A wrapper for fchdir().
134  *
135  * \param fd An open file descriptor.
136  *
137  * \return Standard.
138  */
139 int para_fchdir(int fd)
140 {
141         if (fchdir(fd) < 0)
142                 return -ERRNO_TO_ERROR(errno);
143         return 1;
144 }
145
146 /**
147  * A wrapper for mkdir(2).
148  *
149  * \param path Name of the directory to create.
150  * \param mode The permissions to use.
151  *
152  * \return Standard.
153  */
154 int para_mkdir(const char *path, mode_t mode)
155 {
156         if (!mkdir(path, mode))
157                 return 1;
158         return -ERRNO_TO_ERROR(errno);
159 }
160
161 /**
162  * Open a file and map it into memory.
163  *
164  * \param path Name of the regular file to map.
165  * \param open_mode Either \p O_RDONLY or \p O_RDWR.
166  * \param map On success, the mapping is returned here.
167  * \param size size of the mapping.
168  * \param fd_ptr The file descriptor of the mapping.
169  *
170  * If \a fd_ptr is \p NULL, the file descriptor resulting from the underlying
171  * open call is closed after mmap().  Otherwise the file is kept open and the
172  * file descriptor is returned in \a fd_ptr.
173  *
174  * \return Standard.
175  *
176  * \sa para_open(), mmap(2).
177  */
178 int mmap_full_file(const char *path, int open_mode, void **map,
179                 size_t *size, int *fd_ptr)
180 {
181         int fd, ret, mmap_prot, mmap_flags;
182         struct stat file_status;
183
184         if (open_mode == O_RDONLY) {
185                 mmap_prot = PROT_READ;
186                 mmap_flags = MAP_PRIVATE;
187         } else {
188                 mmap_prot = PROT_READ | PROT_WRITE;
189                 mmap_flags = MAP_SHARED;
190         }
191         ret = para_open(path, open_mode, 0);
192         if (ret < 0)
193                 return ret;
194         fd = ret;
195         if (fstat(fd, &file_status) < 0) {
196                 ret = -ERRNO_TO_ERROR(errno);
197                 goto out;
198         }
199         *size = file_status.st_size;
200         ret = -E_EMPTY;
201         DEBUG_LOG("%s: size %zu\n", path, *size);
202         if (!*size)
203                 goto out;
204         *map = mmap(NULL, *size, mmap_prot, mmap_flags, fd, 0);
205         if (*map == MAP_FAILED) {
206                 *map = NULL;
207                 ret = -E_MMAP;
208                 goto out;
209         }
210         ret = 1;
211 out:
212         if (ret < 0 || !fd_ptr)
213                 close(fd);
214         else
215                 *fd_ptr = fd;
216         return ret;
217 }
218
219 /**
220  * A wrapper for munmap(2).
221  *
222  * \param start The start address of the memory mapping.
223  * \param length The size of the mapping.
224  *
225  * \return Positive on success, \p -E_MUNMAP on errors.
226  *
227  * \sa munmap(2), mmap_full_file().
228  */
229 int para_munmap(void *start, size_t length)
230 {
231         int err;
232         if (munmap(start, length) >= 0)
233                 return 1;
234         err = errno;
235         ERROR_LOG("munmap (%p/%zu) failed: %s\n", start, length,
236                 strerror(err));
237         return -ERRNO_TO_ERROR(err);
238 }