- int err, r;
- FILE *fp;
- char *cmd;
-
- if (!root_mounted || dir[0] != '/') {
- cancel_receive ();
- reply_with_error ("root must be mounted and path must be absolute");
- return -1;
- }
-
- /* "tar -C /sysroot%s -zxf -" but we have to quote the dir. */
- if (asprintf_nowarn (&cmd, "tar -C %R -%cxf -", dir, filter) == -1) {
- err = errno;
- cancel_receive ();
- errno = err;
- reply_with_perror ("asprintf");
- return -1;
- }
-
- if (verbose)
- fprintf (stderr, "%s\n", cmd);
-
- fp = popen (cmd, "w");
- if (fp == NULL) {
- err = errno;
- cancel_receive ();
- errno = err;
- reply_with_perror ("%s", cmd);
- free (cmd);
- return -1;
- }
- free (cmd);
-
- int fd = fileno (fp);
-
- r = receive_file (write_cb, &fd);
- if (r == -1) { /* write error */
- cancel_receive ();
- reply_with_error ("write error on directory: %s", dir);
- pclose (fp);
- return -1;
- }
- if (r == -2) { /* cancellation from library */
- pclose (fp);
- /* Do NOT send any error. */
- return -1;
- }
-
- if (pclose (fp) != 0) {
- if (r == -1) /* if r == 0, file transfer ended already */
- cancel_receive ();
- reply_with_error ("tar subcommand failed on directory: %s", dir);
- return -1;
- }
-
- return 0;