#include <sys/cdefs.h>
#ifndef lint
__RCSID("$NetBSD: pathname.c,v 1.2 2022/02/11 10:55:15 hannken Exp $");
#endif
#include <sys/types.h>
#include <sys/syslimits.h>
#include <stdio.h>
#include <string.h>
#include <time.h>
#include "v7fs.h"
#include "v7fs_impl.h"
#include "v7fs_inode.h"
#include "v7fs_datablock.h"
#include "v7fs_superblock.h"
#include "v7fs_dirent.h"
#include "v7fs_file.h"
#include "fsck_v7fs.h"
static int
connect_lost_and_found(struct v7fs_self *fs, v7fs_ino_t ino)
{
char name[V7FS_NAME_MAX];
v7fs_time_t t;
if (!lost_and_found.mode || !reply("CONNECT?"))
return FSCK_EXIT_CHECK_FAILED;
snprintf(name, sizeof(name), "%d", ino);
v7fs_directory_add_entry(fs, &lost_and_found, ino, name, strlen(name));
t = (v7fs_time_t)time(NULL);
lost_and_found.mtime = lost_and_found.atime = t;
v7fs_inode_writeback(fs, &lost_and_found);
v7fs_superblock_writeback(fs);
return 0;
}
struct lookup_child_arg {
int dir_cnt;
bool print;
struct v7fs_inode *parent;
};
static int
lookup_child_subr(struct v7fs_self *fs, void *ctx, v7fs_daddr_t blk, size_t sz)
{
struct lookup_child_arg *arg = (struct lookup_child_arg *)ctx;
void *buf;
int error = 0;
if (!(buf = scratch_read(fs, blk)))
return 0;
struct v7fs_dirent *dir = (struct v7fs_dirent *)buf;
size_t i, n = sz / sizeof(*dir);
if (!v7fs_dirent_endian_convert(fs, dir, n)) {
pwarn("*** bogus entry found *** dir#%d entry=%zu\n",
arg->parent->inode_number, n);
arg->print = true;
}
for (i = 0; i < n; i++, dir++) {
struct v7fs_inode inode;
if (arg->print)
pwarn("%s %d\n", dir->name, dir->inode_number);
if ((error = v7fs_inode_load(fs, &inode, dir->inode_number)))
{
pwarn("entry #%d not found.", dir->inode_number);
if (reply("REMOVE?"))
v7fs_directory_remove_entry(fs, arg->parent,
dir->name, strlen(dir->name));
} else {
if (v7fs_inode_isdir(&inode))
arg->dir_cnt++;
}
}
scratch_free(fs, buf);
return error;
}
static int
lookup_child_from_dir(struct v7fs_self *fs, struct v7fs_inode *p, bool print)
{
struct lookup_child_arg arg = { .dir_cnt = 0, .print = print,
.parent = p };
v7fs_datablock_foreach(fs, p, lookup_child_subr, &arg);
return arg.dir_cnt;
}
struct lookup_parent_arg {
v7fs_ino_t target_ino;
v7fs_ino_t parent_ino;
};
static int
lookup_parent_from_file_subr(struct v7fs_self *fs, void *ctx,
struct v7fs_inode *p, v7fs_ino_t ino)
{
struct lookup_parent_arg *arg = (struct lookup_parent_arg *)ctx;
if (!v7fs_inode_isdir(p))
return 0;
if (v7fs_file_lookup_by_number(fs, p, arg->target_ino, NULL)) {
arg->parent_ino = ino;
return V7FS_ITERATOR_BREAK;
}
return 0;
}
static v7fs_ino_t
lookup_parent_from_file(struct v7fs_self *fs, v7fs_ino_t ino)
{
struct lookup_parent_arg arg = { .target_ino = ino, .parent_ino = 0 };
v7fs_ilist_foreach(fs, lookup_parent_from_file_subr, &arg);
return arg.parent_ino;
}
static int
lookup_parent_from_dir_subr(struct v7fs_self *fs, void *ctx, v7fs_daddr_t blk,
size_t sz)
{
struct lookup_parent_arg *arg = (struct lookup_parent_arg *)ctx;
void *buf;
int ret = 0;
if (!(buf = scratch_read(fs, blk)))
return 0;
struct v7fs_dirent *dir = (struct v7fs_dirent *)buf;
size_t i, n = sz / sizeof(*dir);
if (!v7fs_dirent_endian_convert(fs, dir, n)) {
scratch_free(fs, buf);
return V7FS_ITERATOR_ERROR;
}
for (i = 0; i < n; i++, dir++) {
if (strncmp(dir->name, "..", 2) != 0)
continue;
arg->parent_ino = dir->inode_number;
ret = V7FS_ITERATOR_BREAK;
break;
}
scratch_free(fs, buf);
return ret;
}
static v7fs_ino_t
lookup_parent_from_dir(struct v7fs_self *fs, struct v7fs_inode *p)
{
struct lookup_parent_arg arg = { .target_ino = 0, .parent_ino = 0 };
v7fs_datablock_foreach(fs, p, lookup_parent_from_dir_subr, &arg);
return arg.parent_ino;
}
static int
pathname_check_file(struct v7fs_self *fs, struct v7fs_inode *p, v7fs_ino_t ino)
{
v7fs_ino_t parent_ino;
struct v7fs_inode parent_inode;
int error = 0;
if (ino == 1)
return 0;
if (!(parent_ino = lookup_parent_from_file(fs, ino)) ||
(error = v7fs_inode_load(fs, &parent_inode, parent_ino)) ||
!v7fs_inode_isdir(&parent_inode)) {
pwarn("*** inode#%d don't have parent.", ino);
v7fs_inode_dump(p);
error = connect_lost_and_found(fs, ino);
}
return error;
}
static int
pathname_check_dir(struct v7fs_self *fs, struct v7fs_inode *p, v7fs_ino_t ino)
{
v7fs_ino_t parent_ino;
struct v7fs_inode parent_inode;
int error = 0;
if (!(parent_ino = lookup_parent_from_dir(fs, p)) ||
(error = v7fs_inode_load(fs, &parent_inode, parent_ino)) ||
!v7fs_inode_isdir(&parent_inode)) {
pwarn("*** ino#%d parent dir missing parent=%d", ino,
parent_ino);
v7fs_inode_dump(p);
if ((error = connect_lost_and_found(fs, ino)))
return error;
}
int cnt = lookup_child_from_dir(fs, p, false);
if ((error = (cnt != p->nlink))) {
pwarn("*** ino#%d corrupt link # of child"
" dir:%d(inode) != %d(cnt)", ino, p->nlink, cnt);
v7fs_inode_dump(p);
lookup_child_from_dir(fs, p, true);
if (reply("CORRECT?")) {
p->nlink = cnt;
v7fs_inode_writeback(fs, p);
error = 0;
} else {
error = FSCK_EXIT_CHECK_FAILED;
}
}
return error;
}
static int
pathname_subr(struct v7fs_self *fs, void *ctx __unused, struct v7fs_inode *p,
v7fs_ino_t ino)
{
int error = 0;
if (!v7fs_inode_allocated(p))
return 0;
progress(0);
if (v7fs_inode_isdir(p)) {
error = pathname_check_dir(fs, p, ino);
} else if (v7fs_inode_isfile(p)) {
error = pathname_check_file(fs, p, ino);
}
return error;
}
int
pathname_check(struct v7fs_self *fs)
{
struct v7fs_superblock *sb = &fs->superblock;
int inodes = V7FS_MAX_INODE(sb) - sb->total_freeinode;
progress(&(struct progress_arg){ .label = "pathname", .tick = inodes /
PROGRESS_BAR_GRANULE });
return v7fs_ilist_foreach(fs, pathname_subr, 0);
}
char *
filename(struct v7fs_self *fs, v7fs_ino_t ino)
{
static char path[V7FS_PATH_MAX];
struct v7fs_inode inode;
v7fs_ino_t parent;
int error;
char name[V7FS_NAME_MAX + 1];
char *p = path + V7FS_PATH_MAX;
size_t n;
if ((error = v7fs_inode_load(fs, &inode, ino)))
return 0;
if (v7fs_inode_isdir(&inode))
parent = lookup_parent_from_dir(fs, &inode);
else
parent = lookup_parent_from_file(fs, ino);
if ((error = v7fs_inode_load(fs, &inode, parent)))
return 0;
if (!v7fs_file_lookup_by_number(fs, &inode, ino, name))
return 0;
n = strlen(name) + 1;
strcpy(p -= n, name);
ino = parent;
while (parent != V7FS_ROOT_INODE) {
parent = lookup_parent_from_dir(fs, &inode);
if ((error = v7fs_inode_load(fs, &inode, parent)))
return 0;
if (!v7fs_file_lookup_by_number(fs, &inode, ino, name))
return 0;
ino = parent;
n = strlen(name) + 1;
strcpy(p - n, name);
p[-1] = '/';
p -= n;
}
*--p = '/';
return p;
}