This is an automated email from the git hooks/post-receive script.

git pushed a commit to branch tymux-12-session-parity
in repository terminology.

View the commit online.

commit 3cb93c47139b4e7953d4457a2bf41587b5f61163
Author: Cedric BAIL <[email protected]>
AuthorDate: Sun Nov 22 23:49:12 2026 -0600

    termd_tymux: send tysend transfers to the acting client
    
    tysend was refused in a session. Its commands now go to the acting
    client, which asks where to save the file as a local terminal does.
    tysend waits for each answer with no timeout, so the daemon or the
    client refuses whenever nobody else will answer.
    
    An approval belongs to the session that asked for it. Switching the
    terminal to another pty or losing the acting role drops the prompt and
    the transfer, as an answer would otherwise reach another program or a
    shell, or let a session write to a file it never asked for. For the
    same reason, a tysend that gives up on a file closes its prompt, and
    Cancel only answers while a request is pending.
    
    A data packet that is truncated or has an odd length is now refused
    instead of being read past its end.
---
 man/tymux.1               |   6 +-
 src/bin/termd_tymux.c     |  49 +++++++---
 src/bin/termio.c          | 156 ++++++++++++++++---------------
 src/bin/termiointernals.h |   1 +
 src/bin/termpty.c         |  56 +++++++++++
 src/bin/termpty.h         |   3 +
 src/bin/termsrv.c         |  10 +-
 src/bin/termtymux.c       |   3 +
 src/bin/tytest.c          |   2 +
 src/bin/tytest_tymux.c    | 230 ++++++++++++++++++++++++++++++++++++++++------
 src/bin/tytest_tymux.h    |   1 +
 src/bin/unit_tests.h      |   1 +
 src/bin/win.c             |  48 ++++++++++
 tests/meson.build         |   2 +-
 14 files changed, 443 insertions(+), 125 deletions(-)

diff --git a/man/tymux.1 b/man/tymux.1
index 2eafdf6f..100cae31 100644
--- a/man/tymux.1
+++ b/man/tymux.1
@@ -255,8 +255,10 @@ popups, the background and the alpha commands to the one terminal that acts
 for the session, its primary one or else the oldest that shares it, never a
 read\-only or paused one. Only that terminal reports the events of the blocks
 to the application. The daemon answers the grid size query itself, with the
-cell size of a native terminal, or 8 by 16 when the session has none, and
-refuses the files \fBtysend\fP sends.
+cell size of a native terminal, or 8 by 16 when the session has none.
+The terminal that acts for the session receives the files \fBtysend\fP sends,
+after the user approves them; the daemon refuses them when the session has no
+native terminal.
 Hyperlinks (OSC 8) work in a native hand-off, which reads their targets from
 the daemon; the inline proxy draws them as plain text.
 .PP
diff --git a/src/bin/termd_tymux.c b/src/bin/termd_tymux.c
index 0acccf48..370da73e 100644
--- a/src/bin/termd_tymux.c
+++ b/src/bin/termd_tymux.c
@@ -386,6 +386,18 @@ _actions_update(TermDTymux *td)
      }
 }
 
+/* Whether the queue of client can take a command of len bytes whole. */
+static Eina_Bool
+_client_command_fits(const Client *client, size_t len)
+{
+   const size_t chunk_max = TERMSRV_MAX_MSG_PAYLOAD - sizeof(TermSrvMsgCommand);
+   size_t fragments = (len + chunk_max - 1) / chunk_max;
+
+   return eina_binbuf_length_get(client->out) + len +
+     fragments * (sizeof(TermSrvMsgHdr) + sizeof(TermSrvMsgCommand)) <=
+     CLIENT_COMMAND_MAX;
+}
+
 /* Queue cmd for client, in as many messages as it takes. A command the
  * queue cannot take whole is skipped for this client, as it must not be
  * dropped for what the output of a program makes the daemon send, and a
@@ -395,12 +407,10 @@ static Eina_Bool
 _client_command_send(Client *client, const char *cmd, size_t len)
 {
    const size_t chunk_max = TERMSRV_MAX_MSG_PAYLOAD - sizeof(TermSrvMsgCommand);
-   size_t fragments = (len + chunk_max - 1) / chunk_max, off;
    unsigned char msg[TERMSRV_MAX_MSG_PAYLOAD];
+   size_t off;
 
-   if (eina_binbuf_length_get(client->out) + len +
-       fragments * (sizeof(TermSrvMsgHdr) + sizeof(TermSrvMsgCommand)) >
-       CLIENT_COMMAND_MAX)
+   if (!_client_command_fits(client, len))
      {
         WRN("client %d does not keep up, skipping a command", client->fd);
         return EINA_TRUE;
@@ -471,27 +481,32 @@ _chids_replay(Client *client)
    return EINA_TRUE;
 }
 
-/* To every native client, or to the action client alone. */
-static void
+/* To every native client, or to the action client alone. Returns whether
+ * some client was sent cmd. */
+static Eina_Bool
 _command_send(TermDTymux *td, const char *cmd, Eina_Bool action_only)
 {
    size_t len = strlen(cmd);
    Eina_List *l, *l_next;
    Client *client;
+   Eina_Bool sent = EINA_FALSE;
 
    EINA_LIST_FOREACH_SAFE(td->clients, l, l_next, client)
      {
         if (action_only ? !client->acts : !client->native)
           continue;
+        Eina_Bool fits = _client_command_fits(client, len);
+
         if (!_client_command_send(client, cmd, len))
           _client_del(client);
+        else if (fits)
+          sent = EINA_TRUE;
      }
+   return sent;
 }
 
-/* The commands of a session with no widget to drive: the media blocks, the
- * size of the grid, which tycat and tyls wait for with no timeout, and the
- * start of a file transfer, which tysend gives up on unless it is answered
- * with k: stage 1 takes no file. */
+/* The commands of a session with no widget to drive: the media blocks, and
+ * the size of the grid, which tycat and tyls wait for with no timeout. */
 static void
 _command_daemon(TermDTymux *td, const char *cmd)
 {
@@ -507,8 +522,15 @@ _command_daemon(TermDTymux *td, const char *cmd)
 
         termpty_write(ty, reply, len);
      }
-   else if (cmd[0] == 'f' && cmd[1] == 's')
-     termpty_write(ty, "n\n", 2);
+}
+
+/* tysend blocks with no timeout for the answer to fs and to each fd: a
+ * command that no client takes is refused here. fr and fx have no answer. */
+static void
+_transfer_refuse(TermDTymux *td, const char *cmd)
+{
+   if (cmd[0] == 'f' && (cmd[1] == 's' || cmd[1] == 'd'))
+     termpty_write(td->pty, "n\n", 2);
 }
 
 static void
@@ -528,7 +550,8 @@ _cb_pty_command(void *data)
          _command_send(td, cmd, EINA_FALSE);
          break;
       case TSRV_CMD_ACTION:
-         _command_send(td, cmd, EINA_TRUE);
+         if (!_command_send(td, cmd, EINA_TRUE))
+           _transfer_refuse(td, cmd);
          break;
       case TSRV_CMD_DROP:
          break;
diff --git a/src/bin/termio.c b/src/bin/termio.c
index 0c99bfba..54752d83 100644
--- a/src/bin/termio.c
+++ b/src/bin/termio.c
@@ -3606,6 +3606,32 @@ _smart_cb_gest_zoom_abort(void *data, void *_event EINA_UNUSED)
 
 /* }}} */
 
+/* Drops a transfer, and the request to approve one, without answering: the
+ * pty that asked is no longer the one sd->pty, or no longer ours to answer
+ * for, and a reply would reach a program or a shell that never asked. The
+ * window is told to delete its popup. */
+static void
+_sendfile_abort(Evas_Object *obj, Termio *sd)
+{
+   if (sd->sendfile.file)
+     {
+        if (sd->sendfile.active) ecore_file_unlink(sd->sendfile.file);
+        eina_stringshare_del(sd->sendfile.file);
+        sd->sendfile.file = NULL;
+     }
+   if (sd->sendfile.f)
+     {
+        fclose(sd->sendfile.f);
+        sd->sendfile.f = NULL;
+     }
+   sd->sendfile.active = EINA_FALSE;
+   sd->sendfile.awaiting = EINA_FALSE;
+   sd->sendfile.progress = 0.0;
+   sd->sendfile.total = 0;
+   sd->sendfile.size = 0;
+   evas_object_smart_callback_call(obj, "send,abort", NULL);
+}
+
 Eina_Bool
 termio_file_send_ok(const Evas_Object *obj, const char *file)
 {
@@ -3615,19 +3641,27 @@ termio_file_send_ok(const Evas_Object *obj, const char *file)
    if (!sd) return EINA_FALSE;
    if (!file) return EINA_FALSE;
    ty = sd->pty;
+#ifdef HAVE_TYMUX
+   if (sd->tymux && !termtymux_acts_get(sd->tymux))
+     {
+        _sendfile_abort((Evas_Object *)obj, sd);
+        return EINA_FALSE;
+     }
+#endif
+   sd->sendfile.awaiting = EINA_FALSE;
    sd->sendfile.f = fopen(file, "w");
    if (sd->sendfile.f)
      {
         eina_stringshare_del(sd->sendfile.file);
         sd->sendfile.file = eina_stringshare_add(file);
         sd->sendfile.active = EINA_TRUE;
-        termpty_write(ty, "k\n", 2);
+        termpty_write_reply(ty, "k\n", 2);
         return EINA_TRUE;
      }
    eina_stringshare_del(sd->sendfile.file);
    sd->sendfile.file = NULL;
    sd->sendfile.active = EINA_FALSE;
-   termpty_write(ty, "n\n", 2);
+   termpty_write_reply(ty, "n\n", 2);
    return EINA_FALSE;
 }
 
@@ -3639,6 +3673,8 @@ termio_file_send_cancel(const Evas_Object *obj)
 
    if (!sd) return;
    ty = sd->pty;
+   if (!sd->sendfile.active && !sd->sendfile.awaiting) return;
+   sd->sendfile.awaiting = EINA_FALSE;
    if (!sd->sendfile.active) goto done;
    sd->sendfile.progress = 0.0;
    sd->sendfile.total = 0;
