Linux Audio

Check our new training course

Yocto / OpenEmbedded training

Mar 24-27, 2025, special US time zones
Register
Loading...
v5.9
  1/*
  2 * Copyright (C) 2000 - 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com)
  3 * Licensed under the GPL
  4 */
  5
  6#include <stdio.h>
  7#include <stddef.h>
  8#include <unistd.h>
  9#include <dirent.h>
 10#include <errno.h>
 11#include <fcntl.h>
 12#include <string.h>
 13#include <sys/stat.h>
 14#include <sys/time.h>
 15#include <sys/types.h>
 16#include <sys/vfs.h>
 17#include <sys/syscall.h>
 18#include "hostfs.h"
 
 19#include <utime.h>
 20
 21static void stat64_to_hostfs(const struct stat64 *buf, struct hostfs_stat *p)
 22{
 23	p->ino = buf->st_ino;
 24	p->mode = buf->st_mode;
 25	p->nlink = buf->st_nlink;
 26	p->uid = buf->st_uid;
 27	p->gid = buf->st_gid;
 28	p->size = buf->st_size;
 29	p->atime.tv_sec = buf->st_atime;
 30	p->atime.tv_nsec = 0;
 31	p->ctime.tv_sec = buf->st_ctime;
 32	p->ctime.tv_nsec = 0;
 33	p->mtime.tv_sec = buf->st_mtime;
 34	p->mtime.tv_nsec = 0;
 35	p->blksize = buf->st_blksize;
 36	p->blocks = buf->st_blocks;
 37	p->maj = os_major(buf->st_rdev);
 38	p->min = os_minor(buf->st_rdev);
 39}
 40
 41int stat_file(const char *path, struct hostfs_stat *p, int fd)
 42{
 43	struct stat64 buf;
 44
 45	if (fd >= 0) {
 46		if (fstat64(fd, &buf) < 0)
 47			return -errno;
 48	} else if (lstat64(path, &buf) < 0) {
 49		return -errno;
 50	}
 51	stat64_to_hostfs(&buf, p);
 52	return 0;
 53}
 54
 55int access_file(char *path, int r, int w, int x)
 56{
 57	int mode = 0;
 58
 59	if (r)
 60		mode = R_OK;
 61	if (w)
 62		mode |= W_OK;
 63	if (x)
 64		mode |= X_OK;
 65	if (access(path, mode) != 0)
 66		return -errno;
 67	else return 0;
 68}
 69
 70int open_file(char *path, int r, int w, int append)
 71{
 72	int mode = 0, fd;
 73
 74	if (r && !w)
 75		mode = O_RDONLY;
 76	else if (!r && w)
 77		mode = O_WRONLY;
 78	else if (r && w)
 79		mode = O_RDWR;
 80	else panic("Impossible mode in open_file");
 81
 82	if (append)
 83		mode |= O_APPEND;
 84	fd = open64(path, mode);
 85	if (fd < 0)
 86		return -errno;
 87	else return fd;
 88}
 89
 90void *open_dir(char *path, int *err_out)
 91{
 92	DIR *dir;
 93
 94	dir = opendir(path);
 95	*err_out = errno;
 96
 97	return dir;
 98}
 99
100void seek_dir(void *stream, unsigned long long pos)
101{
102	DIR *dir = stream;
103
104	seekdir(dir, pos);
105}
106
107char *read_dir(void *stream, unsigned long long *pos_out,
108	       unsigned long long *ino_out, int *len_out,
109	       unsigned int *type_out)
110{
111	DIR *dir = stream;
112	struct dirent *ent;
113
 
114	ent = readdir(dir);
115	if (ent == NULL)
116		return NULL;
117	*len_out = strlen(ent->d_name);
118	*ino_out = ent->d_ino;
119	*type_out = ent->d_type;
120	*pos_out = ent->d_off;
121	return ent->d_name;
122}
123
124int read_file(int fd, unsigned long long *offset, char *buf, int len)
125{
126	int n;
127
128	n = pread64(fd, buf, len, *offset);
129	if (n < 0)
130		return -errno;
131	*offset += n;
132	return n;
133}
134
135int write_file(int fd, unsigned long long *offset, const char *buf, int len)
136{
137	int n;
138
139	n = pwrite64(fd, buf, len, *offset);
140	if (n < 0)
141		return -errno;
142	*offset += n;
143	return n;
144}
145
146int lseek_file(int fd, long long offset, int whence)
147{
148	int ret;
149
150	ret = lseek64(fd, offset, whence);
151	if (ret < 0)
152		return -errno;
153	return 0;
154}
155
156int fsync_file(int fd, int datasync)
157{
158	int ret;
159	if (datasync)
160		ret = fdatasync(fd);
161	else
162		ret = fsync(fd);
163
164	if (ret < 0)
165		return -errno;
166	return 0;
167}
168
169int replace_file(int oldfd, int fd)
170{
171	return dup2(oldfd, fd);
172}
173
174void close_file(void *stream)
175{
176	close(*((int *) stream));
177}
178
179void close_dir(void *stream)
180{
181	closedir(stream);
182}
183
184int file_create(char *name, int mode)
 
