From 25ba33334d2e5ee0ae78513923d2902798f285fc Mon Sep 17 00:00:00 2001 From: sin Date: Tue, 23 Sep 2014 09:38:11 +0100 Subject: Add frienddestroy() and refactor removefriend() friendcreate() populates the fs, frienddestroy() removes the friend related files from the fs. removefriend() only acts on the data from the friend's remove fifo. The "callback" in this case is responsible for reading the fifo like in all other cases (sendfriendtext() etc.). No weird dance with special function arguments to flag "official" removal. All friend related "callbacks" just need to operate on a struct friend *. --- ratox.c | 57 ++++++++++++++++++++++++++++++++------------------------- 1 file changed, 32 insertions(+), 25 deletions(-) diff --git a/ratox.c b/ratox.c index b827f74..0523088 100644 --- a/ratox.c +++ b/ratox.c @@ -183,7 +183,7 @@ static void cbuserstatus(Tox *, int32_t, uint8_t, void *); static void cbfilecontrol(Tox *, int32_t, uint8_t, uint8_t, uint8_t, const uint8_t *, uint16_t, void *); static void sendfriendfile(struct friend *); static void sendfriendtext(struct friend *); -static void removefriend(struct friend *, int); +static void removefriend(struct friend *); static int readpass(const char *); static void dataload(void); static void datasave(void); @@ -194,6 +194,7 @@ static void id2str(uint8_t *, char *); static void str2id(char *, uint8_t *); static struct friend *friendcreate(int32_t); static void friendload(void); +static void frienddestroy(struct friend *); static void loop(void); static void initshutdown(int); static void shutdown(void); @@ -533,26 +534,19 @@ sendfriendtext(struct friend *f) } static void -removefriend(struct friend *f, int official) +removefriend(struct friend *f) { - int i; + char c; - if (official) { - tox_del_friend(tox, f->fid); - datasave(); - printout("Removed friend %s\n", - f->namestr[0] == '\0' ? "Anonymous" : f->namestr); - } - for (i = 0; i < LEN(ffiles); i++) { - if (f->dirfd != -1) { - unlinkat(f->dirfd, ffiles[i].name, 0); - if (f->fd[i] != -1) - close(f->fd[i]); - } - } - rmdir(f->idstr); - /* T0D0: cancel transmissions */ - TAILQ_REMOVE(&friendhead, f, entry); + if (fiforead(f->dirfd, &f->fd[FREMOVE], ffiles[FREMOVE], &c, 1) != 1) + return; + if (c != '1') + return; + tox_del_friend(tox, f->fid); + datasave(); + printout("Removed friend %s\n", + f->namestr[0] == '\0' ? "Anonymous" : f->namestr); + frienddestroy(f); } static int @@ -903,6 +897,23 @@ friendcreate(int32_t fid) return f; } +static void +frienddestroy(struct friend *f) +{ + int i; + + for (i = 0; i < LEN(ffiles); i++) { + if (f->dirfd != -1) { + unlinkat(f->dirfd, ffiles[i].name, 0); + if (f->fd[i] != -1) + close(f->fd[i]); + } + } + rmdir(f->idstr); + /* T0D0: cancel transmissions */ + TAILQ_REMOVE(&friendhead, f, entry); +} + static void friendload(void) { @@ -1174,11 +1185,7 @@ loop(void) } break; case FREMOVE: - if (fiforead(f->dirfd, &f->fd[FREMOVE], ffiles[FREMOVE], &c, 1) != 1) - return; - if (c != '1') - return; - removefriend(f, 1); + removefriend(f); break; default: fprintf(stderr, "Unhandled FIFO read\n"); @@ -1207,7 +1214,7 @@ shutdown(void) /* friends */ for (f = TAILQ_FIRST(&friendhead); f; f = ftmp) { ftmp = TAILQ_NEXT(f, entry); - removefriend(f, 0); + frienddestroy(f); } /* requests */ -- cgit v1.2.3