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

📄 gnunet-auto-share.c

📁 GNUnet是一个安全的点对点网络框架
💻 C
📖 第 1 页 / 共 3 页
字号:
  ctx = GNUNET_FSUI_start (ectx, cfg, "gnunet-auto-share", GNUNET_YES, 32,
                           &printstatus, &verbose);

  dirs_idx1 = dirs_idx2 = dirs;
  while (1)
    if ((*dirs_idx2 == ';') || (*dirs_idx2 == '\0'))
      {
        int end;

        end = *dirs_idx2 == '\0';
        *dirs_idx2 = 0;

        pos = GNUNET_malloc (sizeof (struct DirectoryRecord));
        pos->dirname = GNUNET_expand_file_name (ectx, dirs_idx1);
        pos->records = read_all_records (pos->dirname);
        pos->records_changed = GNUNET_NO;
        pos->run = 0;
        pos->next = head;
        head = pos;

        if (end)
          break;

        dirs_idx1 = ++dirs_idx2;
      }
    else
      dirs_idx2++;

  /* first insert all of the top-level files or directories */
  delay = 5 * GNUNET_CRON_SECONDS;
  while (GNUNET_NO == GNUNET_shutdown_test ())
    {
      work_done = GNUNET_NO;
      GNUNET_thread_sleep (250 * GNUNET_CRON_MILLISECONDS);
      if (ul == NULL)
        {
          pos = head;
          while ((pos != NULL) && (GNUNET_NO == GNUNET_shutdown_test ()))
            {
              GNUNET_disk_directory_scan (ectx, pos->dirname,
                                          &probe_directory, pos);
              if (GNUNET_YES == upload_done)
                {
                  work_done = GNUNET_YES;
                  GNUNET_FSUI_upload_abort (ul);
                  GNUNET_FSUI_upload_stop (ul);
                  upload_done = GNUNET_NO;
                  ul = NULL;
                }
              pos = pos->next;
            }
        }
      else
        {
          if (GNUNET_YES == upload_done)
            {
              work_done = GNUNET_YES;
              GNUNET_FSUI_upload_abort (ul);
              GNUNET_FSUI_upload_stop (ul);
              upload_done = GNUNET_NO;
              ul = NULL;
            }
        }
      if ((ul == NULL) &&
          (work_done == GNUNET_NO) && (GNUNET_NO == GNUNET_shutdown_test ()))
        {
          GNUNET_thread_sleep (delay);
          delay *= 2;
          if (delay > GNUNET_CRON_HOURS)
            delay = GNUNET_CRON_HOURS;
        }
      else
        {
          delay = 5 * GNUNET_CRON_SECONDS;
        }
    }
  GNUNET_FSUI_stop (ctx);
  if (gloKeywords != NULL)
    GNUNET_ECRS_uri_destroy (gloKeywords);
quit:
  while (head != NULL)
    {
      if (head->records_changed)
        write_all_records (head);
      while (head->records != NULL)
        {
          rpos = head->records;
          head->records = rpos->next;
          GNUNET_free (rpos->filename);
          GNUNET_free (rpos);
        }
      pos = head->next;
      GNUNET_free (head->dirname);
      GNUNET_free (head);
      head = pos;
    }

  if (meta_cfg != NULL)
    GNUNET_GC_free (meta_cfg);
  if (sock != NULL)
    GNUNET_client_connection_destroy (sock);
  GNUNET_pid_file_delete (ectx, cfg, PIDFILE_DATA);
  return errorCode;
}

void
auto_share_shutdown_initiate ()
{
  GNUNET_shutdown_initiate ();
}

/**
 * Shutdown gnunetd
 * @param cfg configuration, may be NULL if in service mode
 * @param sig signal code that causes shutdown, optional
 */
void
auto_share_shutdown_request (struct GNUNET_GC_Configuration *cfg, int sig)
{
#ifdef MINGW
  if (!cfg || GNUNET_GC_get_configuration_value_yesno (cfg,
                                                       "GNUNET-AUTO-SHARE",
                                                       "WINSERVICE",
                                                       GNUNET_NO) ==
      GNUNET_YES)
    {
      /* If GNUnet runs as service, only the
         Service Control Manager is allowed
         to kill us. */
      if (sig != SERVICE_CONTROL_STOP)
        {
          SERVICE_STATUS theStat;

          /* Init proper shutdown through the SCM */
          if (GNControlService
              ((SC_HANDLE) hService, SERVICE_CONTROL_STOP, &theStat))
            {
              /* Success */

              /* The Service Control Manager will call
                 gnunetd.c::ServiceCtrlHandler(), which calls
                 this function again. We then stop the gnunetd. */
              return;
            }
          /* We weren't able to tell the SCM to stop the service,
             but we don't care.
             Just shut the gnunetd process down. */
        }

      /* Acknowledge the shutdown request */
      theServiceStatus.dwCurrentState = SERVICE_STOP_PENDING;
      GNSetServiceStatus (hService, &theServiceStatus);
    }
#endif

  auto_share_shutdown_initiate ();
}

#ifdef MINGW
/**
 * This function is called from the Windows Service Control Manager
 * when a service has to shutdown
 */
static void WINAPI
ServiceCtrlHandler (DWORD dwOpcode)
{
  if (dwOpcode == SERVICE_CONTROL_STOP)
    auto_share_shutdown_request (NULL, dwOpcode);
}