@@ -3656,7 +3692,7 @@ termio_file_send_cancel(const Evas_Object *obj)
      }
    sd->sendfile.active = EINA_FALSE;
 done:
-   termpty_write(ty, "n\n", 2);
+   termpty_write_reply(ty, "n\n", 2);
 }
 
 double
@@ -4269,6 +4305,7 @@ _termio_tymux_leave(Evas_Object *obj, Termio *sd)
    Ecore_Window window_id = elm_win_window_id_get(sd->win);
    const char *chid;
 
+   _sendfile_abort(obj, sd);
    termtymux_free(sd->tymux); /* also frees the shadow pty */
    sd->tymux = NULL;
    sd->pty = NULL;
@@ -4541,13 +4578,15 @@ _block_listen_cb(const Eina_Hash *_hash EINA_UNUSED,
 
 /* Fired by termtymux.c when this terminal starts or stops acting for its
  * session: the blocks already shown must follow, as no new command makes
- * them. */
+ * them, and a file it was asked to approve is no longer its to take. */
 static void
-_termio_tymux_acts(void *data, Eina_Bool _acts EINA_UNUSED)
+_termio_tymux_acts(void *data, Eina_Bool acts)
 {
    Evas_Object *obj = data;
    Termio *sd = evas_object_smart_data_get(obj);
 
+   if (sd && !acts)
+     _sendfile_abort(obj, sd);
    if (sd && sd->pty && sd->pty->block.blocks)
      eina_hash_foreach(sd->pty->block.blocks, _block_listen_cb, sd);
 }
@@ -4600,6 +4639,7 @@ _termio_tymux_swap(Evas_Object *obj, Termio *sd, TermTymux *tymux,
     * another leaves that cwd, and tymux_switched_in, as they already are:
     * this terminal's local shell, if it has one to go back to, is further
     * back than the session being replaced. */
+   _sendfile_abort(obj, sd);
    if (sd->tymux)
      {
         termtymux_free(sd->tymux); /* also frees the old sd->pty */
@@ -4994,6 +5034,7 @@ _smart_pty_command(void *data)
      {
         if (ty->cur_cmd[1] == 'r') // receive
           {
+             sd->sendfile.awaiting = EINA_TRUE;
              sd->sendfile.progress = 0.0;
              sd->sendfile.total = 0;
              sd->sendfile.size = 0;
@@ -5005,85 +5046,50 @@ _smart_pty_command(void *data)
           }
         else if (ty->cur_cmd[1] == 'd') // data packet
           {
-             const char *p = strchr(ty->cur_cmd, ' ');
-             Eina_Bool valid = EINA_TRUE;
+             Eina_Binbuf *bb = eina_binbuf_new();
 
-             if (p)
+             if (bb && termpty_file_packet_decode(ty->cur_cmd, bb) &&
+                 sd->sendfile.active)
                {
-                  Eina_Binbuf *bb = eina_binbuf_new();
+                  size_t size = eina_binbuf_length_get(bb);
 
-                  if (bb)
+                  sd->sendfile.total += size;
+                  if (sd->sendfile.size > 0.0)
                     {
-                       unsigned char localbuf[128];
-                       unsigned char localbufpos = 0;
-                       int pksum = atoi(&(ty->cur_cmd[2]));
-                       int sum;
-
-                       eina_binbuf_expand(bb, 32 * 1024);
-                       for (sum = 0, p++; *p; p++)
-                         {
-                            // high nibble
-                            unsigned char v = (unsigned char)(*p);
-                            sum += v;
-                            v = ((v - '@') & 0xf) << 4;
-                            // low nibble
-                            p++;
-                            sum += *p;
-                            v |= ((*p - '@') & 0xf);
-                            localbuf[localbufpos++] = v;
-                            if (localbufpos >= sizeof(localbuf))
-                              {
-                                 eina_binbuf_append_length(bb, localbuf, localbufpos);
-                                 localbufpos = 0;
-                              }
-                            if (!*p) valid = EINA_FALSE;
-                         }
-                       if (localbufpos > 0)
-                         eina_binbuf_append_length(bb, localbuf, localbufpos);
-
-                       if ((valid) && (sum == pksum) && (sd->sendfile.active))
-                         {
-                            // write "ok" (k) to term
-                            size_t size = eina_binbuf_length_get(bb);
-
-                            sd->sendfile.total += size;
-                            if (sd->sendfile.size > 0.0)
-                              {
-                                 sd->sendfile.progress =
-                                   (double)sd->sendfile.total /
-                                   (double)sd->sendfile.size;
-                                 evas_object_smart_callback_call
-                                   (obj, "send,progress", NULL);
-                              }
-                            fwrite(eina_binbuf_string_get(bb), size, 1,
-                                   sd->sendfile.f);
-                            termpty_write(ty, "k\n", 2);
-                         }
-                       else
-                         {
-                            // write "not valid" (n) to term
-                            if (sd->sendfile.file)
-                              {
-                                 ecore_file_unlink(sd->sendfile.file);
-                                 eina_stringshare_del(sd->sendfile.file);
-                                 sd->sendfile.file = NULL;
-                              }
-                            if (sd->sendfile.f)
-                              {
-                                 fclose(sd->sendfile.f);
-                                 sd->sendfile.f = NULL;
-                              }
-                            sd->sendfile.active = EINA_FALSE;
-                            termpty_write(ty, "n\n", 2);
-                            evas_object_smart_callback_call
-                              (obj, "send,end", NULL);
-                         }
-                       eina_binbuf_free(bb);
+                       sd->sendfile.progress =
+                         (double)sd->sendfile.total /
+                         (double)sd->sendfile.size;
+                       evas_object_smart_callback_call
+                         (obj, "send,progress", NULL);
                     }
+                  fwrite(eina_binbuf_string_get(bb), size, 1,
+                         sd->sendfile.f);
+                  termpty_write_reply(ty, "k\n", 2);
                }
+             else
+               {
+                  if (sd->sendfile.file)
+                    {
+                       ecore_file_unlink(sd->sendfile.file);
+                       eina_stringshare_del(sd->sendfile.file);
+                       sd->sendfile.file = NULL;
+                    }
+                  if (sd->sendfile.f)
+                    {
+                       fclose(sd->sendfile.f);
+                       sd->sendfile.f = NULL;
+                    }
+                  sd->sendfile.active = EINA_FALSE;
+                  termpty_write_reply(ty, "n\n", 2);
+                  evas_object_smart_callback_call
+                    (obj, "send,end", NULL);
+               }
+             if (bb) eina_binbuf_free(bb);
           }
         else if (ty->cur_cmd[1] == 'x') // exit data stream
           {
+             if (sd->sendfile.awaiting)
+               _sendfile_abort(obj, sd);
              if (sd->sendfile.active)
                {
                   sd->sendfile.progress = 0.0;
diff --git a/src/bin/termiointernals.h b/src/bin/termiointernals.h
index 0e40556a..124dae5a 100644
--- a/src/bin/termiointernals.h
+++ b/src/bin/termiointernals.h
@@ -95,6 +95,7 @@ struct tag_Termio
       double progress;
       unsigned long long total, size;
       Eina_Bool active : 1;
+      Eina_Bool awaiting : 1; /* tysend waits for the answer of the user */
    } sendfile;
    struct {
       unsigned char state; /* a Termpty_Progress_State, as last shown */
diff --git a/src/bin/termpty.c b/src/bin/termpty.c
index 9674b33a..72031fa0 100644
--- a/src/bin/termpty.c
+++ b/src/bin/termpty.c
@@ -1481,6 +1481,35 @@ termpty_write_reply(Termpty *ty, const char *input, int len)
    ty->write_reply = 0;
 }
 
+Eina_Bool
+termpty_file_packet_decode(const char *cmd, Eina_Binbuf *out)
+{
+   const char *p = strchr(cmd, ' ');
+   unsigned char localbuf[128];
+   unsigned char localbufpos = 0;
+   int sum = 0;
+
+   if (!p) return EINA_FALSE;
+   eina_binbuf_expand(out, 32 * 1024);
+   for (p++; *p; p += 2)
+     {
+        unsigned char high = *p, low = p[1];
+
+        if (!low) return EINA_FALSE;
+        sum += high + low;
+        localbuf[localbufpos++] = (((high - '@') & 0xf) << 4) |
+          ((low - '@') & 0xf);
+        if (localbufpos >= sizeof(localbuf))
+          {
+             eina_binbuf_append_length(out, localbuf, localbufpos);
+             localbufpos = 0;
+          }
+     }
+   if (localbufpos > 0)
+     eina_binbuf_append_length(out, localbuf, localbufpos);
+   return sum == atoi(cmd + 2);
+}
+
 struct screen_info
 {
    Termcell *screen;
@@ -7004,6 +7033,33 @@ tytest_backlog_store_anon_no_fd(void)
    return 0;
 }
 
+/* Test: a data packet of tysend decodes to its bytes when its sum is right,
+ * and is refused when it has no data, an odd count of characters, or a wrong
+ * sum, without reading past its end. */
+int
+tytest_termpty_file_packet(void)
+{
+   Eina_Binbuf *out;
+
+   eina_init();
+   out = eina_binbuf_new();
+   assert(out);
+   assert(termpty_file_packet_decode("fd137 DE", out));
+   assert(eina_binbuf_length_get(out) == 1 &&
+          eina_binbuf_string_get(out)[0] == 0x45);
+   eina_binbuf_reset(out);
+   assert(termpty_file_packet_decode("fd0 ", out));
+   assert(eina_binbuf_length_get(out) == 0);
+   assert(!termpty_file_packet_decode("fd137", out));
+   assert(!termpty_file_packet_decode("fd", out));
+   assert(!termpty_file_packet_decode("fd137 D", out));
+   assert(!termpty_file_packet_decode("fd137 DEF", out));
+   assert(!termpty_file_packet_decode("fd138 DE", out));
+   eina_binbuf_free(out);
+   eina_shutdown();
+   return 0;
+}
+
 #endif /* BINARY_TYTEST */
 
 #endif /* BINARY_TYFUZZ || BINARY_TYTEST */
diff --git a/src/bin/termpty.h b/src/bin/termpty.h
index 931170b3..bbe91917 100644
--- a/src/bin/termpty.h
+++ b/src/bin/termpty.h
@@ -478,6 +478,9 @@ void       termpty_write(Termpty *ty, const char *input, int len);
  * the user typed: termpty_write(), except that a write hook sees it with
  * write_reply set, to keep it out of what it does with keystrokes. */
 void       termpty_write_reply(Termpty *ty, const char *input, int len);
+/* Decodes the data of the "fd" command cmd of tysend into out. Returns
+ * EINA_FALSE if the packet is malformed or its sum is wrong. */
+Eina_Bool  termpty_file_packet_decode(const char *cmd, Eina_Binbuf *out);
 /* Redirect termpty_write() through FUNC instead of the real pty fd.
  * FUNC returns EINA_TRUE when it consumed the write, EINA_FALSE to fall
  * through to the normal pty write buffer. A NULL FUNC clears the hook. */
diff --git a/src/bin/termsrv.c b/src/bin/termsrv.c
index 1a21c11a..d5ee5d99 100644
--- a/src/bin/termsrv.c
+++ b/src/bin/termsrv.c
@@ -1182,7 +1182,7 @@ termsrv_command_route(const char *cmd)
          break;
       case 'f':
          if (strchr("rsdx", cmd[1]))
-           return TSRV_CMD_DAEMON;
+           return TSRV_CMD_ACTION;
          break;
      }
    return TSRV_CMD_DROP;
@@ -1439,10 +1439,10 @@ tytest_termsrv_command_route(void)
    assert(ROUTE("atnr;0123;0000abcd;x") == TSRV_CMD_DROP);
    assert(ROUTE("atc;0123;0000abcd") == TSRV_CMD_DROP);
    assert(ROUTE("ap;0123;0000abcd;x") == TSRV_CMD_ACTION);
-   assert(ROUTE("frfile") == TSRV_CMD_DAEMON);
-   assert(ROUTE("fs12") == TSRV_CMD_DAEMON);
-   assert(ROUTE("fd1 AB") == TSRV_CMD_DAEMON);
-   assert(ROUTE("fx") == TSRV_CMD_DAEMON);
+   assert(ROUTE("frfile") == TSRV_CMD_ACTION);
+   assert(ROUTE("fs12") == TSRV_CMD_ACTION);
+   assert(ROUTE("fd1 AB") == TSRV_CMD_ACTION);
+   assert(ROUTE("fx") == TSRV_CMD_ACTION);
    assert(ROUTE("fz") == TSRV_CMD_DROP);
    assert(ROUTE("zz") == TSRV_CMD_DROP);
    assert(ROUTE("z") == TSRV_CMD_DROP);
diff --git a/src/bin/termtymux.c b/src/bin/termtymux.c
index db9f2884..353860ec 100644
--- a/src/bin/termtymux.c
+++ b/src/bin/termtymux.c
@@ -1016,6 +1016,9 @@ _command_receive(TermTymux *ts, const void *payload, uint32_t size)
         ty->cb.command.func(ty->cb.command.data);
         ty->cur_cmd = NULL;
      }
+   else if (cmd[0] == 'f' && (cmd[1] == 's' || cmd[1] == 'd'))
+     /* tysend waits for this answer with no timeout */
+     termpty_write_reply(ty, "n\n", 2);
    free(cmd);
 }
 
diff --git a/src/bin/tytest.c b/src/bin/tytest.c
index 48e12131..8014d5f5 100644
--- a/src/bin/tytest.c
+++ b/src/bin/tytest.c
@@ -113,6 +113,7 @@ static struct {
        { "termpty_block_serialize", tytest_termpty_block_serialize},
        { "termpty_block_channel_find", tytest_termpty_block_channel_find},
        { "termpty_block_msg_count", tytest_termpty_block_msg_count},
+       { "termpty_file_packet", tytest_termpty_file_packet},
        { "termpty_block_channel_find_newer_claims", tytest_termpty_block_channel_find_newer_claims},
        { "backlog_row_get_args", tytest_backlog_row_get_args},
        { "percent_decode", tytest_percent_decode},
@@ -357,6 +358,7 @@ static const struct {
        { "tymux_live_commands_full", tytest_tymux_live_commands_full},
        { "tymux_live_commands_replay", tytest_tymux_live_commands_replay},
        { "tymux_live_commands_flood", tytest_tymux_live_commands_flood},
+       { "tymux_live_tysend", tytest_tymux_live_tysend},
 #endif
        { NULL, NULL},
 };
diff --git a/src/bin/tytest_tymux.c b/src/bin/tytest_tymux.c
index 87b80191..cd17be22 100644
--- a/src/bin/tytest_tymux.c
+++ b/src/bin/tytest_tymux.c
@@ -3065,6 +3065,47 @@ _commands_qs_check(TermTymux *ts, Termpty *ty)
    assert(tytest_tymux_wait(_file_done, &file, 5.0));
 }
 
+/* The shell command that runs tysend on a file of the session's directory,
+ * the way it fails left in tysend.err and tysend.rc. */
+static void
+_tysend_command(char *cmd, size_t size, const char *file)
+{
+   char exe[PATH_MAX], payload[sizeof(_runtime_dir) + 16];
+   char err[sizeof(_runtime_dir) + 16], rc[sizeof(_runtime_dir) + 16];
+   ssize_t n = readlink("/proc/self/exe", exe, sizeof(exe) - sizeof("/tysend"));
+
+   assert(n > 0);
+   exe[n] = '\0';
+   strcpy(strrchr(exe, '/'), "/tysend");
+   _dir_path(payload, sizeof(payload), file);
+   _dir_path(err, sizeof(err), "tysend.err");
+   _dir_path(rc, sizeof(rc), "tysend.rc");
+   snprintf(cmd, size, "%s %s 2> %s; echo $? > %s\n", exe, payload, err, rc);
+}
+
+static void
+_tysend_payload_write(const char *file)
+{
+   char payload[sizeof(_runtime_dir) + 16];
+   FILE *f;
+
+   _dir_path(payload, sizeof(payload), file);
+   f = fopen(payload, "w");
+   assert(f);
+   fputs("some bytes\n", f);
+   fclose(f);
+}
+
+static void
+_tysend_failed_check(void)
+{
+   File_Want failed = { "tysend.err", "Send Fail\n" };
+   File_Want done = { "tysend.rc", "255" };
+
+   assert(tytest_tymux_wait(_file_done, &done, 10.0));
+   assert(tytest_tymux_wait(_file_done, &failed, 1.0));
+}
+
 static void
 _commands_qs_no_native_check(void)
 {
@@ -3079,38 +3120,16 @@ _commands_qs_no_native_check(void)
             "printf '\\033}qs\\000'; read -r l; echo \"$l\" > %s\n", path);
    assert(termsrv_msg_send(raw.fd, TSRV_MSG_INPUT, cmd, strlen(cmd)) == 0);
    assert(tytest_tymux_wait(_file_done, &file, 10.0));
+   /* tysend waits for an answer with no timeout: a session with no client
+    * to approve the file refuses it at once */
+   _tysend_payload_write("payload");
+   _tysend_command(cmd, sizeof(cmd), "payload");
+   assert(termsrv_msg_send(raw.fd, TSRV_MSG_INPUT, cmd, strlen(cmd)) == 0);
+   _tysend_failed_check();
    termsrv_reader_clear(&raw.reader);
    close(raw.fd);
 }
 
-/* tysend asks for the file, and the answer of a session that takes none
- * makes it give up instead of waiting for ever. */
-static void
-_commands_tysend_check(TermTymux *ts, Termpty *ty)
-{
-   char exe[PATH_MAX], cmd[PATH_MAX + 256], payload[sizeof(_runtime_dir) + 16];
-   char err[sizeof(_runtime_dir) + 16], rc[sizeof(_runtime_dir) + 16];
-   File_Want failed = { "tysend.err", "Send Fail\n" }, done = { "tysend.rc", "255" };
-   ssize_t n = readlink("/proc/self/exe", exe, sizeof(exe) - sizeof("/tysend"));
-   FILE *f;
-
-   assert(n > 0);
-   exe[n] = '\0';
-   strcpy(strrchr(exe, '/'), "/tysend");
-   _dir_path(payload, sizeof(payload), "payload");
-   _dir_path(err, sizeof(err), "tysend.err");
-   _dir_path(rc, sizeof(rc), "tysend.rc");
-   f = fopen(payload, "w");
-   assert(f);
-   fputs("some bytes\n", f);
-   fclose(f);
-   snprintf(cmd, sizeof(cmd), "%s %s 2> %s; echo $? > %s\n", exe, payload,
-            err, rc);
-   _session_type(ts, ty, cmd);
-   assert(tytest_tymux_wait(_file_done, &done, 10.0));
-   assert(tytest_tymux_wait(_file_done, &failed, 1.0));
-}
-
 /* The 200 KiB iC arrived whole, at every native client */
 static void
 _commands_big_check(Cmd_Log *logs[], int count)
@@ -3224,7 +3243,6 @@ _live_commands_body(void)
 
    /* the answers of the session */
    _commands_qs_check(a, ty_a);
-   _commands_tysend_check(a, ty_a);
 
    /* what a client says on behalf of the session is no keystroke, and is
     * the action client's alone */
@@ -3581,3 +3599,157 @@ tytest_tymux_live_commands_flood(void)
 {
    return tytest_tymux_live_run(_live_commands_flood_body);
 }
+
+#define TYSEND_SESSION "ty-tysend"
+#define TYSEND_FILE_SIZE (1024 * 1024)
+
+/* What the action client of the session did with a transfer, as termio
+ * does: approve the file, check the sum of each packet, keep its bytes. */
+typedef struct
+{
+   Termpty       *ty;
+   unsigned char *data;
+   size_t         size;
+   size_t         announced;
+   unsigned int   packets;
+   Eina_Bool      bad;
+   Eina_Bool      ended;
+} Tysend_Rx;
+
+static void
+_tysend_rx_packet(Tysend_Rx *rx, const char *cmd)
+{
+   const char *p = strchr(cmd, ' ');
+   int sum = 0;
+
+   if (!p || rx->size + strlen(p) / 2 > rx->announced)
+     {
+        rx->bad = EINA_TRUE;
+        termpty_write_reply(rx->ty, "n\n", 2);
+        return;
+     }
+   for (p++; p[0] && p[1]; p += 2)
+     {
+        sum += (unsigned char)p[0] + (unsigned char)p[1];
+        rx->data[rx->size++] = (((p[0] - '@') & 0xf) << 4) | ((p[1] - '@') & 0xf);
+     }
+   if (sum != atoi(cmd + 2) || *p)
+     {
+        rx->bad = EINA_TRUE;
+        termpty_write_reply(rx->ty, "n\n", 2);
+        return;
+     }
+   rx->packets++;
+   termpty_write_reply(rx->ty, "k\n", 2);
+}
+
+static void
+_tysend_rx_cb(void *data)
+{
+   Tysend_Rx *rx = data;
+   const char *cmd = rx->ty->cur_cmd;
+
+   assert(cmd[0] == 'f');
+   switch (cmd[1])
+     {
+      case 'r':
+         termpty_write_reply(rx->ty, "k\n", 2);
+         break;
+      case 's':
+         rx->announced = atoll(cmd + 2);
+         rx->data = "" + 1);
+         assert(rx->data);
+         break;
+      case 'd':
+         _tysend_rx_packet(rx, cmd);
+         break;
+      case 'x':
+         rx->ended = EINA_TRUE;
+         break;
+     }
+}
+
+static void
+_tysend_results_clear(void)
+{
+   char path[sizeof(_runtime_dir) + 16];
+
+   _dir_path(path, sizeof(path), "tysend.rc");
+   unlink(path);
+   _dir_path(path, sizeof(path), "tysend.err");
+   unlink(path);
+}
+
+static void
+_tysend_random_write(const char *file, unsigned char *sent)
+{
+   char path[sizeof(_runtime_dir) + 16];
+   int fd = open("/dev/urandom", O_RDONLY);
+   size_t got = 0;
+   FILE *f;
+
+   assert(fd >= 0);
+   while (got < TYSEND_FILE_SIZE)
+     {
+        ssize_t n = read(fd, sent + got, TYSEND_FILE_SIZE - got);
+
+        assert(n > 0);
+        got += n;
+     }
+   close(fd);
+   _dir_path(path, sizeof(path), file);
+   f = fopen(path, "w");
+   assert(f);
+   assert(fwrite(sent, 1, TYSEND_FILE_SIZE, f) == TYSEND_FILE_SIZE);
+   fclose(f);
+}
+
+/* tysend sends a file through a session to its action client, which the
+ * user's choice to turn the escapes off silences: tysend must then fail
+ * instead of waiting for ever. */
+static int
+_live_tysend_body(void)
+{
+   TermTymux *a;
+   Termpty *ty_a;
+   Tysend_Rx rx = { 0 };
+   File_Want done = { "tysend.rc", "0" };
+   char cmd[PATH_MAX + 256];
+   unsigned char *sent = malloc(TYSEND_FILE_SIZE);
+
+   assert(sent);
+   assert(tytest_tymux_start(TYSEND_SESSION, 80, 24, &a, &ty_a));
+   rx.ty = ty_a;
+   ty_a->cb.command.func = _tysend_rx_cb;
+   ty_a->cb.command.data = ""
+   assert(tytest_tymux_wait(_acts_done, &(Acts_Want){ a, EINA_TRUE }, 5.0));
+
+   _config->ty_escapes = EINA_FALSE;
+   _tysend_payload_write("small");
+   _tysend_command(cmd, sizeof(cmd), "small");
+   _session_type(a, ty_a, cmd);
+   _tysend_failed_check();
+   _config->ty_escapes = EINA_TRUE;
+   assert(!rx.data && !rx.packets);
+
+   _tysend_results_clear();
+   _tysend_random_write("big", sent);
+   _tysend_command(cmd, sizeof(cmd), "big");
+   _session_type(a, ty_a, cmd);
+   assert(tytest_tymux_wait(_file_done, &done, 30.0));
+   _idle();
+   assert(rx.ended && !rx.bad);
+   assert(rx.size == TYSEND_FILE_SIZE && !memcmp(rx.data, sent, TYSEND_FILE_SIZE));
+   assert(rx.packets > 1);
+
+   free(rx.data);
+   free(sent);
+   tytest_tymux_stop(a);
+   return 0;
+}
+
+int
+tytest_tymux_live_tysend(void)
+{
+   return tytest_tymux_live_run(_live_tysend_body);
+}
diff --git a/src/bin/tytest_tymux.h b/src/bin/tytest_tymux.h
index f508fe77..b497e3ad 100644
--- a/src/bin/tytest_tymux.h
+++ b/src/bin/tytest_tymux.h
@@ -68,5 +68,6 @@ int tytest_tymux_live_commands(void);
 int tytest_tymux_live_commands_full(void);
 int tytest_tymux_live_commands_replay(void);
 int tytest_tymux_live_commands_flood(void);
