xref: /netbsd-src/sys/fs/v7fs/v7fs_dirent.c (revision 514b0270dd0ac6e8ffce7a47d2bc570d815599f3)
1 /*	$NetBSD: v7fs_dirent.c,v 1.3 2022/02/11 10:55:15 hannken Exp $	*/
2 
3 /*-
4  * Copyright (c) 2011 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by UCHIYAMA Yasushi.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29  * POSSIBILITY OF SUCH DAMAGE.
30  */
31 
32 #if HAVE_NBTOOL_CONFIG_H
33 #include "nbtool_config.h"
34 #endif
35 
36 #include <sys/cdefs.h>
37 __KERNEL_RCSID(0, "$NetBSD: v7fs_dirent.c,v 1.3 2022/02/11 10:55:15 hannken Exp $");
38 #if defined _KERNEL_OPT
39 #include "opt_v7fs.h"
40 #endif
41 
42 #ifdef _KERNEL
43 #include <sys/systm.h>
44 #include <sys/param.h>
45 #else
46 #include <stdio.h>
47 #include <string.h>
48 #endif
49 
50 #include "v7fs.h"
51 #include "v7fs_impl.h"
52 #include "v7fs_endian.h"
53 #include "v7fs_inode.h"
54 #include "v7fs_dirent.h"
55 
56 #ifdef V7FS_DIRENT_DEBUG
57 #define	DPRINTF(fmt, args...)	printf("%s: " fmt, __func__, ##args)
58 #else
59 #define	DPRINTF(fmt, args...)	((void)0)
60 #endif
61 
62 bool
v7fs_dirent_endian_convert(struct v7fs_self * fs,struct v7fs_dirent * dir,int n)63 v7fs_dirent_endian_convert(struct v7fs_self *fs, struct v7fs_dirent *dir, int n)
64 {
65 	struct v7fs_superblock *sb = &fs->superblock;
66 	int i;
67 	v7fs_ino_t ino;
68 	bool ok = true;
69 
70 	for (i = 0; i < n; i++, dir++) {
71 		ino = V7FS_VAL16(fs, dir->inode_number);
72 		if (v7fs_inode_number_sanity(sb, ino)) {
73 			DPRINTF("Invalid inode# %d %s\n", ino, dir->name);
74 			ok = false;
75 		}
76 		dir->inode_number = ino;
77 	}
78 
79 	return ok;
80 }
81 
82 void
v7fs_dirent_filename(char * dst,const char * src,size_t srclen)83 v7fs_dirent_filename(char *dst/* size must be V7FS_NAME_MAX + 1 */,
84     const char *src, size_t srclen)
85 {
86 
87 	if (srclen > V7FS_NAME_MAX)
88 		srclen = V7FS_NAME_MAX;
89 
90 	memset(dst, 0, V7FS_NAME_MAX + 1);
91 	strncpy(dst, src, srclen);
92 }
93