185{
186	int fd;
187
 
 
 
 
 
 
 
 
 
 
188	fd = open64(name, O_CREAT | O_RDWR, mode);
189	if (fd < 0)
190		return -errno;
191	return fd;
192}
193
194int set_attr(const char *file, struct hostfs_iattr *attrs, int fd)
195{
196	struct hostfs_stat st;
197	struct timeval times[2];
198	int err, ma;
199
200	if (attrs->ia_valid & HOSTFS_ATTR_MODE) {
201		if (fd >= 0) {
202			if (fchmod(fd, attrs->ia_mode) != 0)
203				return -errno;
204		} else if (chmod(file, attrs->ia_mode) != 0) {
205			return -errno;
206		}
207	}
208	if (attrs->ia_valid & HOSTFS_ATTR_UID) {
209		if (fd >= 0) {
210			if (fchown(fd, attrs->ia_uid, -1))
211				return -errno;
212		} else if (chown(file, attrs->ia_uid, -1)) {
213			return -errno;
214		}
215	}
216	if (attrs->ia_valid & HOSTFS_ATTR_GID) {
217		if (fd >= 0) {
218			if (fchown(fd, -1, attrs->ia_gid))
219				return -errno;
220		} else if (chown(file, -1, attrs->ia_gid)) {
221			return -errno;
222		}
223	}
224	if (attrs->ia_valid & HOSTFS_ATTR_SIZE) {
225		if (fd >= 0) {
226			if (ftruncate(fd, attrs->ia_size))
227				return -errno;
228		} else if (truncate(file, attrs->ia_size)) {
229			return -errno;
230		}
231	}
232
233	/*
234	 * Update accessed and/or modified time, in two parts: first set
235	 * times according to the changes to perform, and then call futimes()
236	 * or utimes() to apply them.
237	 */
238	ma = (HOSTFS_ATTR_ATIME_SET | HOSTFS_ATTR_MTIME_SET);
239	if (attrs->ia_valid & ma) {
240		err = stat_file(file, &st, fd);
241		if (err != 0)
242			return err;
243
244		times[0].tv_sec = st.atime.tv_sec;
245		times[0].tv_usec = st.atime.tv_nsec / 1000;
246		times[1].tv_sec = st.mtime.tv_sec;
247		times[1].tv_usec = st.mtime.tv_nsec / 1000;
248
249		if (attrs->ia_valid & HOSTFS_ATTR_ATIME_SET) {
250			times[0].tv_sec = attrs->ia_atime.tv_sec;
251			times[0].tv_usec = attrs->ia_atime.tv_nsec / 1000;
252		}
253		if (attrs->ia_valid & HOSTFS_ATTR_MTIME_SET) {
254			times[1].tv_sec = attrs->ia_mtime.tv_sec;
255			times[1].tv_usec = attrs->ia_mtime.tv_nsec / 1000;
256		}
257
258		if (fd >= 0) {
259			if (futimes(fd, times) != 0)
260				return -errno;
261		} else if (utimes(file, times) != 0) {
262			return -errno;
263		}
264	}
265
266	/* Note: ctime is not handled */
267	if (attrs->ia_valid & (HOSTFS_ATTR_ATIME | HOSTFS_ATTR_MTIME)) {
268		err = stat_file(file, &st, fd);
269		attrs->ia_atime = st.atime;
270		attrs->ia_mtime = st.mtime;
271		if (err != 0)
272			return err;
273	}
274	return 0;
275}
276
277int make_symlink(const char *from, const char *to)
278{
279	int err;
280
281	err = symlink(to, from);
282	if (err)
283		return -errno;
284	return 0;
285}
286
287int unlink_file(const char *file)
288{
289	int err;
290
291	err = unlink(file);
292	if (err)
293		return -errno;
294	return 0;
295}
296
297int do_mkdir(const char *file, int mode)
298{
299	int err;
300
301	err = mkdir(file, mode);
302	if (err)
303		return -errno;
304	return 0;
305}
306
307int hostfs_do_rmdir(const char *file)
308{
309	int err;
310
311	err = rmdir(file);
312	if (err)
313		return -errno;
314	return 0;
315}
316
317int do_mknod(const char *file, int mode, unsigned int major, unsigned int minor)
318{
319	int err;
320
321	err = mknod(file, mode, os_makedev(major, minor));
322	if (err)
323		return -errno;
324	return 0;
325}
326
327int link_file(const char *to, const char *from)
328{
329	int err;
330
331	err = link(to, from);
332	if (err)
333		return -errno;
334	return 0;
335}
336
337int hostfs_do_readlink(char *file, char *buf, int size)
338{
339	int n;
340
341	n = readlink(file, buf, size);
342	if (n < 0)
343		return -errno;
344	if (n < size)
345		buf[n] = '\0';
346	return n;
347}
348
349int rename_file(char *from, char *to)
350{
351	int err;
352
353	err = rename(from, to);
354	if (err < 0)
355		return -errno;
356	return 0;
357}
358
359int rename2_file(char *from, char *to, unsigned int flags)
360{
361	int err;
362
363#ifndef SYS_renameat2
364#  ifdef __x86_64__
365#    define SYS_renameat2 316
366#  endif
367#  ifdef __i386__
368#    define SYS_renameat2 353
369#  endif
370#endif
371
372#ifdef SYS_renameat2
373	err = syscall(SYS_renameat2, AT_FDCWD, from, AT_FDCWD, to, flags);
374	if (err < 0) {
375		if (errno != ENOSYS)
376			return -errno;
377		else
378			return -EINVAL;
379	}
380	return 0;
381#else
382	return -EINVAL;
383#endif
384}
385
386int do_statfs(char *root, long *bsize_out, long long *blocks_out,
387	      long long *bfree_out, long long *bavail_out,
388	      long long *files_out, long long *ffree_out,
389	      void *fsid_out, int fsid_size, long *namelen_out)
390{
391	struct statfs64 buf;
392	int err;
393
394	err = statfs64(root, &buf);
395	if (err < 0)
396		return -errno;
397
398	*bsize_out = buf.f_bsize;
399	*blocks_out = buf.f_blocks;
400	*bfree_out = buf.f_bfree;
401	*bavail_out = buf.f_bavail;
402	*files_out = buf.f_files;
403	*ffree_out = buf.f_ffree;
404	memcpy(fsid_out, &buf.f_fsid,
405	       sizeof(buf.f_fsid) > fsid_size ? fsid_size :
406	       sizeof(buf.f_fsid));
407	*namelen_out = buf.f_namelen;
408
409	return 0;
410}
v3.5.6
  1/*
  2 * Copyright (C) 2000 - 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com)
  3 * Licensed under the GPL
  4 */
  5
  6#include <stdio.h>
  7#include <stddef.h>
  8#include <unistd.h>
  9#include <dirent.h>
 10#include <errno.h>
 11#include <fcntl.h>
 12#include <string.h>
 13#include <sys/stat.h>
 14#include <sys/time.h>
 15#include <sys/types.h>
 16#include <sys/vfs.h>
 
 17#include "hostfs.h"
 18#include "os.h"
 19#include <utime.h>
 20
 21static void stat64_to_hostfs(const struct stat64 *buf, struct hostfs_stat *p)
 22{
 23	p->ino = buf->st_ino;
 24	p->mode = buf->st_mode;
 25	p->nlink = buf->st_nlink;
 26	p->uid = buf->st_uid;
 27	p->gid = buf->st_gid;
 28	p->size = buf->st_size;
 29	p->atime.tv_sec = buf->st_atime;
 30	p->atime.tv_nsec = 0;
 31	p->ctime.tv_sec = buf->st_ctime;
 32	p->ctime.tv_nsec = 0;
 33	p->mtime.tv_sec = buf->st_mtime;
 34	p->mtime.tv_nsec = 0;
 35	p->blksize = buf->st_blksize;
 36	p->blocks = buf->st_blocks;
 37	p->maj = os_major(buf->st_rdev);
 38	p->min = os_minor(buf->st_rdev);
 39}
 40
 41int stat_file(const char *path, struct hostfs_stat *p, int fd)
 42{
 43	struct stat64 buf;
 44
 45	if (fd >= 0) {
 46		if (fstat64(fd, &buf) < 0)
 47			return -errno;
 48	} else if (lstat64(path, &buf) < 0) {
 49		return -errno;
 50	}
 51	stat64_to_hostfs(&buf, p);
 52	return 0;
 53}
 54
 55int access_file(char *path, int r, int w, int x)
 56{
 57	int mode = 0;
 58
 59	if (r)
 60		mode = R_OK;
 61	if (w)
 62		mode |= W_OK;
 63	if (x)
 64		mode |= X_OK;
 65	if (access(path, mode) != 0)
 66		return -errno;
 67	else return 0;
 68}
 69
 70int open_file(char *path, int r, int w, int append)
 71{
 72	int mode = 0, fd;
 73
 74	if (r && !w)
 75		mode = O_RDONLY;
 76	else if (!r && w)
 77		mode = O_WRONLY;
 78	else if (r && w)
 79		mode = O_RDWR;
 80	else panic("Impossible mode in open_file");
 81
 82	if (append)
 83		mode |= O_APPEND;
 84	fd = open64(path, mode);
 85	if (fd < 0)
 86		return -errno;
 87	else return fd;
 88}
 89
 90void *open_dir(char *path, int *err_out)
 91{
 92	DIR *dir;
 93
 94	dir = opendir(path);
 95	*err_out = errno;
 96
 97	return dir;
 98}
 99
