0

I'm using an API (that I cannot change) with a function that accepts a directory file descriptor. This function creates some files inside the directory and fills them.

What I want to do is to send this directory over socket, and the current implementation is:

create local directory -> tar -> read the tar file -> send via socket -> untar

Is it possible to avoid to create locally the directory and tar, passing the bytes directly over socket? I cannot pass the socket as file descriptor because it expects a directory.

ocirocir
  • 3,543
  • 2
  • 24
  • 34
  • 1
    I assume you want to pass the socket descriptor to the API? Then no it's not possible. While the abstractions are basically the same (a small integer "descriptor") for file, directories and sockets (and more), the underlying structures are different and does different things. Different types of descriptors are simply not compatible in that way. – Some programmer dude Dec 11 '15 at 10:02
  • Mount a network share. Otherwise no, you can't. – n. m. could be an AI Dec 11 '15 at 10:09
  • You could probably use *libtar* to create and write the tar file on the fly over the socket. In particular, the `tar_fdopen` call would let you use the socket's file descriptor as the destination for the tar file being created on the fly. – Ian Abbott Dec 11 '15 at 10:34
  • A file descriptor is a file descriptor - whether it's for a directory or a file or a socket. It's quite possible to send any open file descriptor over a **Unix** (AF_UNIX/AF_LOCAL) socket. See http://stackoverflow.com/questions/28003921/sending-file-descriptor-by-linux-socket – Andrew Henle Dec 11 '15 at 12:36
  • @AndrewHenle but not with AF_INET right? – ocirocir Dec 11 '15 at 12:53
  • @FedericoReghenzani *but not with AF_INET right?* No. That wouldn't work. Instead of creating a tar file, just send it: `tar -cf - ... | ssh -e none ... tar -xf -` (see http://unix.stackexchange.com/questions/10026/how-can-i-best-copy-large-numbers-of-small-files-over-scp - note that the accepted answer does not protect against any escape sequence characters in the data stream, hence the `-e none` arguments.) – Andrew Henle Dec 11 '15 at 13:34

0 Answers0