void
ServiceMain (DWORD argc, LPSTR * argv)
{
  memset (&theServiceStatus, 0, sizeof (theServiceStatus));
  theServiceStatus.dwServiceType = SERVICE_WIN32;
  theServiceStatus.dwControlsAccepted = SERVICE_ACCEPT_STOP;
  theServiceStatus.dwCurrentState = SERVICE_RUNNING;

  hService =
    GNRegisterServiceCtrlHandler ("GNUnet Auto Share", ServiceCtrlHandler);
  if (!hService)
    return;

  GNSetServiceStatus (hService, &theServiceStatus);
  auto_share_main ();
  theServiceStatus.dwCurrentState = SERVICE_STOPPED;
  GNSetServiceStatus (hService, &theServiceStatus);
}
#endif


/**
 * The main function to auto share directories with GNUnet.
 *
 * @param argc number of arguments from the command line
 * @param argv command line arguments
 * @return return 0 for ok, -1 on error
 */
int
main (int argc, char *const *argv)
{
  int i;
  int errorCode;
  char *log_file_name;
  char *dirs;
  unsigned int dirs_len;
  char *fullname;
  struct stat stbuf;
  const char *dirs_idx1;
  char *dirs_idx2;
  char *base;
  int seen;
  int added;
  int do_break;

  errorCode = 0;
  myout = stdout;
  i = GNUNET_init (argc,
                   argv,
                   "gnunet-auto-share [OPTIONS] DIRECTORY",
                   &cfgFilename, gnunetauto_shareOptions, &ectx, &cfg);
  if (i == -1)
    {
      errorCode = -1;
      goto end;
    }

  if (i < argc)
    {
      GNUNET_GC_get_configuration_value_string (cfg, "GNUNET-AUTO-SHARE",
                                                "DIRS", "", &dirs);
      dirs_len = strlen (dirs);
      added = 0;
      while (i < argc)
        {
          fullname = GNUNET_expand_file_name (ectx, argv[i]);
          if (0 != STAT (fullname, &stbuf))
            {
              FPRINTF (myout,
                       _("Could not access `%s': %s\n"),
                       fullname, STRERROR (errno));
              errorCode = 1;
              GNUNET_free (fullname);
              GNUNET_free (dirs);
              goto end;
            }
          seen = 0;
          dirs_idx1 = dirs_idx2 = base = GNUNET_strdup (dirs);
          while (1)
            {
              if ((*dirs_idx2 == ';') || (*dirs_idx2 == '\0'))
                {
                  do_break = ('\0' == *dirs_idx2);
                  *dirs_idx2 = '\0';
                  if (0 == strcmp (dirs_idx1, fullname))
                    {
                      seen = 1;
                      FPRINTF (myout,
                               _
                               ("Directory `%s' is already on the list of shared directories.\n"),
                               fullname);
                      break;
                    }
                  if (do_break)
                    break;
                  dirs_idx1 = ++dirs_idx2;
                }
              else
                dirs_idx2++;
            }
          GNUNET_free (base);
          if (seen == 0)
            {
              dirs = GNUNET_realloc (dirs, dirs_len + strlen (fullname) + 2);
              if (dirs_len > 0)
                strcat (dirs, ";");
              strcat (dirs, fullname);
              GNUNET_free (fullname);
              added = 1;
            }
          i++;
        }
      GNUNET_GC_set_configuration_value_string (cfg, ectx,
                                                "GNUNET-AUTO-SHARE", "DIRS",
                                                dirs);
      GNUNET_free (dirs);
      if (GNUNET_GC_write_configuration (cfg, cfgFilename) != GNUNET_SYSERR)
        {
          if (added)
            {
              FPRINTF (myout,
                       "%s",
                       _
                       ("The specified directories were added to the list of "
                        "shared directories.\n"));
            }
          errorCode = 0;
        }
      else
        {
          errorCode = -1;
          goto end;
        }
    }

  if (GNUNET_YES != debug_flag)
    {
      GNUNET_GC_get_configuration_value_filename (cfg,
                                                  "GNUNET-AUTO-SHARE",
                                                  "LOGFILE",
                                                  GNUNET_DEFAULT_HOME_DIRECTORY
                                                  "/gnunet-auto-share.log",
                                                  &log_file_name);
      myout = fopen (log_file_name, "a");
      if (myout == NULL)
        {
          fprintf (stderr,
                   _("Could not open logfile `%s': %s\n"),
                   log_file_name, strerror (errno));
          GNUNET_free (log_file_name);
          errorCode = -1;
          goto end;
        }
      GNUNET_free (log_file_name);
    }
#ifdef MINGW
  if (GNUNET_GC_get_configuration_value_yesno (cfg,
                                               "GNUNET-AUTO-SHARE",
                                               "WINSERVICE",
                                               GNUNET_NO) == GNUNET_YES)
    {
      SERVICE_TABLE_ENTRY DispatchTable[] =
        { {"GNUnet Auto Share", ServiceMain}
      , {NULL, NULL}
      };
      errorCode = (GNStartServiceCtrlDispatcher (DispatchTable) != 0);
    }
  else
#endif
    {
      errorCode = auto_share_main ();
    }
end:
  GNUNET_fini (ectx, cfg);
  if (myout != stdout)
    fclose (myout);
  return errorCode;
}

/* end of gnunet-auto-share.c */

⌨️ 快捷键说明

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