+static bool keep_sysupgrade;
+
+static ssize_t
+fs_getxattr(const char *path, const char *name, void *value, size_t size)
+{
+ return syscall(__NR_getxattr, path, name, value, size);
+}
+
+static ssize_t
+fs_setxattr(const char *path, const char *name, const void *value, size_t size, int flags)
+{
+ return syscall(__NR_setxattr, path, name, value, size, flags);
+}
+
+static int
+handle_rmdir(const char *dir)
+{
+ struct dirent *dt;
+ struct stat st;
+ DIR *d;
+ int fd;
+
+ d = opendir(dir);
+ if (!d)
+ return -1;
+
+ fd = dirfd(d);
+
+ while ((dt = readdir(d)) != NULL) {
+ if (fstatat(fd, dt->d_name, &st, AT_SYMLINK_NOFOLLOW) || S_ISDIR(st.st_mode))
+ continue;
+
+ if (keep_sysupgrade && !strcmp(dt->d_name, "sysupgrade.tgz"))
+ continue;
+
+ unlinkat(fd, dt->d_name, 0);
+ }
+
+ closedir(d);
+ rmdir(dir);
+
+ return 0;
+}
+