⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ftp.c

📁 一个从网络上自动下载文件的自由工具
💻 C
📖 第 1 页 / 共 4 页
字号:
    {      print_length (*len, restval, true);      expected_bytes = *len;    /* for fd_read_body's progress bar */    }  else if (expected_bytes)    print_length (expected_bytes, restval, false);  /* Get the contents of the document.  */  flags = 0;  if (restval && rest_failed)    flags |= rb_skip_startpos;  *len = restval;  rd_size = 0;  res = fd_read_body (dtsock, fp,                      expected_bytes ? expected_bytes - restval : 0,                      restval, &rd_size, len, &con->dltime, flags);  tms = datetime_str (time (NULL));  tmrate = retr_rate (rd_size, con->dltime);  total_download_time += con->dltime;  fd_close (local_sock);  /* Close the local file.  */  if (!output_stream || con->cmd & DO_LIST)    fclose (fp);  /* If fd_read_body couldn't write to fp, bail out.  */  if (res == -2)    {      logprintf (LOG_NOTQUIET, _("%s: %s, closing control connection.\n"),                 con->target, strerror (errno));      fd_close (csock);      con->csock = -1;      fd_close (dtsock);      return FWRITEERR;    }  else if (res == -1)    {      logprintf (LOG_NOTQUIET, _("%s (%s) - Data connection: %s; "),                 tms, tmrate, fd_errstr (dtsock));      if (opt.server_response)        logputs (LOG_ALWAYS, "\n");    }  fd_close (dtsock);  /* Get the server to tell us if everything is retrieved.  */  err = ftp_response (csock, &respline);  if (err != FTPOK)    {      /* The control connection is decidedly closed.  Print the time         only if it hasn't already been printed.  */      if (res != -1)        logprintf (LOG_NOTQUIET, "%s (%s) - ", tms, tmrate);      logputs (LOG_NOTQUIET, _("Control connection closed.\n"));      /* If there is an error on the control connection, close it, but         return FTPRETRINT, since there is a possibility that the         whole file was retrieved nevertheless (but that is for         ftp_loop_internal to decide).  */      fd_close (csock);      con->csock = -1;      return FTPRETRINT;    } /* err != FTPOK */  /* If retrieval failed for any reason, return FTPRETRINT, but do not     close socket, since the control connection is still alive.  If     there is something wrong with the control connection, it will     become apparent later.  */  if (*respline != '2')    {      xfree (respline);      if (res != -1)        logprintf (LOG_NOTQUIET, "%s (%s) - ", tms, tmrate);      logputs (LOG_NOTQUIET, _("Data transfer aborted.\n"));      return FTPRETRINT;    }  xfree (respline);  if (res == -1)    {      /* What now?  The data connection was erroneous, whereas the         response says everything is OK.  We shall play it safe.  */      return FTPRETRINT;    }  if (!(cmd & LEAVE_PENDING))    {      /* Closing the socket is faster than sending 'QUIT' and the         effect is the same.  */      fd_close (csock);      con->csock = -1;    }  /* If it was a listing, and opt.server_response is true,     print it out.  */  if (opt.server_response && (con->cmd & DO_LIST))    {      mkalldirs (con->target);      fp = fopen (con->target, "r");      if (!fp)        logprintf (LOG_ALWAYS, "%s: %s\n", con->target, strerror (errno));      else        {          char *line;          /* The lines are being read with read_whole_line because of             no-buffering on opt.lfile.  */          while ((line = read_whole_line (fp)) != NULL)            {              char *p = strchr (line, '\0');              while (p > line && (p[-1] == '\n' || p[-1] == '\r'))                *--p = '\0';              logprintf (LOG_ALWAYS, "%s\n", escnonprint (line));              xfree (line);            }          fclose (fp);        }    } /* con->cmd & DO_LIST && server_response */  return RETRFINISHED;}/* A one-file FTP loop.  This is the part where FTP retrieval is   retried, and retried, and retried, and...   This loop either gets commands from con, or (if ON_YOUR_OWN is   set), makes them up to retrieve the file given by the URL.  */static uerr_tftp_loop_internal (struct url *u, struct fileinfo *f, ccon *con){  int count, orig_lp;  wgint restval, len = 0;  char *tms, *locf;  const char *tmrate = NULL;  uerr_t err;  struct_stat st;  if (!con->target)    con->target = url_file_name (u);  if (opt.noclobber && file_exists_p (con->target))    {      logprintf (LOG_VERBOSE,                 _("File `%s' already there; not retrieving.\n"), con->target);      /* If the file is there, we suppose it's retrieved OK.  */      return RETROK;    }  /* Remove it if it's a link.  */  remove_link (con->target);  if (!opt.output_document)    locf = con->target;  else    locf = opt.output_document;  count = 0;  if (con->st & ON_YOUR_OWN)    con->st = ON_YOUR_OWN;  orig_lp = con->cmd & LEAVE_PENDING ? 1 : 0;  /* THE loop.  */  do    {      /* Increment the pass counter.  */      ++count;      sleep_between_retrievals (count);      if (con->st & ON_YOUR_OWN)        {          con->cmd = 0;          con->cmd |= (DO_RETR | LEAVE_PENDING);          if (con->csock != -1)            con->cmd &= ~ (DO_LOGIN | DO_CWD);          else            con->cmd |= (DO_LOGIN | DO_CWD);        }      else /* not on your own */        {          if (con->csock != -1)            con->cmd &= ~DO_LOGIN;          else            con->cmd |= DO_LOGIN;          if (con->st & DONE_CWD)            con->cmd &= ~DO_CWD;          else            con->cmd |= DO_CWD;        }      /* Decide whether or not to restart.  */      if (opt.always_rest          && stat (locf, &st) == 0          && S_ISREG (st.st_mode))        /* When -c is used, continue from on-disk size.  (Can't use           hstat.len even if count>1 because we don't want a failed           first attempt to clobber existing data.)  */        restval = st.st_size;      else if (count > 1)        restval = len;          /* start where the previous run left off */      else        restval = 0;      /* Get the current time string.  */      tms = datetime_str (time (NULL));      /* Print fetch message, if opt.verbose.  */      if (opt.verbose)        {          char *hurl = url_string (u, URL_AUTH_HIDE_PASSWD);          char tmp[256];          strcpy (tmp, "        ");          if (count > 1)            sprintf (tmp, _("(try:%2d)"), count);          logprintf (LOG_VERBOSE, "--%s--  %s\n  %s => `%s'\n",                     tms, hurl, tmp, locf);#ifdef WINDOWS          ws_changetitle (hurl);#endif          xfree (hurl);        }      /* Send getftp the proper length, if fileinfo was provided.  */      if (f)        len = f->size;      else        len = 0;      err = getftp (u, &len, restval, con);      if (con->csock == -1)        con->st &= ~DONE_CWD;      else        con->st |= DONE_CWD;      switch (err)        {        case HOSTERR: case CONIMPOSSIBLE: case FWRITEERR: case FOPENERR:        case FTPNSFOD: case FTPLOGINC: case FTPNOPASV: case CONTNOTSUPPORTED:          /* Fatal errors, give up.  */          return err;        case CONSOCKERR: case CONERROR: case FTPSRVERR: case FTPRERR:        case WRITEFAILED: case FTPUNKNOWNTYPE: case FTPSYSERR:        case FTPPORTERR: case FTPLOGREFUSED: case FTPINVPASV:        case FOPEN_EXCL_ERR:          printwhat (count, opt.ntry);          /* non-fatal errors */          if (err == FOPEN_EXCL_ERR)            {              /* Re-determine the file name. */              xfree_null (con->target);              con->target = url_file_name (u);              locf = con->target;            }          continue;        case FTPRETRINT:          /* If the control connection was closed, the retrieval             will be considered OK if f->size == len.  */          if (!f || len != f->size)            {              printwhat (count, opt.ntry);              continue;            }          break;        case RETRFINISHED:          /* Great!  */          break;        default:          /* Not as great.  */          abort ();        }      tms = datetime_str (time (NULL));      if (!opt.spider)        tmrate = retr_rate (len - restval, con->dltime);      /* If we get out of the switch above without continue'ing, we've         successfully downloaded a file.  Remember this fact. */      downloaded_file (FILE_DOWNLOADED_NORMALLY, locf);      if (con->st & ON_YOUR_OWN)        {          fd_close (con->csock);          con->csock = -1;        }      if (!opt.spider)        logprintf (LOG_VERBOSE, _("%s (%s) - `%s' saved [%s]\n\n"),                   tms, tmrate, locf, number_to_static_string (len));      if (!opt.verbose && !opt.quiet)        {          /* Need to hide the password from the URL.  The `if' is here             so that we don't do the needless allocation every             time. */          char *hurl = url_string (u, URL_AUTH_HIDE_PASSWD);          logprintf (LOG_NONVERBOSE, "%s URL: %s [%s] -> \"%s\" [%d]\n",                     tms, hurl, number_to_static_string (len), locf, count);          xfree (hurl);        }      if ((con->cmd & DO_LIST))        /* This is a directory listing file. */        {          if (!opt.remove_listing)            /* --dont-remove-listing was specified, so do count this towards the               number of bytes and files downloaded. */            {              total_downloaded_bytes += len;              opt.numurls++;            }          /* Deletion of listing files is not controlled by --delete-after, but             by the more specific option --dont-remove-listing, and the code             to do this deletion is in another function. */        }      else if (!opt.spider)        /* This is not a directory listing file. */        {          /* Unlike directory listing files, don't pretend normal files weren't             downloaded if they're going to be deleted.  People seeding proxies,             for instance, may want to know how many bytes and files they've             downloaded through it. */          total_downloaded_bytes += len;          opt.numurls++;          if (opt.delete_after)            {              DEBUGP (("\Removing file due to --delete-after in ftp_loop_internal():\n"));              logprintf (LOG_VERBOSE, _("Removing %s.\n"), locf);              if (unlink (locf))                logprintf (LOG_NOTQUIET, "unlink: %s\n", strerror (errno));            }        }      /* Restore the original leave-pendingness.  */      if (orig_lp)        con->cmd |= LEAVE_PENDING;      else        con->cmd &= ~LEAVE_PENDING;      return RETROK;    } while (!opt.ntry || (count < opt.ntry));  if (con->csock != -1 && (con->st & ON_YOUR_OWN))    {      fd_close (con->csock);      con->csock = -1;    }  return TRYLIMEXC;}/* Return the directory listing in a reusable format.  The directory   is specifed in u->dir.  */static uerr_tftp_get_listing (struct url *u, ccon *con, struct fileinfo **f){  uerr_t err;  char *uf;                     /* url file name */  char *lf;                     /* list file name */  char *old_target = con->target;  con->st &= ~ON_YOUR_OWN;  con->cmd |= (DO_LIST | LEAVE_PENDING);  con->cmd &= ~DO_RETR;  /* Find the listing file name.  We do it by taking the file name of     the URL and replacing the last component with the listing file     name.  */  uf = url_file_name (u);  lf = file_merge (uf, LIST_FILENAME);  xfree (uf);  DEBUGP ((_("Using `%s' as listing tmp file.\n"), lf));  con->target = lf;  err = ftp_loop_internal (u, NULL, con);  con->target = old_target;  if (err == RETROK)    *f = ftp_parse_ls (lf, con->rs);  else    *f = NULL;  if (opt.remove_listing)    {      if (unlink (lf))        logprintf (LOG_NOTQUIET, "unlink: %s\n", strerror (errno));      else        logprintf (LOG_VERBOSE, _("Removed `%s'.\n"), lf);    }  xfree (lf);  con->cmd &= ~DO_LIST;  return err;}static uerr_t ftp_retrieve_dirs (struct url *, struct fileinfo *, ccon *);static uerr_t ftp_retrieve_glob (struct url *, ccon *, int);static struct fileinfo *delelement (struct fileinfo *, struct fileinfo **);static void freefileinfo (struct fileinfo *f);/* Retrieve a list of files given in struct fileinfo linked list.  If   a file is a symbolic link, do not retrieve it, but rather try to   set up a similar link on the local disk, if the symlinks are   supported.   If opt.recursive is set, after all files have been retrieved,   ftp_retrieve_dirs will be called to retrieve the directories.  */static uerr_tftp_retrieve_list (struct url *u, struct fileinfo *f, ccon *con){  static int depth = 0;  uerr_t err;  struct fileinfo *orig;  wgint local_size;  time_t tml;  bool dlthis;  /* Increase the depth.  */  ++depth;  if (opt.reclevel != INFINITE_RECURSION && depth > opt.reclevel)    {      DEBUGP ((_("Recursion depth %d exceeded max. depth %d.\n"),               depth, opt.reclevel));      --depth;      return RECLEVELEXC;    }  assert (f != NULL);  orig = f;  con->st &= ~ON_YOUR_OWN;  if (!(con->st & DONE_CWD))    con->cmd |= DO_CWD;  else    con->cmd &= ~DO_CWD;  con->cmd |= (DO_RETR | LEAVE_PENDING);  if (con->csock < 0)    con->cmd |= DO_LOGIN;  else    con->cmd &= ~DO_LOGIN;  err = RETROK;                 /* in case it's not used */  while (f)    {      char *old_target, *ofile;      if (opt.quota && total_downloaded_bytes > opt.quota)        {          --depth;          return QUOTEXC;        }      old_target = con->target;      ofile = xstrdup (u->file);      url_set_file (u, f->name);      con->target = url_file_name (u);      err = RETROK;      dlthis = true;      if (opt.timestamping && f->type == FT_PLAINFILE)        {          struct_stat st;          /* If conversion of HTML files retrieved via FTP is ever implemented,             we'll need to stat() <file>.orig here when -K has been specified.             I'm not implementing it now since files on an FTP server are much             more likely than files on an HTTP server to legitimately have a             .orig suffix. */          if (!stat (con->target, &st))            {              bool eq_size;              bool cor_val;              /* Else, get it from the file.  */              local_size = st.st_size;              tml = st.st_mtime;#ifdef WINDOWS              /* Modification time granularity is 2 seconds for Windows, so                 increase local time by 1 second for later comparison. */              tml++;#endif              /* Compare file sizes only for servers that tell us correct                 values. Assume sizes being equal for servers that lie                 about file size.  */              cor_val = (con->rs == ST_UNIX || con->rs == ST_WINNT);              eq_size = cor_val ? (local_size == f->size) : true;              if (f->tstamp <= tml && eq_size)                {                  /* Remote file is older, file sizes can be compared and                     are both equal. */

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -