Просмотр исходного кода

* Don't pass fileno(f) around to Stream, use the fd or filename only

Bryan Drewery 16 лет назад
Родитель
Сommit
4696a7652c
6 измененных файлов с 38 добавлено и 66 удалено
  1. 4 4
      src/conf.c
  2. 1 1
      src/conf.h
  3. 2 2
      src/misc.c
  4. 14 20
      src/mod/share.mod/share.c
  5. 17 38
      src/userrec.c
  6. 0 1
      src/userrec.h

+ 4 - 4
src/conf.c

@@ -203,7 +203,7 @@ confedit()
 
 
   um = umask(077);
   um = umask(077);
 
 
-  autowrote = writeconf(NULL, tmpconf.f, CONF_COMMENT);
+  autowrote = writeconf(NULL, tmpconf.fd, CONF_COMMENT);
   fstat(tmpconf.fd, &st);		/* for file modification compares */
   fstat(tmpconf.fd, &st);		/* for file modification compares */
 //  tmpconf.my_close();
 //  tmpconf.my_close();
 
 
@@ -767,7 +767,7 @@ readconf(const char *fname, int bits)
 char s1_9[3] = "",s1_5[3] = "",s1_1[3] = "";
 char s1_9[3] = "",s1_5[3] = "",s1_1[3] = "";
 
 
 int
 int
-writeconf(char *filename, FILE *f, int bits)
+writeconf(char *filename, int fd, int bits)
 {
 {
   conf_bot *bot = NULL;
   conf_bot *bot = NULL;
   int autowrote = 0;
   int autowrote = 0;
@@ -913,8 +913,8 @@ writeconf(char *filename, FILE *f, int bits)
              bot->net.host ? bot->net.host : (bot->net.host6 ? bot->net.host6 : "*"), bot->net.ip6 ? bot->net.ip6 : "");
              bot->net.host ? bot->net.host : (bot->net.host6 ? bot->net.host6 : "*"), bot->net.ip6 ? bot->net.ip6 : "");
   }
   }
 
 
-  if (f)
-    stream.writeFile(fileno(f));
+  if (fd != -1)
+    stream.writeFile(fd);
   else
   else
     stream.writeFile(filename);
     stream.writeFile(filename);
 
 

+ 1 - 1
src/conf.h

@@ -67,7 +67,7 @@ void free_conf_bots(conf_bot *);
 void free_bot(conf_bot *bot);
 void free_bot(conf_bot *bot);
 int readconf(const char *, int);
 int readconf(const char *, int);
 int parseconf(bool);
 int parseconf(bool);
-int writeconf(char *, FILE *, int);
+int writeconf(char *, int, int);
 void fill_conf_bot(bool fatal = 1);
 void fill_conf_bot(bool fatal = 1);
 void bin_to_conf(bool error = 0);
 void bin_to_conf(bool error = 0);
 void conf_checkpids(conf_bot *bots, bool all = 1);
 void conf_checkpids(conf_bot *bots, bool all = 1);

+ 2 - 2
src/misc.c

@@ -728,7 +728,7 @@ restart(int idx)
   stream << buf.printf(STR("+ip6 %s\n"), myipstr(AF_INET6));
   stream << buf.printf(STR("+ip6 %s\n"), myipstr(AF_INET6));
   replay_cache(-1, &stream);
   replay_cache(-1, &stream);
 
 
-  stream.writeFile(fileno(socks->f));
+  stream.writeFile(socks->fd);
 
 
   socks->my_close();
   socks->my_close();
 
 
@@ -862,7 +862,7 @@ int updatebin(int idx, char *par, int secs)
 
 
   Tempfile *conffile = new Tempfile("conf");
   Tempfile *conffile = new Tempfile("conf");
 
 
-  if (writeconf(NULL, conffile->f, CONF_ENC)) {
+  if (writeconf(NULL, conffile->fd, CONF_ENC)) {
     putlog(LOG_MISC, "*", STR("Failed to write temporary config file for update."));
     putlog(LOG_MISC, "*", STR("Failed to write temporary config file for update."));
     delete conffile;
     delete conffile;
     return 1;
     return 1;

+ 14 - 20
src/mod/share.mod/share.c

@@ -1244,25 +1244,6 @@ check_expired_tbufs()
   }
   }
 }
 }
 
 
-static bool
-write_tmp_userfile(char *fn, const struct userrec *bu, int idx)
-{
-  FILE *f = NULL;
-  int ok = 1;
-
-  if ((f = fopen(fn, "wb"))) {
-    if (real_writeuserfile(idx, bu, f, 1)) {
-      unlink(fn);
-      ok = 0;
-    }
-    fclose(f);
-    fixmod(fn);
-  }
-  if (!ok)
-    putlog(LOG_MISC, "*", "ERROR writing user file to transfer.");
-  return ok;
-}
-
 /* Erase old user list, switch to new one.
 /* Erase old user list, switch to new one.
  */
  */
 void
 void
@@ -1492,7 +1473,20 @@ start_sending_users(int idx)
   simple_snprintf(share_file, sizeof(share_file), "%s.share.%s", tempdir, rand);
   simple_snprintf(share_file, sizeof(share_file), "%s.share.%s", tempdir, rand);
   //mktemp(share_file); //Although safe here, g++ complains too much.
   //mktemp(share_file); //Although safe here, g++ complains too much.
 
 
-  write_tmp_userfile(share_file, userlist, idx);
+/* FIXME: REMOVE AFTER 1.2.14 */
+  bool old = 0;
+
+  tand_t* bot = idx != -1 ? findbot(dcc[idx].nick) : NULL;
+  if (bot && bot->buildts < 1175102242) /* flood-* hacks */
+    old = 1;
+
+  const char salt1[] = SALT1;
+  EncryptedStream stream(salt1);
+  stream_writeuserfile(stream, userlist, idx, old);
+  if (stream.writeFile(share_file, (ENC_KEEP_NEWLINES|ENC_AES_256_ECB|ENC_BASE64_BROKEN))) {
+    putlog(LOG_MISC, "*", "ERROR writing user file to transfer.");
+    unlink(share_file);
+  }
 
 
 /* compress.mod
 /* compress.mod
   if (!compress_file(share_file, compress_level)) {
   if (!compress_file(share_file, compress_level)) {

+ 17 - 38
src/userrec.c

@@ -485,50 +485,15 @@ int write_userfile(int idx)
   if (userlist == NULL || !conf.bot->hub)
   if (userlist == NULL || !conf.bot->hub)
     return 1;			/* No point in saving userfile */
     return 1;			/* No point in saving userfile */
 
 
-  FILE *f = NULL;
-  char *new_userfile = NULL;
-  size_t siz = strlen(userfile) + 4 + 1;
-
-  new_userfile = (char *) my_calloc(1, siz);
-  simple_snprintf(new_userfile, siz, "%s~new", userfile);
-
-  f = fopen(new_userfile, "w");
-  if (f == NULL) {
-    putlog(LOG_MISC, "*", "ERROR writing user file.");
-    free(new_userfile);
-    return 2;
-  }
-  fchmod(fileno(f), S_IRUSR | S_IWUSR);
-
-  char backup[DIRMAX] = "";
-
   if (idx >= 0)
   if (idx >= 0)
     dprintf(idx, "Saving userfile...\n");
     dprintf(idx, "Saving userfile...\n");
 
 
   if (sort_users)
   if (sort_users)
     sort_userlist();
     sort_userlist();
 
 
-
   putlog(LOG_DEBUG, "@", "Writing user entries.");
   putlog(LOG_DEBUG, "@", "Writing user entries.");
 
 
-  if (real_writeuserfile(idx, userlist, f)) {
-    putlog(LOG_MISC, "*", "ERROR writing user file. (%s)", strerror(ferror(f)));
-    unlink(new_userfile);
-    fclose(f);
-    free(new_userfile);
-    return 3;
-  }
-  fclose(f);
-  putlog(LOG_DEBUG, "@", "Done writing userfile.");
-  simple_snprintf(backup, sizeof backup, "%s/%s~", conf.datadir, userfile);
-  copyfile(userfile, backup);
-  movefile(new_userfile, userfile);
-  free(new_userfile);
-  return 0;
-}
-
-/* Used by writeuserfile() and write_tmp_userfile() in share.c */
-int real_writeuserfile(int idx, const struct userrec *bu, FILE *f, bool compat) {
+  Tempfile *new_userfile = new Tempfile("userfile");
 /* FIXME: REMOVE AFTER 1.2.14 */
 /* FIXME: REMOVE AFTER 1.2.14 */
   bool old = 0;
   bool old = 0;
 
 
@@ -538,8 +503,22 @@ int real_writeuserfile(int idx, const struct userrec *bu, FILE *f, bool compat)
 
 
   const char salt1[] = SALT1;
   const char salt1[] = SALT1;
   EncryptedStream stream(salt1);
   EncryptedStream stream(salt1);
-  stream_writeuserfile(stream, bu, idx, old);
-  return stream.writeFile(fileno(f), compat ? ENC_DEFAULT : (ENC_KEEP_NEWLINES|ENC_AES_256_ECB|ENC_BASE64_BROKEN));
+  stream_writeuserfile(stream, userlist, idx, old);
+  if (stream.writeFile(new_userfile->fd)) {
+    putlog(LOG_MISC, "*", "ERROR writing user file. (%s)", strerror(errno));
+    delete new_userfile;
+    return 3;
+  }
+  new_userfile->my_close();
+  putlog(LOG_DEBUG, "@", "Done writing userfile.");
+
+  char backup[DIRMAX] = "";
+
+  simple_snprintf(backup, sizeof backup, "%s/%s~", conf.datadir, userfile);
+  copyfile(userfile, backup);
+  movefile(new_userfile->file, userfile);
+  delete new_userfile;
+  return 0;
 }
 }
 
 
 int change_handle(struct userrec *u, char *newh)
 int change_handle(struct userrec *u, char *newh)

+ 0 - 1
src/userrec.h

@@ -17,7 +17,6 @@ int change_handle(struct userrec *, char *);
 void correct_handle(char *);
 void correct_handle(char *);
 void stream_writeuserfile(bd::Stream&, const struct userrec *, int, bool = 0);
 void stream_writeuserfile(bd::Stream&, const struct userrec *, int, bool = 0);
 int write_userfile(int);
 int write_userfile(int);
-int real_writeuserfile(int idx, const struct userrec *bu, FILE *f, bool = 0);
 void touch_laston(struct userrec *, char *, time_t);
 void touch_laston(struct userrec *, char *, time_t);
 void user_del_chan(char *);
 void user_del_chan(char *);
 struct userrec *host_conflicts(char *);
 struct userrec *host_conflicts(char *);