fish.c 45 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506
  1. /* Virtual File System: FISH implementation for transfering files over
  2. shell connections.
  3. Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006,
  4. 2007 Free Software Foundation, Inc.
  5. Written by: 1998 Pavel Machek
  6. Spaces fix: 2000 Michal Svec
  7. 2010 Andrew Borodin
  8. 2010 Slava Zanko
  9. 2010 Ilia Maslakov
  10. Derived from ftpfs.c.
  11. This program is free software; you can redistribute it and/or
  12. modify it under the terms of the GNU Library General Public License
  13. as published by the Free Software Foundation; either version 2 of
  14. the License, or (at your option) any later version.
  15. This program is distributed in the hope that it will be useful,
  16. but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. GNU Library General Public License for more details.
  19. You should have received a copy of the GNU Library General Public
  20. License along with this program; if not, write to the Free Software
  21. Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
  22. /**
  23. * \file
  24. * \brief Source: Virtual File System: FISH implementation for transfering files over
  25. * shell connections
  26. * \author Pavel Machek
  27. * \author Michal Svec
  28. * \date 1998, 2000
  29. *
  30. * Derived from ftpfs.c
  31. * Read README.fish for protocol specification.
  32. *
  33. * Syntax of path is: \verbatim /#sh:user@host[:Cr]/path \endverbatim
  34. * where C means you want compressed connection,
  35. * and r means you want to use rsh
  36. *
  37. * Namespace: fish_vfs_ops exported.
  38. */
  39. /* Define this if your ssh can take -I option */
  40. #include <config.h>
  41. #include <errno.h>
  42. #include <fcntl.h>
  43. #include <pwd.h>
  44. #include <grp.h>
  45. #include <sys/time.h> /* gettimeofday() */
  46. #include <stdlib.h>
  47. #include <string.h>
  48. #include <inttypes.h> /* uintmax_t */
  49. #include "lib/global.h"
  50. #include "lib/tty/tty.h" /* enable/disable interrupt key */
  51. #include "lib/strescape.h"
  52. #include "lib/unixcompat.h"
  53. #include "lib/fileloc.h"
  54. #include "src/filemanager/layout.h" /* print_vfs_message */
  55. #include "src/execute.h" /* pre_exec, post_exec */
  56. #include "vfs-impl.h"
  57. #include "utilvfs.h"
  58. #include "netutil.h"
  59. #include "xdirentry.h"
  60. #include "gc.h" /* vfs_stamp_create */
  61. #include "fish.h"
  62. #include "fishdef.h"
  63. /*** global variables ****************************************************************************/
  64. int fish_directory_timeout = 900;
  65. /*** file scope macro definitions ****************************************************************/
  66. #define DO_RESOLVE_SYMLINK 1
  67. #define DO_OPEN 2
  68. #define DO_FREE_RESOURCE 4
  69. #define FISH_FLAG_COMPRESSED 1
  70. #define FISH_FLAG_RSH 2
  71. #define OPT_FLUSH 1
  72. #define OPT_IGNORE_ERROR 2
  73. /*
  74. * Reply codes.
  75. */
  76. #define PRELIM 1 /* positive preliminary */
  77. #define COMPLETE 2 /* positive completion */
  78. #define CONTINUE 3 /* positive intermediate */
  79. #define TRANSIENT 4 /* transient negative completion */
  80. #define ERROR 5 /* permanent negative completion */
  81. /* command wait_flag: */
  82. #define NONE 0x00
  83. #define WAIT_REPLY 0x01
  84. #define WANT_STRING 0x02
  85. /* environment flags */
  86. #define FISH_HAVE_HEAD 1
  87. #define FISH_HAVE_SED 2
  88. #define FISH_HAVE_AWK 4
  89. #define FISH_HAVE_PERL 8
  90. #define FISH_HAVE_LSQ 16
  91. #define FISH_HAVE_DATE_MDYT 32
  92. #define FISH_HAVE_TAIL 64
  93. #define SUP super->u.fish
  94. #define PREFIX \
  95. char buf[BUF_LARGE]; \
  96. const char *crpath; \
  97. char *rpath, *mpath; \
  98. struct vfs_s_super *super; \
  99. mpath = g_strdup (path); \
  100. crpath = vfs_s_get_path_mangle (me, mpath, &super, 0); \
  101. if (crpath == NULL) \
  102. { \
  103. g_free (mpath); \
  104. return -1; \
  105. } \
  106. rpath = strutils_shell_escape (crpath); \
  107. g_free (mpath)
  108. /*** file scope type declarations ****************************************************************/
  109. /*** file scope variables ************************************************************************/
  110. static char reply_str[80];
  111. static struct vfs_class vfs_fish_ops;
  112. /*** file scope functions ************************************************************************/
  113. /* --------------------------------------------------------------------------------------------- */
  114. static char *
  115. fish_load_script_from_file (const char *hostname, const char *script_name, const char *def_content)
  116. {
  117. char *scr_filename = NULL;
  118. char *scr_content;
  119. gsize scr_len = 0;
  120. /* 1st: scan user directory */
  121. scr_filename = g_build_path (PATH_SEP_STR, home_dir, MC_USERCONF_DIR, FISH_PREFIX, hostname,
  122. script_name, (char *) NULL);
  123. /* silent about user dir */
  124. g_file_get_contents (scr_filename, &scr_content, &scr_len, NULL);
  125. g_free (scr_filename);
  126. /* 2nd: scan system dir */
  127. if (scr_content == NULL)
  128. {
  129. scr_filename =
  130. g_build_path (PATH_SEP_STR, LIBEXECDIR, FISH_PREFIX, script_name, (char *) NULL);
  131. g_file_get_contents (scr_filename, &scr_content, &scr_len, NULL);
  132. g_free (scr_filename);
  133. }
  134. if (scr_content != NULL)
  135. return scr_content;
  136. return g_strdup (def_content);
  137. }
  138. /* --------------------------------------------------------------------------------------------- */
  139. static int
  140. fish_decode_reply (char *s, int was_garbage)
  141. {
  142. int code;
  143. if (!sscanf (s, "%d", &code))
  144. {
  145. code = 500;
  146. return 5;
  147. }
  148. if (code < 100)
  149. return was_garbage ? ERROR : (!code ? COMPLETE : PRELIM);
  150. return code / 100;
  151. }
  152. /* --------------------------------------------------------------------------------------------- */
  153. /* Returns a reply code, check /usr/include/arpa/ftp.h for possible values */
  154. static int
  155. fish_get_reply (struct vfs_class *me, int sock, char *string_buf, int string_len)
  156. {
  157. char answer[1024];
  158. int was_garbage = 0;
  159. for (;;)
  160. {
  161. if (!vfs_s_get_line (me, sock, answer, sizeof (answer), '\n'))
  162. {
  163. if (string_buf)
  164. *string_buf = 0;
  165. return 4;
  166. }
  167. if (strncmp (answer, "### ", 4))
  168. {
  169. was_garbage = 1;
  170. if (string_buf)
  171. g_strlcpy (string_buf, answer, string_len);
  172. }
  173. else
  174. return fish_decode_reply (answer + 4, was_garbage);
  175. }
  176. }
  177. /* --------------------------------------------------------------------------------------------- */
  178. static int
  179. fish_command (struct vfs_class *me, struct vfs_s_super *super, int wait_reply, const char *fmt, ...)
  180. {
  181. va_list ap;
  182. char *str;
  183. int status;
  184. FILE *logfile = MEDATA->logfile;
  185. va_start (ap, fmt);
  186. str = g_strdup_vprintf (fmt, ap);
  187. va_end (ap);
  188. if (logfile)
  189. {
  190. size_t ret;
  191. ret = fwrite (str, strlen (str), 1, logfile);
  192. ret = fflush (logfile);
  193. }
  194. tty_enable_interrupt_key ();
  195. status = write (SUP.sockw, str, strlen (str));
  196. g_free (str);
  197. tty_disable_interrupt_key ();
  198. if (status < 0)
  199. return TRANSIENT;
  200. if (wait_reply)
  201. return fish_get_reply (me, SUP.sockr,
  202. (wait_reply & WANT_STRING) ? reply_str :
  203. NULL, sizeof (reply_str) - 1);
  204. return COMPLETE;
  205. }
  206. /* --------------------------------------------------------------------------------------------- */
  207. static void
  208. fish_free_archive (struct vfs_class *me, struct vfs_s_super *super)
  209. {
  210. if ((SUP.sockw != -1) || (SUP.sockr != -1))
  211. {
  212. print_vfs_message (_("fish: Disconnecting from %s"), super->name ? super->name : "???");
  213. fish_command (me, super, NONE, "#BYE\nexit\n");
  214. close (SUP.sockw);
  215. close (SUP.sockr);
  216. SUP.sockw = SUP.sockr = -1;
  217. }
  218. g_free (SUP.host);
  219. g_free (SUP.user);
  220. g_free (SUP.cwdir);
  221. g_free (SUP.password);
  222. g_free (SUP.scr_ls);
  223. g_free (SUP.scr_exists);
  224. g_free (SUP.scr_mkdir);
  225. g_free (SUP.scr_unlink);
  226. g_free (SUP.scr_chown);
  227. g_free (SUP.scr_chmod);
  228. g_free (SUP.scr_utime);
  229. g_free (SUP.scr_rmdir);
  230. g_free (SUP.scr_ln);
  231. g_free (SUP.scr_mv);
  232. g_free (SUP.scr_hardlink);
  233. g_free (SUP.scr_get);
  234. g_free (SUP.scr_send);
  235. g_free (SUP.scr_append);
  236. g_free (SUP.scr_info);
  237. g_free (SUP.scr_env);
  238. }
  239. /* --------------------------------------------------------------------------------------------- */
  240. static void
  241. fish_pipeopen (struct vfs_s_super *super, const char *path, const char *argv[])
  242. {
  243. int fileset1[2], fileset2[2];
  244. int res;
  245. if ((pipe (fileset1) < 0) || (pipe (fileset2) < 0))
  246. vfs_die ("Cannot pipe(): %m.");
  247. res = fork ();
  248. if (res != 0)
  249. {
  250. if (res < 0)
  251. vfs_die ("Cannot fork(): %m.");
  252. /* We are the parent */
  253. close (fileset1[0]);
  254. SUP.sockw = fileset1[1];
  255. close (fileset2[1]);
  256. SUP.sockr = fileset2[0];
  257. }
  258. else
  259. {
  260. res = dup2 (fileset1[0], 0);
  261. close (fileset1[0]);
  262. close (fileset1[1]);
  263. res = dup2 (fileset2[1], 1);
  264. close (2);
  265. /* stderr to /dev/null */
  266. res = open ("/dev/null", O_WRONLY);
  267. close (fileset2[0]);
  268. close (fileset2[1]);
  269. execvp (path, const_cast (char **, argv));
  270. _exit (3);
  271. }
  272. }
  273. /* --------------------------------------------------------------------------------------------- */
  274. static char *
  275. fish_set_env (int flags)
  276. {
  277. GString *tmp;
  278. tmp = g_string_sized_new (250);
  279. g_string_assign (tmp, "");
  280. if ((flags & FISH_HAVE_HEAD) != 0)
  281. g_string_append (tmp, "FISH_HAVE_HEAD=1 export FISH_HAVE_HEAD; ");
  282. if ((flags & FISH_HAVE_SED) != 0)
  283. g_string_append (tmp, "FISH_HAVE_SED=1 export FISH_HAVE_SED; ");
  284. if ((flags & FISH_HAVE_AWK) != 0)
  285. g_string_append (tmp, "FISH_HAVE_AWK=1 export FISH_HAVE_AWK; ");
  286. if ((flags & FISH_HAVE_PERL) != 0)
  287. g_string_append (tmp, "FISH_HAVE_PERL=1 export FISH_HAVE_PERL; ");
  288. if ((flags & FISH_HAVE_LSQ) != 0)
  289. g_string_append (tmp, "FISH_HAVE_LSQ=1 export FISH_HAVE_LSQ; ");
  290. if ((flags & FISH_HAVE_DATE_MDYT) != 0)
  291. g_string_append (tmp, "FISH_HAVE_DATE_MDYT=1 export FISH_HAVE_DATE_MDYT; ");
  292. if ((flags & FISH_HAVE_TAIL) != 0)
  293. g_string_append (tmp, "FISH_HAVE_TAIL=1 export FISH_HAVE_TAIL; ");
  294. return g_string_free (tmp, FALSE);
  295. }
  296. /* --------------------------------------------------------------------------------------------- */
  297. static gboolean
  298. fish_info (struct vfs_class *me, struct vfs_s_super *super)
  299. {
  300. char buffer[8192];
  301. if (fish_command (me, super, NONE, SUP.scr_info) == COMPLETE)
  302. {
  303. while (1)
  304. {
  305. int res;
  306. res = vfs_s_get_line_interruptible (me, buffer, sizeof (buffer), SUP.sockr);
  307. if ((!res) || (res == EINTR))
  308. ERRNOR (ECONNRESET, FALSE);
  309. if (!strncmp (buffer, "### ", 4))
  310. break;
  311. SUP.host_flags = atol (buffer);
  312. }
  313. return TRUE;
  314. }
  315. ERRNOR (E_PROTO, FALSE);
  316. }
  317. /* --------------------------------------------------------------------------------------------- */
  318. /* The returned directory should always contain a trailing slash */
  319. static char *
  320. fish_getcwd (struct vfs_class *me, struct vfs_s_super *super)
  321. {
  322. if (fish_command (me, super, WANT_STRING, "#PWD\npwd; echo '### 200'\n") == COMPLETE)
  323. return g_strconcat (reply_str, "/", (char *) NULL);
  324. ERRNOR (EIO, NULL);
  325. }
  326. /* --------------------------------------------------------------------------------------------- */
  327. static void
  328. fish_open_archive_pipeopen (struct vfs_s_super *super)
  329. {
  330. char gbuf[10];
  331. const char *argv[10]; /* All of 10 is used now */
  332. const char *xsh = (SUP.flags == FISH_FLAG_RSH ? "rsh" : "ssh");
  333. int i = 0;
  334. argv[i++] = xsh;
  335. if (SUP.flags == FISH_FLAG_COMPRESSED)
  336. argv[i++] = "-C";
  337. if (SUP.flags > FISH_FLAG_RSH)
  338. {
  339. argv[i++] = "-p";
  340. g_snprintf (gbuf, sizeof (gbuf), "%d", SUP.flags);
  341. argv[i++] = gbuf;
  342. }
  343. /*
  344. * Add the user name to the ssh command line only if it was explicitly
  345. * set in vfs URL. rsh/ssh will get current user by default
  346. * plus we can set convenient overrides in ~/.ssh/config (explicit -l
  347. * option breaks it for some)
  348. */
  349. if (SUP.user)
  350. {
  351. argv[i++] = "-l";
  352. argv[i++] = SUP.user;
  353. }
  354. else
  355. {
  356. /* The rest of the code assumes it to be a valid username */
  357. SUP.user = vfs_get_local_username ();
  358. }
  359. argv[i++] = SUP.host;
  360. argv[i++] = "echo FISH:; /bin/sh";
  361. argv[i++] = NULL;
  362. fish_pipeopen (super, xsh, argv);
  363. }
  364. /* --------------------------------------------------------------------------------------------- */
  365. static gboolean
  366. fish_open_archive_talk (struct vfs_class *me, struct vfs_s_super *super)
  367. {
  368. char answer[2048];
  369. printf ("\n%s\n", _("fish: Waiting for initial line..."));
  370. if (!vfs_s_get_line (me, SUP.sockr, answer, sizeof (answer), ':'))
  371. return FALSE;
  372. if (strstr (answer, "assword"))
  373. {
  374. /* Currently, this does not work. ssh reads passwords from
  375. /dev/tty, not from stdin :-(. */
  376. printf ("\n%s\n", _("Sorry, we cannot do password authenticated connections for now."));
  377. return FALSE;
  378. #if 0
  379. if (!SUP.password)
  380. {
  381. char *p, *op;
  382. p = g_strdup_printf (_("fish: Password is required for %s"), SUP.user);
  383. op = vfs_get_password (p);
  384. g_free (p);
  385. if (op == NULL)
  386. return FALSE;
  387. SUP.password = op;
  388. }
  389. printf ("\n%s\n", _("fish: Sending password..."));
  390. {
  391. size_t str_len;
  392. str_len = strlen (SUP.password);
  393. if ((write (SUP.sockw, SUP.password, str_len) != (ssize_t) str_len)
  394. || (write (SUP.sockw, "\n", 1) != 1))
  395. {
  396. return FALSE;
  397. }
  398. }
  399. #endif
  400. }
  401. return TRUE;
  402. }
  403. /* --------------------------------------------------------------------------------------------- */
  404. static int
  405. fish_open_archive_int (struct vfs_class *me, struct vfs_s_super *super)
  406. {
  407. gboolean ftalk;
  408. /* hide panels */
  409. pre_exec ();
  410. /* open pipe */
  411. fish_open_archive_pipeopen (super);
  412. /* Start talk with ssh-server (password prompt, etc ) */
  413. ftalk = fish_open_archive_talk (me, super);
  414. /* show panels */
  415. post_exec ();
  416. if (!ftalk)
  417. ERRNOR (E_PROTO, -1);
  418. print_vfs_message (_("fish: Sending initial line..."));
  419. /*
  420. * Run `start_fish_server'. If it doesn't exist - no problem,
  421. * we'll talk directly to the shell.
  422. */
  423. if (fish_command
  424. (me, super, WAIT_REPLY,
  425. "#FISH\necho; start_fish_server 2>&1; echo '### 200'\n") != COMPLETE)
  426. ERRNOR (E_PROTO, -1);
  427. print_vfs_message (_("fish: Handshaking version..."));
  428. if (fish_command (me, super, WAIT_REPLY, "#VER 0.0.3\necho '### 000'\n") != COMPLETE)
  429. ERRNOR (E_PROTO, -1);
  430. /* Set up remote locale to C, otherwise dates cannot be recognized */
  431. if (fish_command
  432. (me, super, WAIT_REPLY,
  433. "LANG=C LC_ALL=C LC_TIME=C; export LANG LC_ALL LC_TIME;\n" "echo '### 200'\n") != COMPLETE)
  434. ERRNOR (E_PROTO, -1);
  435. print_vfs_message (_("fish: Getting host info..."));
  436. if (fish_info (me, super))
  437. SUP.scr_env = fish_set_env (SUP.host_flags);
  438. print_vfs_message (_("fish: Setting up current directory..."));
  439. SUP.cwdir = fish_getcwd (me, super);
  440. print_vfs_message (_("fish: Connected, home %s."), SUP.cwdir);
  441. #if 0
  442. super->name = g_strconcat ("/#sh:", SUP.user, "@", SUP.host, "/", (char *) NULL);
  443. #endif
  444. super->name = g_strdup (PATH_SEP_STR);
  445. super->root = vfs_s_new_inode (me, super, vfs_s_default_stat (me, S_IFDIR | 0755));
  446. return 0;
  447. }
  448. /* --------------------------------------------------------------------------------------------- */
  449. static int
  450. fish_open_archive (struct vfs_class *me, struct vfs_s_super *super,
  451. const char *archive_name, char *op)
  452. {
  453. char *host, *user, *password, *p;
  454. int flags;
  455. (void) archive_name;
  456. p = vfs_split_url (strchr (op, ':') + 1, &host, &user, &flags,
  457. &password, 0, URL_NOSLASH | URL_USE_ANONYMOUS);
  458. g_free (p);
  459. SUP.host = host;
  460. SUP.user = user;
  461. SUP.flags = flags;
  462. if (!strncmp (op, "rsh:", 4))
  463. SUP.flags = FISH_FLAG_RSH;
  464. SUP.cwdir = NULL;
  465. if (password)
  466. SUP.password = password;
  467. SUP.scr_ls = fish_load_script_from_file (host, FISH_LS_FILE, FISH_LS_DEF_CONTENT);
  468. SUP.scr_exists = fish_load_script_from_file (host, FISH_EXISTS_FILE, FISH_EXISTS_DEF_CONTENT);
  469. SUP.scr_mkdir = fish_load_script_from_file (host, FISH_MKDIR_FILE, FISH_MKDIR_DEF_CONTENT);
  470. SUP.scr_unlink = fish_load_script_from_file (host, FISH_UNLINK_FILE, FISH_UNLINK_DEF_CONTENT);
  471. SUP.scr_chown = fish_load_script_from_file (host, FISH_CHOWN_FILE, FISH_CHOWN_DEF_CONTENT);
  472. SUP.scr_chmod = fish_load_script_from_file (host, FISH_CHMOD_FILE, FISH_CHMOD_DEF_CONTENT);
  473. SUP.scr_utime = fish_load_script_from_file (host, FISH_UTIME_FILE, FISH_UTIME_DEF_CONTENT);
  474. SUP.scr_rmdir = fish_load_script_from_file (host, FISH_RMDIR_FILE, FISH_RMDIR_DEF_CONTENT);
  475. SUP.scr_ln = fish_load_script_from_file (host, FISH_LN_FILE, FISH_LN_DEF_CONTENT);
  476. SUP.scr_mv = fish_load_script_from_file (host, FISH_MV_FILE, FISH_MV_DEF_CONTENT);
  477. SUP.scr_hardlink =
  478. fish_load_script_from_file (host, FISH_HARDLINK_FILE, FISH_HARDLINK_DEF_CONTENT);
  479. SUP.scr_get = fish_load_script_from_file (host, FISH_GET_FILE, FISH_GET_DEF_CONTENT);
  480. SUP.scr_send = fish_load_script_from_file (host, FISH_SEND_FILE, FISH_SEND_DEF_CONTENT);
  481. SUP.scr_append = fish_load_script_from_file (host, FISH_APPEND_FILE, FISH_APPEND_DEF_CONTENT);
  482. SUP.scr_info = fish_load_script_from_file (host, FISH_INFO_FILE, FISH_INFO_DEF_CONTENT);
  483. return fish_open_archive_int (me, super);
  484. }
  485. /* --------------------------------------------------------------------------------------------- */
  486. static int
  487. fish_archive_same (struct vfs_class *me, struct vfs_s_super *super,
  488. const char *archive_name, char *op, void *cookie)
  489. {
  490. char *host, *user;
  491. int flags;
  492. int result;
  493. (void) me;
  494. (void) archive_name;
  495. (void) cookie;
  496. op = vfs_split_url (strchr (op, ':') + 1, &host, &user, &flags, 0, 0,
  497. URL_NOSLASH | URL_USE_ANONYMOUS);
  498. g_free (op);
  499. if (user == NULL)
  500. user = vfs_get_local_username ();
  501. result = ((strcmp (host, SUP.host) == 0)
  502. && (strcmp (user, SUP.user) == 0) && (flags == SUP.flags));
  503. g_free (host);
  504. g_free (user);
  505. return result;
  506. }
  507. /* --------------------------------------------------------------------------------------------- */
  508. static int
  509. fish_dir_load (struct vfs_class *me, struct vfs_s_inode *dir, char *remote_path)
  510. {
  511. struct vfs_s_super *super = dir->super;
  512. char buffer[8192];
  513. struct vfs_s_entry *ent = NULL;
  514. FILE *logfile;
  515. char *quoted_path;
  516. int reply_code;
  517. gchar *shell_commands;
  518. /*
  519. * Simple FISH debug interface :]
  520. */
  521. #if 0
  522. if (!(MEDATA->logfile))
  523. {
  524. MEDATA->logfile = fopen ("/tmp/mc-FISH.sh", "w");
  525. }
  526. #endif
  527. logfile = MEDATA->logfile;
  528. print_vfs_message (_("fish: Reading directory %s..."), remote_path);
  529. gettimeofday (&dir->timestamp, NULL);
  530. dir->timestamp.tv_sec += fish_directory_timeout;
  531. quoted_path = strutils_shell_escape (remote_path);
  532. shell_commands = g_strconcat (SUP.scr_env, "FISH_FILENAME=%s;\n", SUP.scr_ls, (char *) NULL);
  533. fish_command (me, super, NONE, shell_commands, quoted_path);
  534. g_free (shell_commands);
  535. g_free (quoted_path);
  536. ent = vfs_s_generate_entry (me, NULL, dir, 0);
  537. while (1)
  538. {
  539. int res = vfs_s_get_line_interruptible (me, buffer, sizeof (buffer), SUP.sockr);
  540. if ((!res) || (res == EINTR))
  541. {
  542. vfs_s_free_entry (me, ent);
  543. me->verrno = ECONNRESET;
  544. goto error;
  545. }
  546. if (logfile)
  547. {
  548. fputs (buffer, logfile);
  549. fputs ("\n", logfile);
  550. fflush (logfile);
  551. }
  552. if (!strncmp (buffer, "### ", 4))
  553. break;
  554. if ((!buffer[0]))
  555. {
  556. if (ent->name)
  557. {
  558. vfs_s_insert_entry (me, dir, ent);
  559. ent = vfs_s_generate_entry (me, NULL, dir, 0);
  560. }
  561. continue;
  562. }
  563. #define ST ent->ino->st
  564. switch (buffer[0])
  565. {
  566. case ':':
  567. {
  568. char *temp;
  569. char *data_start = buffer + 1;
  570. char *filename = data_start;
  571. char *linkname = data_start;
  572. char *filename_bound = filename + strlen (filename);
  573. char *linkname_bound = filename_bound;
  574. if (!strcmp (data_start, "\".\"") || !strcmp (data_start, "\"..\""))
  575. break; /* We'll do "." and ".." ourselves */
  576. if (S_ISLNK (ST.st_mode))
  577. {
  578. /* we expect: "escaped-name" -> "escaped-name"
  579. // -> cannot occur in filenames,
  580. // because it will be escaped to -\> */
  581. if (*filename == '"')
  582. ++filename;
  583. linkname = strstr (filename, "\" -> \"");
  584. if (!linkname)
  585. {
  586. /* broken client, or smth goes wrong */
  587. linkname = filename_bound;
  588. if (filename_bound > filename && *(filename_bound - 1) == '"')
  589. --filename_bound; /* skip trailing " */
  590. }
  591. else
  592. {
  593. filename_bound = linkname;
  594. linkname += 6; /* strlen ("\" -> \"") */
  595. if (*(linkname_bound - 1) == '"')
  596. --linkname_bound; /* skip trailing " */
  597. }
  598. ent->name = g_strndup (filename, filename_bound - filename);
  599. temp = ent->name;
  600. ent->name = strutils_shell_unescape (ent->name);
  601. g_free (temp);
  602. ent->ino->linkname = g_strndup (linkname, linkname_bound - linkname);
  603. temp = ent->ino->linkname;
  604. ent->ino->linkname = strutils_shell_unescape (ent->ino->linkname);
  605. g_free (temp);
  606. }
  607. else
  608. {
  609. /* we expect: "escaped-name" */
  610. if (filename_bound - filename > 2)
  611. {
  612. /*
  613. there is at least 2 "
  614. and we skip them
  615. */
  616. if (*filename == '"')
  617. ++filename;
  618. if (*(filename_bound - 1) == '"')
  619. --filename_bound;
  620. }
  621. ent->name = g_strndup (filename, filename_bound - filename);
  622. temp = ent->name;
  623. ent->name = strutils_shell_unescape (ent->name);
  624. g_free (temp);
  625. }
  626. break;
  627. }
  628. case 'S':
  629. #ifdef HAVE_ATOLL
  630. ST.st_size = (off_t) atoll (buffer + 1);
  631. #else
  632. ST.st_size = (off_t) atof (buffer + 1);
  633. #endif
  634. break;
  635. case 'P':
  636. {
  637. size_t skipped;
  638. vfs_parse_filemode (buffer + 1, &skipped, &ST.st_mode);
  639. break;
  640. }
  641. case 'R':
  642. {
  643. /*
  644. raw filemode:
  645. we expect: Roctal-filemode octal-filetype uid.gid
  646. */
  647. size_t skipped;
  648. vfs_parse_raw_filemode (buffer + 1, &skipped, &ST.st_mode);
  649. break;
  650. }
  651. case 'd':
  652. {
  653. vfs_split_text (buffer + 1);
  654. if (!vfs_parse_filedate (0, &ST.st_ctime))
  655. break;
  656. ST.st_atime = ST.st_mtime = ST.st_ctime;
  657. }
  658. break;
  659. case 'D':
  660. {
  661. struct tm tim;
  662. if (sscanf (buffer + 1, "%d %d %d %d %d %d", &tim.tm_year, &tim.tm_mon,
  663. &tim.tm_mday, &tim.tm_hour, &tim.tm_min, &tim.tm_sec) != 6)
  664. break;
  665. ST.st_atime = ST.st_mtime = ST.st_ctime = mktime (&tim);
  666. }
  667. break;
  668. case 'E':
  669. {
  670. int maj, min;
  671. if (sscanf (buffer + 1, "%d,%d", &maj, &min) != 2)
  672. break;
  673. #ifdef HAVE_STRUCT_STAT_ST_RDEV
  674. ST.st_rdev = makedev (maj, min);
  675. #endif
  676. }
  677. }
  678. }
  679. vfs_s_free_entry (me, ent);
  680. reply_code = fish_decode_reply (buffer + 4, 0);
  681. if (reply_code == COMPLETE)
  682. {
  683. g_free (SUP.cwdir);
  684. SUP.cwdir = g_strdup (remote_path);
  685. print_vfs_message (_("%s: done."), me->name);
  686. return 0;
  687. }
  688. else if (reply_code == ERROR)
  689. {
  690. me->verrno = EACCES;
  691. }
  692. else
  693. {
  694. me->verrno = E_REMOTE;
  695. }
  696. error:
  697. print_vfs_message (_("%s: failure"), me->name);
  698. return -1;
  699. }
  700. /* --------------------------------------------------------------------------------------------- */
  701. static int
  702. fish_file_store (struct vfs_class *me, struct vfs_s_fh *fh, char *name, char *localname)
  703. {
  704. gchar *shell_commands = NULL;
  705. struct vfs_s_super *super = FH_SUPER;
  706. int n, total;
  707. char buffer[8192];
  708. struct stat s;
  709. int was_error = 0;
  710. int h;
  711. char *quoted_name;
  712. h = open (localname, O_RDONLY);
  713. if (h == -1)
  714. ERRNOR (EIO, -1);
  715. if (fstat (h, &s) < 0)
  716. {
  717. close (h);
  718. ERRNOR (EIO, -1);
  719. }
  720. /* First, try this as stor:
  721. *
  722. * ( head -c number ) | ( cat > file; cat >/dev/null )
  723. *
  724. * If `head' is not present on the remote system, `dd' will be used.
  725. * Unfortunately, we cannot trust most non-GNU `head' implementations
  726. * even if `-c' options is supported. Therefore, we separate GNU head
  727. * (and other modern heads?) using `-q' and `-' . This causes another
  728. * implementations to fail (because of "incorrect options").
  729. *
  730. * Fallback is:
  731. *
  732. * rest=<number>
  733. * while [ $rest -gt 0 ]
  734. * do
  735. * cnt=`expr \( $rest + 255 \) / 256`
  736. * n=`dd bs=256 count=$cnt | tee -a <target_file> | wc -c`
  737. * rest=`expr $rest - $n`
  738. * done
  739. *
  740. * `dd' was not designed for full filling of input buffers,
  741. * and does not report exact number of bytes (not blocks).
  742. * Therefore a more complex shell script is needed.
  743. *
  744. * On some systems non-GNU head writes "Usage:" error report to stdout
  745. * instead of stderr. It makes impossible the use of "head || dd"
  746. * algorithm for file appending case, therefore just "dd" is used for it.
  747. */
  748. quoted_name = strutils_shell_escape (name);
  749. print_vfs_message (_("fish: store %s: sending command..."), quoted_name);
  750. /* FIXME: File size is limited to ULONG_MAX */
  751. if (!fh->u.fish.append)
  752. {
  753. shell_commands = g_strconcat (SUP.scr_env, "FISH_FILENAME=%s FISH_FILESIZE=%" PRIuMAX ";\n",
  754. SUP.scr_append, (char *) NULL);
  755. n = fish_command (me, super, WAIT_REPLY, shell_commands, quoted_name,
  756. (uintmax_t) s.st_size);
  757. g_free (shell_commands);
  758. }
  759. else
  760. {
  761. shell_commands = g_strconcat (SUP.scr_env, "FISH_FILENAME=%s FISH_FILESIZE=%" PRIuMAX ";\n",
  762. SUP.scr_send, (char *) NULL);
  763. n = fish_command (me, super, WAIT_REPLY, shell_commands, quoted_name,
  764. (uintmax_t) s.st_size);
  765. g_free (shell_commands);
  766. }
  767. if (n != PRELIM)
  768. {
  769. close (h);
  770. ERRNOR (E_REMOTE, -1);
  771. }
  772. total = 0;
  773. while (1)
  774. {
  775. int t;
  776. while ((n = read (h, buffer, sizeof (buffer))) < 0)
  777. {
  778. if ((errno == EINTR) && tty_got_interrupt ())
  779. continue;
  780. print_vfs_message (_("fish: Local read failed, sending zeros"));
  781. close (h);
  782. h = open ("/dev/zero", O_RDONLY);
  783. }
  784. if (n == 0)
  785. break;
  786. t = write (SUP.sockw, buffer, n);
  787. if (t != n)
  788. {
  789. if (t == -1)
  790. me->verrno = errno;
  791. else
  792. me->verrno = EIO;
  793. goto error_return;
  794. }
  795. tty_disable_interrupt_key ();
  796. total += n;
  797. print_vfs_message ("%s: %d/%" PRIuMAX,
  798. was_error ? _("fish: storing zeros") : _("fish: storing file"),
  799. total, (uintmax_t) s.st_size);
  800. }
  801. close (h);
  802. g_free (quoted_name);
  803. if ((fish_get_reply (me, SUP.sockr, NULL, 0) != COMPLETE) || was_error)
  804. ERRNOR (E_REMOTE, -1);
  805. return 0;
  806. error_return:
  807. close (h);
  808. fish_get_reply (me, SUP.sockr, NULL, 0);
  809. g_free (quoted_name);
  810. return -1;
  811. }
  812. /* --------------------------------------------------------------------------------------------- */
  813. static int
  814. fish_linear_start (struct vfs_class *me, struct vfs_s_fh *fh, off_t offset)
  815. {
  816. gchar *shell_commands = NULL;
  817. struct vfs_s_super *super = FH_SUPER;
  818. char *name;
  819. char *quoted_name;
  820. name = vfs_s_fullpath (me, fh->ino);
  821. if (name == NULL)
  822. return 0;
  823. quoted_name = strutils_shell_escape (name);
  824. g_free (name);
  825. fh->u.fish.append = 0;
  826. /*
  827. * Check whether the remote file is readable by using `dd' to copy
  828. * a single byte from the remote file to /dev/null. If `dd' completes
  829. * with exit status of 0 use `cat' to send the file contents to the
  830. * standard output (i.e. over the network).
  831. */
  832. shell_commands = g_strconcat (SUP.scr_env, "FISH_FILENAME=%s FISH_START_OFFSET=%" PRIuMAX ";\n",
  833. SUP.scr_get, (char *) NULL);
  834. offset = fish_command (me, super, WANT_STRING, shell_commands, quoted_name, (uintmax_t) offset);
  835. g_free (shell_commands);
  836. g_free (quoted_name);
  837. if (offset != PRELIM)
  838. ERRNOR (E_REMOTE, 0);
  839. fh->linear = LS_LINEAR_OPEN;
  840. fh->u.fish.got = 0;
  841. errno = 0;
  842. #if SIZEOF_OFF_T == SIZEOF_LONG
  843. fh->u.fish.total = (off_t) strtol (reply_str, NULL, 10);
  844. #else
  845. fh->u.fish.total = (off_t) strtoll (reply_str, NULL, 10);
  846. #endif
  847. if (errno != 0)
  848. ERRNOR (E_REMOTE, 0);
  849. return 1;
  850. }
  851. /* --------------------------------------------------------------------------------------------- */
  852. static void
  853. fish_linear_abort (struct vfs_class *me, struct vfs_s_fh *fh)
  854. {
  855. struct vfs_s_super *super = FH_SUPER;
  856. char buffer[8192];
  857. int n;
  858. print_vfs_message (_("Aborting transfer..."));
  859. do
  860. {
  861. n = MIN (8192, fh->u.fish.total - fh->u.fish.got);
  862. if (n != 0)
  863. {
  864. n = read (SUP.sockr, buffer, n);
  865. if (n < 0)
  866. return;
  867. fh->u.fish.got += n;
  868. }
  869. }
  870. while (n != 0);
  871. if (fish_get_reply (me, SUP.sockr, NULL, 0) != COMPLETE)
  872. print_vfs_message (_("Error reported after abort."));
  873. else
  874. print_vfs_message (_("Aborted transfer would be successful."));
  875. }
  876. /* --------------------------------------------------------------------------------------------- */
  877. static int
  878. fish_linear_read (struct vfs_class *me, struct vfs_s_fh *fh, void *buf, size_t len)
  879. {
  880. struct vfs_s_super *super = FH_SUPER;
  881. ssize_t n = 0;
  882. len = MIN ((size_t) (fh->u.fish.total - fh->u.fish.got), len);
  883. tty_disable_interrupt_key ();
  884. while (len != 0 && ((n = read (SUP.sockr, buf, len)) < 0))
  885. {
  886. if ((errno == EINTR) && !tty_got_interrupt ())
  887. continue;
  888. break;
  889. }
  890. tty_enable_interrupt_key ();
  891. if (n > 0)
  892. fh->u.fish.got += n;
  893. else if (n < 0)
  894. fish_linear_abort (me, fh);
  895. else if (fish_get_reply (me, SUP.sockr, NULL, 0) != COMPLETE)
  896. ERRNOR (E_REMOTE, -1);
  897. ERRNOR (errno, n);
  898. }
  899. /* --------------------------------------------------------------------------------------------- */
  900. static void
  901. fish_linear_close (struct vfs_class *me, struct vfs_s_fh *fh)
  902. {
  903. if (fh->u.fish.total != fh->u.fish.got)
  904. fish_linear_abort (me, fh);
  905. }
  906. /* --------------------------------------------------------------------------------------------- */
  907. static int
  908. fish_ctl (void *fh, int ctlop, void *arg)
  909. {
  910. (void) arg;
  911. (void) fh;
  912. (void) ctlop;
  913. return 0;
  914. #if 0
  915. switch (ctlop)
  916. {
  917. case VFS_CTL_IS_NOTREADY:
  918. {
  919. int v;
  920. if (!FH->linear)
  921. vfs_die ("You may not do this");
  922. if (FH->linear == LS_LINEAR_CLOSED || FH->linear == LS_LINEAR_PREOPEN)
  923. return 0;
  924. v = vfs_s_select_on_two (FH_SUPER->u.fish.sockr, 0);
  925. if (((v < 0) && (errno == EINTR)) || v == 0)
  926. return 1;
  927. return 0;
  928. }
  929. default:
  930. return 0;
  931. }
  932. #endif
  933. }
  934. /* --------------------------------------------------------------------------------------------- */
  935. static int
  936. fish_send_command (struct vfs_class *me, struct vfs_s_super *super, const char *cmd, int flags)
  937. {
  938. int r;
  939. r = fish_command (me, super, WAIT_REPLY, "%s", cmd);
  940. vfs_stamp_create (&vfs_fish_ops, super);
  941. if (r != COMPLETE)
  942. ERRNOR (E_REMOTE, -1);
  943. if (flags & OPT_FLUSH)
  944. vfs_s_invalidate (me, super);
  945. return 0;
  946. }
  947. /* --------------------------------------------------------------------------------------------- */
  948. static int
  949. fish_rename (struct vfs_class *me, const char *path1, const char *path2)
  950. {
  951. gchar *shell_commands = NULL;
  952. char buf[BUF_LARGE];
  953. const char *crpath1, *crpath2;
  954. char *rpath1, *rpath2, *mpath1, *mpath2;
  955. struct vfs_s_super *super, *super2;
  956. mpath1 = g_strdup (path1);
  957. crpath1 = vfs_s_get_path_mangle (me, mpath1, &super, 0);
  958. if (crpath1 == NULL)
  959. {
  960. g_free (mpath1);
  961. return -1;
  962. }
  963. mpath2 = g_strdup (path2);
  964. crpath2 = vfs_s_get_path_mangle (me, mpath2, &super2, 0);
  965. if (crpath2 == NULL)
  966. {
  967. g_free (mpath1);
  968. g_free (mpath2);
  969. return -1;
  970. }
  971. rpath1 = strutils_shell_escape (crpath1);
  972. g_free (mpath1);
  973. rpath2 = strutils_shell_escape (crpath2);
  974. g_free (mpath2);
  975. shell_commands = g_strconcat (SUP.scr_env, "FISH_FILEFROM=%s FISH_FILETO=%s;\n",
  976. SUP.scr_mv, (char *) NULL);
  977. g_snprintf (buf, sizeof (buf), shell_commands, rpath1, rpath2);
  978. g_free (shell_commands);
  979. g_free (rpath1);
  980. g_free (rpath2);
  981. return fish_send_command (me, super2, buf, OPT_FLUSH);
  982. }
  983. /* --------------------------------------------------------------------------------------------- */
  984. static int
  985. fish_link (struct vfs_class *me, const char *path1, const char *path2)
  986. {
  987. gchar *shell_commands = NULL;
  988. char buf[BUF_LARGE];
  989. const char *crpath1, *crpath2;
  990. char *rpath1, *rpath2, *mpath1, *mpath2;
  991. struct vfs_s_super *super, *super2;
  992. mpath1 = g_strdup (path1);
  993. crpath1 = vfs_s_get_path_mangle (me, mpath1, &super, 0);
  994. if (crpath1 == NULL)
  995. {
  996. g_free (mpath1);
  997. return -1;
  998. }
  999. mpath2 = g_strdup (path2);
  1000. crpath2 = vfs_s_get_path_mangle (me, mpath2, &super2, 0);
  1001. if (crpath2 == NULL)
  1002. {
  1003. g_free (mpath1);
  1004. g_free (mpath2);
  1005. return -1;
  1006. }
  1007. rpath1 = strutils_shell_escape (crpath1);
  1008. g_free (mpath1);
  1009. rpath2 = strutils_shell_escape (crpath2);
  1010. g_free (mpath2);
  1011. shell_commands = g_strconcat (SUP.scr_env, "FISH_FILEFROM=%s FISH_FILETO=%s;\n",
  1012. SUP.scr_hardlink, (char *) NULL);
  1013. g_snprintf (buf, sizeof (buf), shell_commands, rpath1, rpath2);
  1014. g_free (shell_commands);
  1015. g_free (rpath1);
  1016. g_free (rpath2);
  1017. return fish_send_command (me, super2, buf, OPT_FLUSH);
  1018. }
  1019. /* --------------------------------------------------------------------------------------------- */
  1020. static int
  1021. fish_symlink (struct vfs_class *me, const char *setto, const char *path)
  1022. {
  1023. char *qsetto;
  1024. gchar *shell_commands = NULL;
  1025. char buf[BUF_LARGE];
  1026. const char *crpath;
  1027. char *rpath, *mpath;
  1028. struct vfs_s_super *super;
  1029. mpath = g_strdup (path);
  1030. crpath = vfs_s_get_path_mangle (me, mpath, &super, 0);
  1031. if (crpath == NULL)
  1032. {
  1033. g_free (mpath);
  1034. return -1;
  1035. }
  1036. rpath = strutils_shell_escape (crpath);
  1037. g_free (mpath);
  1038. qsetto = strutils_shell_escape (setto);
  1039. shell_commands = g_strconcat (SUP.scr_env, "FISH_FILEFROM=%s FISH_FILETO=%s;\n",
  1040. SUP.scr_ln, (char *) NULL);
  1041. g_snprintf (buf, sizeof (buf), shell_commands, qsetto, rpath);
  1042. g_free (shell_commands);
  1043. g_free (qsetto);
  1044. g_free (rpath);
  1045. return fish_send_command (me, super, buf, OPT_FLUSH);
  1046. }
  1047. /* --------------------------------------------------------------------------------------------- */
  1048. static int
  1049. fish_chmod (struct vfs_class *me, const char *path, int mode)
  1050. {
  1051. gchar *shell_commands = NULL;
  1052. PREFIX;
  1053. shell_commands = g_strconcat (SUP.scr_env, "FISH_FILENAME=%s FISH_FILEMODE=%4.4o;\n",
  1054. SUP.scr_chmod, (char *) NULL);
  1055. g_snprintf (buf, sizeof (buf), shell_commands, rpath, mode & 07777);
  1056. g_free (shell_commands);
  1057. g_free (rpath);
  1058. return fish_send_command (me, super, buf, OPT_FLUSH);
  1059. }
  1060. /* --------------------------------------------------------------------------------------------- */
  1061. static int
  1062. fish_chown (struct vfs_class *me, const char *path, uid_t owner, gid_t group)
  1063. {
  1064. char *sowner, *sgroup;
  1065. struct passwd *pw;
  1066. struct group *gr;
  1067. pw = getpwuid (owner);
  1068. if (pw == NULL)
  1069. return 0;
  1070. gr = getgrgid (group);
  1071. if (gr == NULL)
  1072. return 0;
  1073. sowner = pw->pw_name;
  1074. sgroup = gr->gr_name;
  1075. {
  1076. gchar *shell_commands = NULL;
  1077. PREFIX;
  1078. shell_commands = g_strconcat (SUP.scr_env,
  1079. "FISH_FILENAME=%s FISH_FILEOWNER=%s FISH_FILEGROUP=%s;\n",
  1080. SUP.scr_chown, (char *) NULL);
  1081. g_snprintf (buf, sizeof (buf), shell_commands, rpath, sowner, sgroup);
  1082. g_free (shell_commands);
  1083. fish_send_command (me, super, buf, OPT_FLUSH);
  1084. /* FIXME: what should we report if chgrp succeeds but chown fails? */
  1085. /* fish_send_command(me, super, buf, OPT_FLUSH); */
  1086. g_free (rpath);
  1087. return fish_send_command (me, super, buf, OPT_FLUSH);
  1088. }
  1089. }
  1090. /* --------------------------------------------------------------------------------------------- */
  1091. static int
  1092. fish_utime (struct vfs_class *me, const char *path, struct utimbuf *times)
  1093. {
  1094. gchar *shell_commands = NULL;
  1095. char utcmtime[16], utcatime[16];
  1096. struct tm *gmt;
  1097. PREFIX;
  1098. gmt = gmtime(&times->modtime);
  1099. g_snprintf (utcmtime, sizeof (utcmtime), "%04d%02d%02d%02d%02d.%02d",
  1100. gmt->tm_year+1900, gmt->tm_mon+1, gmt->tm_mday,
  1101. gmt->tm_hour, gmt->tm_min, gmt->tm_sec);
  1102. gmt = gmtime(&times->actime);
  1103. g_snprintf (utcatime, sizeof (utcatime), "%04d%02d%02d%02d%02d.%02d",
  1104. gmt->tm_year+1900, gmt->tm_mon+1, gmt->tm_mday,
  1105. gmt->tm_hour, gmt->tm_min, gmt->tm_sec);
  1106. shell_commands = g_strconcat (SUP.scr_env, "FISH_FILENAME=%s FISH_FILEATIME=%ld FISH_FILEMTIME=%ld ",
  1107. "FISH_TOUCHATIME=%s FISH_TOUCHMTIME=%s;\n",
  1108. SUP.scr_utime, (char *) NULL);
  1109. g_snprintf (buf, sizeof (buf), shell_commands,
  1110. rpath, (long)times->actime, (long)times->modtime, utcatime, utcmtime);
  1111. g_free (shell_commands);
  1112. g_free (rpath);
  1113. return fish_send_command (me, super, buf, OPT_FLUSH);
  1114. }
  1115. /* --------------------------------------------------------------------------------------------- */
  1116. static int
  1117. fish_unlink (struct vfs_class *me, const char *path)
  1118. {
  1119. gchar *shell_commands = NULL;
  1120. PREFIX;
  1121. shell_commands =
  1122. g_strconcat (SUP.scr_env, "FISH_FILENAME=%s;\n", SUP.scr_unlink, (char *) NULL);
  1123. g_snprintf (buf, sizeof (buf), shell_commands, rpath);
  1124. g_free (shell_commands);
  1125. g_free (rpath);
  1126. return fish_send_command (me, super, buf, OPT_FLUSH);
  1127. }
  1128. /* --------------------------------------------------------------------------------------------- */
  1129. static int
  1130. fish_exists (struct vfs_class *me, const char *path)
  1131. {
  1132. gchar *shell_commands = NULL;
  1133. PREFIX;
  1134. shell_commands =
  1135. g_strconcat (SUP.scr_env, "FISH_FILENAME=%s;\n", SUP.scr_exists, (char *) NULL);
  1136. g_snprintf (buf, sizeof (buf), shell_commands, rpath);
  1137. g_free (shell_commands);
  1138. g_free (rpath);
  1139. return (fish_send_command (me, super, buf, OPT_FLUSH) == 0) ? 1 : 0;
  1140. }
  1141. /* --------------------------------------------------------------------------------------------- */
  1142. static int
  1143. fish_mkdir (struct vfs_class *me, const char *path, mode_t mode)
  1144. {
  1145. gchar *shell_commands = NULL;
  1146. int ret_code;
  1147. PREFIX;
  1148. (void) mode;
  1149. shell_commands = g_strconcat (SUP.scr_env, "FISH_FILENAME=%s;\n", SUP.scr_mkdir, (char *) NULL);
  1150. g_snprintf (buf, sizeof (buf), shell_commands, rpath);
  1151. g_free (shell_commands);
  1152. g_free (rpath);
  1153. ret_code = fish_send_command (me, super, buf, OPT_FLUSH);
  1154. if (ret_code != 0)
  1155. return ret_code;
  1156. if (!fish_exists (me, path))
  1157. {
  1158. ERRNOR (EACCES, -1);
  1159. }
  1160. return 0;
  1161. }
  1162. /* --------------------------------------------------------------------------------------------- */
  1163. static int
  1164. fish_rmdir (struct vfs_class *me, const char *path)
  1165. {
  1166. gchar *shell_commands = NULL;
  1167. PREFIX;
  1168. shell_commands = g_strconcat (SUP.scr_env, "FISH_FILENAME=%s;\n", SUP.scr_rmdir, (char *) NULL);
  1169. g_snprintf (buf, sizeof (buf), shell_commands, rpath);
  1170. g_free (shell_commands);
  1171. g_free (rpath);
  1172. return fish_send_command (me, super, buf, OPT_FLUSH);
  1173. }
  1174. /* --------------------------------------------------------------------------------------------- */
  1175. static int
  1176. fish_fh_open (struct vfs_class *me, struct vfs_s_fh *fh, int flags, mode_t mode)
  1177. {
  1178. (void) mode;
  1179. fh->u.fish.append = 0;
  1180. /* File will be written only, so no need to retrieve it */
  1181. if (((flags & O_WRONLY) == O_WRONLY) && !(flags & (O_RDONLY | O_RDWR)))
  1182. {
  1183. fh->u.fish.append = flags & O_APPEND;
  1184. if (!fh->ino->localname)
  1185. {
  1186. int tmp_handle = vfs_mkstemps (&fh->ino->localname, me->name,
  1187. fh->ino->ent->name);
  1188. if (tmp_handle == -1)
  1189. return -1;
  1190. close (tmp_handle);
  1191. }
  1192. return 0;
  1193. }
  1194. if (!fh->ino->localname)
  1195. if (vfs_s_retrieve_file (me, fh->ino) == -1)
  1196. return -1;
  1197. if (!fh->ino->localname)
  1198. vfs_die ("retrieve_file failed to fill in localname");
  1199. return 0;
  1200. }
  1201. /* --------------------------------------------------------------------------------------------- */
  1202. static void
  1203. fish_fill_names (struct vfs_class *me, fill_names_f func)
  1204. {
  1205. struct vfs_s_super *super = MEDATA->supers;
  1206. char *name;
  1207. char gbuf[10];
  1208. while (super)
  1209. {
  1210. const char *flags = "";
  1211. switch (SUP.flags)
  1212. {
  1213. case FISH_FLAG_RSH:
  1214. flags = ":r";
  1215. break;
  1216. case FISH_FLAG_COMPRESSED:
  1217. flags = ":C";
  1218. break;
  1219. default:
  1220. if (SUP.flags > FISH_FLAG_RSH)
  1221. {
  1222. g_snprintf (gbuf, sizeof (gbuf), ":%d", SUP.flags);
  1223. flags = gbuf;
  1224. }
  1225. break;
  1226. }
  1227. name = g_strconcat ("/#sh:", SUP.user, "@", SUP.host, flags, "/", SUP.cwdir, (char *) NULL);
  1228. (*func) (name);
  1229. g_free (name);
  1230. super = super->next;
  1231. }
  1232. }
  1233. /* --------------------------------------------------------------------------------------------- */
  1234. static void *
  1235. fish_open (struct vfs_class *me, const char *file, int flags, mode_t mode)
  1236. {
  1237. /*
  1238. sorry, i've places hack here
  1239. cause fish don't able to open files with O_EXCL flag
  1240. */
  1241. flags &= ~O_EXCL;
  1242. return vfs_s_open (me, file, flags, mode);
  1243. }
  1244. /* --------------------------------------------------------------------------------------------- */
  1245. /*** public functions ****************************************************************************/
  1246. /* --------------------------------------------------------------------------------------------- */
  1247. void
  1248. init_fish (void)
  1249. {
  1250. static struct vfs_s_subclass fish_subclass;
  1251. tcp_init ();
  1252. fish_subclass.flags = VFS_S_REMOTE;
  1253. fish_subclass.archive_same = fish_archive_same;
  1254. fish_subclass.open_archive = fish_open_archive;
  1255. fish_subclass.free_archive = fish_free_archive;
  1256. fish_subclass.fh_open = fish_fh_open;
  1257. fish_subclass.dir_load = fish_dir_load;
  1258. fish_subclass.file_store = fish_file_store;
  1259. fish_subclass.linear_start = fish_linear_start;
  1260. fish_subclass.linear_read = fish_linear_read;
  1261. fish_subclass.linear_close = fish_linear_close;
  1262. vfs_s_init_class (&vfs_fish_ops, &fish_subclass);
  1263. vfs_fish_ops.name = "fish";
  1264. vfs_fish_ops.prefix = "sh:";
  1265. vfs_fish_ops.fill_names = fish_fill_names;
  1266. vfs_fish_ops.chmod = fish_chmod;
  1267. vfs_fish_ops.chown = fish_chown;
  1268. vfs_fish_ops.utime = fish_utime;
  1269. vfs_fish_ops.open = fish_open;
  1270. vfs_fish_ops.symlink = fish_symlink;
  1271. vfs_fish_ops.link = fish_link;
  1272. vfs_fish_ops.unlink = fish_unlink;
  1273. vfs_fish_ops.rename = fish_rename;
  1274. vfs_fish_ops.mkdir = fish_mkdir;
  1275. vfs_fish_ops.rmdir = fish_rmdir;
  1276. vfs_fish_ops.ctl = fish_ctl;
  1277. vfs_register_class (&vfs_fish_ops);
  1278. }
  1279. /* --------------------------------------------------------------------------------------------- */