1 /* This file contains routines for creating and manipulating path strings.
3 * The entry points into this file are:
4 * make_path construct a path string for an inode
5 * push_path add a path component to the end of a path string
6 * pop_path remove the last path component from a path string
9 * April 2009 (D.C. van Moolenbroek)
14 /*===========================================================================*
16 *===========================================================================*/
17 int make_path(char path
[PATH_MAX
], struct inode
*ino
)
19 /* Given an inode, construct the path identifying that inode.
21 char buf
[PATH_MAX
], *p
, *prefix
;
22 size_t len
, plen
, total
;
24 p
= &buf
[sizeof(buf
) - 1];
27 dprintf(("%s: make_path: constructing path for inode %d\n",
28 sffs_name
, ino
->i_num
));
30 /* Get the length of the prefix, skipping any leading slashes. */
31 for (prefix
= sffs_params
->p_prefix
; prefix
[0] == '/'; prefix
++);
32 plen
= strlen(prefix
);
34 /* Construct the path right-to-left in a temporary buffer first. */
35 for (total
= plen
; ino
!= NULL
&& !IS_ROOT(ino
); ino
= ino
->i_parent
) {
36 len
= strlen(ino
->i_name
);
41 if (total
>= sizeof(buf
))
45 memcpy(p
+ 1, ino
->i_name
, len
);
48 /* If any of the intermediate inodes has no parent, the final inode is no
49 * longer addressable by name.
54 /* Put the result in the actual buffer. We need the leading slash in the
55 * temporary buffer only when the prefix is not empty.
57 if (!prefix
[0] && p
[0] == '/') p
++;
59 strlcpy(path
, prefix
, PATH_MAX
);
60 strlcpy(&path
[plen
], p
, PATH_MAX
- plen
);
62 dprintf(("%s: make_path: resulting path is '%s'\n", sffs_name
, path
));
67 /*===========================================================================*
69 *===========================================================================*/
70 int push_path(char path
[PATH_MAX
], char *name
)
72 /* Add a component to the end of a path.
80 if (len
+ add
>= PATH_MAX
)
83 if (len
> 0) path
[len
++] = '/';
84 strlcpy(&path
[len
], name
, PATH_MAX
- len
);
89 /*===========================================================================*
91 *===========================================================================*/
92 void pop_path(char path
[PATH_MAX
])
94 /* Remove the last component from a path.
98 p
= strrchr(path
, '/');
103 /* Can't pop the root component */