+int tytest_tymux_live_tysend(void);
 
 #endif
diff --git a/src/bin/unit_tests.h b/src/bin/unit_tests.h
index cced3912..ee966ee1 100644
--- a/src/bin/unit_tests.h
+++ b/src/bin/unit_tests.h
@@ -10,6 +10,7 @@ int tytest_termpty_block_command(void);
 int tytest_termpty_block_serialize(void);
 int tytest_termpty_block_channel_find(void);
 int tytest_termpty_block_msg_count(void);
+int tytest_termpty_file_packet(void);
 int tytest_termpty_block_channel_find_newer_claims(void);
 int tytest_write_hook(void);
 int tytest_backlog_long_wrap(void);
diff --git a/src/bin/win.c b/src/bin/win.c
index 6b1ecc30..332bcf57 100644
--- a/src/bin/win.c
+++ b/src/bin/win.c
@@ -6846,6 +6846,7 @@ _sendfile_progress_hide_delay(void *data)
    if (!term->sendfile_progress_enabled) return EINA_FALSE;
    term->sendfile_progress_enabled = EINA_FALSE;
    elm_layout_signal_emit(term->bg, "sendfile,progress,off", "terminology");
+   if (!term->sendfile_progress) return EINA_FALSE;
    t = evas_object_data_get(term->sendfile_progress, "sendfile-progress-timer");
    if (t) ecore_timer_del(t);
    t = ecore_timer_add(10.0, _sendfile_progress_reset, term->sendfile_progress);
@@ -6962,6 +6963,7 @@ _sendfile_request_hide_delay(void *data)
    if (!term->sendfile_request_enabled) return EINA_FALSE;
    term->sendfile_request_enabled = EINA_FALSE;
    elm_layout_signal_emit(term->bg, "sendfile,request,off", "terminology");
+   if (!term->sendfile_request) return EINA_FALSE;
    t = evas_object_data_get(term->sendfile_request, "sendfile-request-timer");
    if (t) ecore_timer_del(t);
    t = ecore_timer_add(10.0, _sendfile_request_reset, term->sendfile_request);
@@ -7235,6 +7237,51 @@ _cb_send_end(void *data,
    _sendfile_progress_hide(term);
 }
 
+/* The terminal dropped the transfer, which was asked of another pty or of
+ * a terminal that no longer acts: nothing of it may stay on screen or
+ * answer for it, so the popup and the progress panel go at once. */
+static void
+_cb_send_abort(void *data,
+               Evas_Object *_obj EINA_UNUSED,
+               void *_event EINA_UNUSED)
+{
+   Term *term = data;
+   Eina_Bool focused = EINA_FALSE;
+
+   if (term->sendfile_request_hide_timer)
+     ecore_timer_del(term->sendfile_request_hide_timer);
+   term->sendfile_request_hide_timer = NULL;
+   if (term->sendfile_progress_hide_timer)
+     ecore_timer_del(term->sendfile_progress_hide_timer);
+   term->sendfile_progress_hide_timer = NULL;
+   if (term->sendfile_request_enabled)
+     {
+        term->sendfile_request_enabled = EINA_FALSE;
+        elm_layout_signal_emit(term->bg, "sendfile,request,off",
+                               "terminology");
+     }
+   if (term->sendfile_progress_enabled)
+     {
+        term->sendfile_progress_enabled = EINA_FALSE;
+        elm_layout_signal_emit(term->bg, "sendfile,progress,off",
+                               "terminology");
+     }
+   if (term->sendfile_request)
+     {
+        focused = elm_object_focus_get(term->sendfile_request);
+        evas_object_del(term->sendfile_request);
+     }
+   if (term->sendfile_progress)
+     {
+        focused |= elm_object_focus_get(term->sendfile_progress);
+        evas_object_del(term->sendfile_progress);
+     }
+   term->sendfile_request = NULL;
+   term->sendfile_progress = NULL;
+   term->sendfile_progress_bar = NULL;
+   if (focused) term_focus(term);
+}
+
 static Eina_Bool
 _cb_cmd_del(void *data)
 {
@@ -8068,6 +8115,7 @@ term_new(Win *wn, Config *config, const char *cmd,
    evas_object_smart_callback_add(o, "progress,change", _cb_progress_change,
                                   term);
    evas_object_smart_callback_add(o, "send,end", _cb_send_end, term);
+   evas_object_smart_callback_add(o, "send,abort", _cb_send_abort, term);
    evas_object_show(o);
 
    wn->terms = eina_list_append(wn->terms, term);
diff --git a/tests/meson.build b/tests/meson.build
index 8c4c7228..2008ae2c 100644
--- a/tests/meson.build
+++ b/tests/meson.build
@@ -111,7 +111,7 @@ if tests
                 'tymux_live_blocks', 'tymux_live_commands',
                 'tymux_live_commands_full', 'tymux_live_commands_replay',
                 'tymux_live_commands_flood', 'tymux_live_attach_frame',
-                'tymux_live_attach_sync'],
+                'tymux_live_attach_sync', 'tymux_live_tysend'],
          depends: [tymuxd, tysend],
          timeout: 120)
     # Sends tens of MiB of links through a session: apart, for its time.

-- 
To stop receiving notification emails like this one, please contact
the administrator of this repository.

Reply via email to