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(path
, ino
)
21 /* Given an inode, construct the path identifying that inode.
23 char buf
[PATH_MAX
], *p
, *prefix
;
24 size_t len
, plen
, total
;
26 p
= &buf
[sizeof(buf
) - 1];
29 dprintf(("%s: make_path: constructing path for inode %d\n",
30 sffs_name
, ino
->i_num
));
32 /* Get the length of the prefix, skipping any leading slashes. */
33 for (prefix
= sffs_params
->p_prefix
; prefix
[0] == '/'; prefix
++);
34 plen
= strlen(prefix
);
36 /* Construct the path right-to-left in a temporary buffer first. */
37 for (total
= plen
; ino
!= NULL
&& !IS_ROOT(ino
); ino
= ino
->i_parent
) {
38 len
= strlen(ino
->i_name
);
43 if (total
>= sizeof(buf
))
47 memcpy(p
+ 1, ino
->i_name
, len
);
50 /* If any of the intermediate inodes has no parent, the final inode is no
51 * longer addressable by name.
56 /* Put the result in the actual buffer. We need the leading slash in the
57 * temporary buffer only when the prefix is not empty.
59 if (!prefix
[0] && p
[0] == '/') p
++;
61 strlcpy(path
, prefix
, PATH_MAX
);
62 strlcpy(&path
[plen
], p
, PATH_MAX
- plen
);
64 dprintf(("%s: make_path: resulting path is '%s'\n", sffs_name
, path
));
69 /*===========================================================================*
71 *===========================================================================*/
72 int push_path(path
, name
)
76 /* Add a component to the end of a path.
84 if (len
+ add
>= PATH_MAX
)
87 if (len
> 0) path
[len
++] = '/';
88 strlcpy(&path
[len
], name
, PATH_MAX
- len
);
93 /*===========================================================================*
95 *===========================================================================*/
99 /* Remove the last component from a path.
103 p
= strrchr(path
, '/');
108 /* Can't pop the root component */