+static int prepare_symlink_tree(void)
+{
+ int i;
+ char tmppath[PATH_MAX] = {};
+
+ /* Create the temporary directory */
+ if (mkdtemp(tree_origin) == NULL) {
+ PRINT_ERR("failed to mkdtemp");
+ goto error;
+ }
+
+ /* Create the directories of the test tree */
+ for (i = 0; i < num_tree_dirs; i++) {
+ snprintf(tmppath, sizeof(tmppath), "%s/%s", tree_origin,
+ tree_dirs[i]);
+
+ if (mkdir(tmppath, 0755) != 0) {
+ PRINT_ERR("mkdir failed with path \"%s\"", tmppath);
+ goto error;
+ }
+ }
+
+ /* Create the symlinks of the test tree */
+ for (i = 0; i < num_tree_symlinks; i++) {
+ snprintf(tmppath, sizeof(tmppath), "%s/%s",
+ tree_origin, tree_symlinks[i].orig);
+
+ if (symlink(tree_symlinks[i].dest, tmppath) != 0) {
+ PRINT_ERR("failed to symlink \"%s\" to \"%s\"", tmppath,
+ tree_symlinks[i].dest);
+ goto error;
+ }
+ }
+
+ return 0;
+
+error:
+ return 1;
+}
+
+static int free_symlink_tree(void)
+{
+ int i;
+ char tmppath[PATH_MAX];
+
+ /* Remove the symlinks from the test tree */
+ for (i = num_tree_symlinks - 1; i > -1; i--) {
+ snprintf(tmppath, PATH_MAX, "%s/%s",
+ tree_origin, tree_symlinks[i].orig);
+
+ if (unlink(tmppath) != 0) {
+ PRINT_ERR("failed to unlink \"%s\"", tmppath);
+ goto error;
+ }
+ }
+
+ /* Remove the directories from the test tree */
+ for (i = num_tree_dirs - 1; i > -1; i--) {
+ snprintf(tmppath, PATH_MAX, "%s/%s", tree_origin, tree_dirs[i]);
+
+ if (rmdir(tmppath) != 0) {
+ PRINT_ERR("failed to rmdir \"%s\"", tmppath);
+ goto error;
+ }
+ }
+
+ /* Remove the temporary directory */
+ if (rmdir(tree_origin) != 0) {
+ PRINT_ERR("failed to rmdir \"%s\"", tree_origin);
+ goto error;
+ }
+
+ return 0;
+
+error:
+ return 1;
+}
+