100char *read_dir(void *stream, unsigned long long *pos,
 
 
 
 
 
 
 
101	       unsigned long long *ino_out, int *len_out,
102	       unsigned int *type_out)
103{
104	DIR *dir = stream;
105	struct dirent *ent;
106
107	seekdir(dir, *pos);
108	ent = readdir(dir);
109	if (ent == NULL)
110		return NULL;
111	*len_out = strlen(ent->d_name);
112	*ino_out = ent->d_ino;
113	*type_out = ent->d_type;
114	*pos = telldir(dir);
115	return ent->d_name;
116}
117
118int read_file(int fd, unsigned long long *offset, char *buf, int len)
119{
120	int n;
121
122	n = pread64(fd, buf, len, *offset);
123	if (n < 0)
124		return -errno;
125	*offset += n;
126	return n;
127}
128
129int write_file(int fd, unsigned long long *offset, const char *buf, int len)
130{
131	int n;
132
133	n = pwrite64(fd, buf, len, *offset);
134	if (n < 0)
135		return -errno;
136	*offset += n;
137	return n;
138}
139
140int lseek_file(int fd, long long offset, int whence)
141{
142	int ret;
143
144	ret = lseek64(fd, offset, whence);
145	if (ret < 0)
146		return -errno;
147	return 0;
148}
149
150int fsync_file(int fd, int datasync)
151{
152	int ret;
153	if (datasync)
154		ret = fdatasync(fd);
155	else
156		ret = fsync(fd);
157
158	if (ret < 0)
159		return -errno;
160	return 0;
161}
162
163int replace_file(int oldfd, int fd)
164{
165	return dup2(oldfd, fd);
166}
167
168void close_file(void *stream)
169{
170	close(*((int *) stream));
171}
172
173void close_dir(void *stream)
174{
175	closedir(stream);
176}
177
178int file_create(char *name, int ur, int uw, int ux, int gr,
179		int gw, int gx, int or, int ow, int ox)
180{
181	int mode, fd;
182
183	mode = 0;
184	mode |= ur ? S_IRUSR : 0;
185	mode |= uw ? S_IWUSR : 0;
186	mode |= ux ? S_IXUSR : 0;
187	mode |= gr ? S_IRGRP : 0;
188	mode |= gw ? S_IWGRP : 0;
189	mode |= gx ? S_IXGRP : 0;
190	mode |= or ? S_IROTH : 0;
191	mode |= ow ? S_IWOTH : 0;
192	mode |= ox ? S_IXOTH : 0;
193	fd = open64(name, O_CREAT | O_RDWR, mode);
194	if (fd < 0)
195		return -errno;
196	return fd;
197}
198
199int set_attr(const char *file, struct hostfs_iattr *attrs, int fd)
200{
201	struct hostfs_stat st;
202	struct timeval times[2];
203	int err, ma;
204
205	if (attrs->ia_valid & HOSTFS_ATTR_MODE) {
206		if (fd >= 0) {
207			if (fchmod(fd, attrs->ia_mode) != 0)
208				return -errno;
209		} else if (chmod(file, attrs->ia_mode) != 0) {
210			return -errno;
211		}
212	}
213	if (attrs->ia_valid & HOSTFS_ATTR_UID) {
214		if (fd >= 0) {
215			if (fchown(fd, attrs->ia_uid, -1))
216				return -errno;
217		} else if (chown(file, attrs->ia_uid, -1)) {
218			return -errno;
219		}
220	}
221	if (attrs->ia_valid & HOSTFS_ATTR_GID) {
222		if (fd >= 0) {
223			if (fchown(fd, -1, attrs->ia_gid))
224				return -errno;
225		} else if (chown(file, -1, attrs->ia_gid)) {
226			return -errno;
227		}
228	}
229	if (attrs->ia_valid & HOSTFS_ATTR_SIZE) {
230		if (fd >= 0) {
231			if (ftruncate(fd, attrs->ia_size))
232				return -errno;
233		} else if (truncate(file, attrs->ia_size)) {
234			return -errno;
235		}
236	}
237
238	/*
239	 * Update accessed and/or modified time, in two parts: first set
240	 * times according to the changes to perform, and then call futimes()
241	 * or utimes() to apply them.
242	 */
243	ma = (HOSTFS_ATTR_ATIME_SET | HOSTFS_ATTR_MTIME_SET);
244	if (attrs->ia_valid & ma) {
245		err = stat_file(file, &st, fd);
246		if (err != 0)
247			return err;
248
249		times[0].tv_sec = st.atime.tv_sec;
250		times[0].tv_usec = st.atime.tv_nsec / 1000;
251		times[1].tv_sec = st.mtime.tv_sec;
252		times[1].tv_usec = st.mtime.tv_nsec / 1000;
253
254		if (attrs->ia_valid & HOSTFS_ATTR_ATIME_SET) {
255			times[0].tv_sec = attrs->ia_atime.tv_sec;
256			times[0].tv_usec = attrs->ia_atime.tv_nsec / 1000;
257		}
258		if (attrs->ia_valid & HOSTFS_ATTR_MTIME_SET) {
259			times[1].tv_sec = attrs->ia_mtime.tv_sec;
260			times[1].tv_usec = attrs->ia_mtime.tv_nsec / 1000;
261		}
262
263		if (fd >= 0) {
264			if (futimes(fd, times) != 0)
265				return -errno;
266		} else if (utimes(file, times) != 0) {
267			return -errno;
268		}
269	}
270
271	/* Note: ctime is not handled */
272	if (attrs->ia_valid & (HOSTFS_ATTR_ATIME | HOSTFS_ATTR_MTIME)) {
273		err = stat_file(file, &st, fd);
274		attrs->ia_atime = st.atime;
275		attrs->ia_mtime = st.mtime;
276		if (err != 0)
277			return err;
278	}
279	return 0;
280}
281
282int make_symlink(const char *from, const char *to)
283{
284	int err;
285
286	err = symlink(to, from);
287	if (err)
288		return -errno;
289	return 0;
290}
291
292int unlink_file(const char *file)
293{
294	int err;
295
296	err = unlink(file);
297	if (err)
298		return -errno;
299	return 0;
300}
301
302int do_mkdir(const char *file, int mode)
303{
304	int err;
305
306	err = mkdir(file, mode);
307	if (err)
308		return -errno;
309	return 0;
310}
311
312int do_rmdir(const char *file)
313{
314	int err;
315
316	err = rmdir(file);
317	if (err)
318		return -errno;
319	return 0;
320}
321
322int do_mknod(const char *file, int mode, unsigned int major, unsigned int minor)
323{
324	int err;
325
326	err = mknod(file, mode, os_makedev(major, minor));
327	if (err)
328		return -errno;
329	return 0;
330}
331
332int link_file(const char *to, const char *from)
333{
334	int err;
335
336	err = link(to, from);
337	if (err)
338		return -errno;
339	return 0;
340}
341
342int hostfs_do_readlink(char *file, char *buf, int size)
343{
344	int n;
345
346	n = readlink(file, buf, size);
347	if (n < 0)
348		return -errno;
349	if (n < size)
350		buf[n] = '\0';
351	return n;
352}
353
354int rename_file(char *from, char *to)
355{
356	int err;
357
358	err = rename(from, to);
359	if (err < 0)
360		return -errno;
361	return 0;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
362}
363
364int do_statfs(char *root, long *bsize_out, long long *blocks_out,
365	      long long *bfree_out, long long *bavail_out,
366	      long long *files_out, long long *ffree_out,
367	      void *fsid_out, int fsid_size, long *namelen_out)
368{
369	struct statfs64 buf;
370	int err;
371
372	err = statfs64(root, &buf);
373	if (err < 0)
374		return -errno;
375
376	*bsize_out = buf.f_bsize;
377	*blocks_out = buf.f_blocks;
378	*bfree_out = buf.f_bfree;
379	*bavail_out = buf.f_bavail;
380	*files_out = buf.f_files;
381	*ffree_out = buf.f_ffree;
382	memcpy(fsid_out, &buf.f_fsid,
383	       sizeof(buf.f_fsid) > fsid_size ? fsid_size :
384	       sizeof(buf.f_fsid));
385	*namelen_out = buf.f_namelen;
386
387	return 0;
388}