From 2e6d5c8bc9c886c67d75ae2c0e6436d4c8928a6e Mon Sep 17 00:00:00 2001 From: Mike Gelfand Date: Sun, 31 May 2015 22:13:31 +0000 Subject: [PATCH] Add more booleans to the picture --- daemon/daemon.c | 4 +-- gtk/open-dialog.c | 6 ++-- gtk/tr-core.c | 8 ++--- libtransmission/bandwidth.c | 1 + libtransmission/bitfield.c | 6 +++- libtransmission/cache.c | 2 +- libtransmission/natpmp.c | 2 +- libtransmission/platform.c | 2 +- libtransmission/platform.h | 2 +- libtransmission/resume.c | 8 ++--- libtransmission/torrent-ctor.c | 52 ++++++++++++++++---------------- libtransmission/torrent-magnet.c | 4 +-- libtransmission/torrent.c | 14 ++++----- libtransmission/torrent.h | 2 +- libtransmission/tr-dht.c | 14 ++++----- libtransmission/tr-dht.h | 2 +- libtransmission/tr-lpd.c | 12 ++++---- libtransmission/transmission.h | 12 ++++---- libtransmission/web.c | 2 +- 19 files changed, 80 insertions(+), 75 deletions(-) diff --git a/daemon/daemon.c b/daemon/daemon.c index 4380670f1..7ec971580 100644 --- a/daemon/daemon.c +++ b/daemon/daemon.c @@ -201,11 +201,11 @@ onFileAdded (tr_session * session, const char * dir, const char * file) else { bool trash = false; - int test = tr_ctorGetDeleteSource (ctor, &trash); + const bool test = tr_ctorGetDeleteSource (ctor, &trash); tr_logAddInfo ("Parsing .torrent file successful \"%s\"", file); - if (!test && trash) + if (test && trash) { tr_error * error = NULL; diff --git a/gtk/open-dialog.c b/gtk/open-dialog.c index b0764645b..61cef2b49 100644 --- a/gtk/open-dialog.c +++ b/gtk/open-dialog.c @@ -289,7 +289,7 @@ gtr_torrent_options_dialog_new (GtkWindow * parent, TrCore * core, tr_ctor * cto GTK_RESPONSE_CANCEL, -1); - if (tr_ctorGetDownloadDir (ctor, TR_FORCE, &str)) + if (!tr_ctorGetDownloadDir (ctor, TR_FORCE, &str)) g_assert_not_reached (); g_assert (str); @@ -375,7 +375,7 @@ gtr_torrent_options_dialog_new (GtkWindow * parent, TrCore * core, tr_ctor * cto /* torrent priority row */ row++; w = data->run_check; - if (tr_ctorGetPaused (ctor, TR_FORCE, &flag)) + if (!tr_ctorGetPaused (ctor, TR_FORCE, &flag)) g_assert_not_reached (); gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (w), !flag); gtk_grid_attach (grid, w, 0, row, 2, 1); @@ -383,7 +383,7 @@ gtr_torrent_options_dialog_new (GtkWindow * parent, TrCore * core, tr_ctor * cto /* "trash .torrent file" row */ row++; w = data->trash_check; - if (tr_ctorGetDeleteSource (ctor, &flag)) + if (!tr_ctorGetDeleteSource (ctor, &flag)) g_assert_not_reached (); gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (w), flag); gtk_grid_attach (grid, w, 0, row, 2, 1); diff --git a/gtk/tr-core.c b/gtk/tr-core.c index 4c346d259..96340b0c0 100644 --- a/gtk/tr-core.c +++ b/gtk/tr-core.c @@ -1171,16 +1171,16 @@ core_add_ctor (TrCore * core, tr_ctor * ctor, static void core_apply_defaults (tr_ctor * ctor) { - if (tr_ctorGetPaused (ctor, TR_FORCE, NULL)) + if (!tr_ctorGetPaused (ctor, TR_FORCE, NULL)) tr_ctorSetPaused (ctor, TR_FORCE, !gtr_pref_flag_get (TR_KEY_start_added_torrents)); - if (tr_ctorGetDeleteSource (ctor, NULL)) + if (!tr_ctorGetDeleteSource (ctor, NULL)) tr_ctorSetDeleteSource (ctor, gtr_pref_flag_get (TR_KEY_trash_original_torrent_files)); - if (tr_ctorGetPeerLimit (ctor, TR_FORCE, NULL)) + if (!tr_ctorGetPeerLimit (ctor, TR_FORCE, NULL)) tr_ctorSetPeerLimit (ctor, TR_FORCE, gtr_pref_int_get (TR_KEY_peer_limit_per_torrent)); - if (tr_ctorGetDownloadDir (ctor, TR_FORCE, NULL)) + if (!tr_ctorGetDownloadDir (ctor, TR_FORCE, NULL)) tr_ctorSetDownloadDir (ctor, TR_FORCE, gtr_pref_string_get (TR_KEY_download_dir)); } diff --git a/libtransmission/bandwidth.c b/libtransmission/bandwidth.c index 845c0d99e..9a1c52fa4 100644 --- a/libtransmission/bandwidth.c +++ b/libtransmission/bandwidth.c @@ -346,6 +346,7 @@ bandwidthClamp (const tr_bandwidth * b, return byteCount; } + unsigned int tr_bandwidthClamp (const tr_bandwidth * b, tr_direction dir, diff --git a/libtransmission/bitfield.c b/libtransmission/bitfield.c index bfa0995b7..d69e08203 100644 --- a/libtransmission/bitfield.c +++ b/libtransmission/bitfield.c @@ -146,8 +146,10 @@ tr_bitfieldHas (const tr_bitfield * b, size_t n) **** ***/ +#ifndef NDEBUG + static bool -tr_bitfieldIsValid (const tr_bitfield * b UNUSED) +tr_bitfieldIsValid (const tr_bitfield * b) { assert (b != NULL); assert ((b->alloc_count == 0) == (b->bits == 0)); @@ -156,6 +158,8 @@ tr_bitfieldIsValid (const tr_bitfield * b UNUSED) return true; } +#endif + size_t tr_bitfieldCountTrueBits (const tr_bitfield * b) { diff --git a/libtransmission/cache.c b/libtransmission/cache.c index 909cffd1d..0e18afd74 100644 --- a/libtransmission/cache.c +++ b/libtransmission/cache.c @@ -453,7 +453,7 @@ tr_cacheFlushFile (tr_cache * cache, tr_torrent * torrent, tr_file_index_t i) err = flushContiguous (cache, pos, getBlockRun (cache, pos, NULL)); } - return err; + return err; } int diff --git a/libtransmission/natpmp.c b/libtransmission/natpmp.c index 1e8813048..d5ec7c343 100644 --- a/libtransmission/natpmp.c +++ b/libtransmission/natpmp.c @@ -98,7 +98,7 @@ tr_natpmpClose (tr_natpmp * nat) } } -static int +static bool canSendCommand (const struct tr_natpmp * nat) { return tr_time () >= nat->command_time; diff --git a/libtransmission/platform.c b/libtransmission/platform.c index f4f9ca0ce..ec700f271 100644 --- a/libtransmission/platform.c +++ b/libtransmission/platform.c @@ -192,7 +192,7 @@ tr_lockLock (tr_lock * l) ++l->depth; } -int +bool tr_lockHave (const tr_lock * l) { return (l->depth > 0) && diff --git a/libtransmission/platform.h b/libtransmission/platform.h index f1f112ebc..b9abd1b73 100644 --- a/libtransmission/platform.h +++ b/libtransmission/platform.h @@ -75,7 +75,7 @@ void tr_lockLock (tr_lock *); void tr_lockUnlock (tr_lock *); /** @brief return nonzero if the specified lock is locked */ -int tr_lockHave (const tr_lock *); +bool tr_lockHave (const tr_lock *); /* @} */ diff --git a/libtransmission/resume.c b/libtransmission/resume.c index 29902454f..6a8b52bbc 100644 --- a/libtransmission/resume.c +++ b/libtransmission/resume.c @@ -77,7 +77,7 @@ addPeers (tr_torrent * tor, const uint8_t * buf, int buflen) } } - return numAdded; + return numAdded; } @@ -872,7 +872,7 @@ setFromCtor (tr_torrent * tor, uint64_t fields, const tr_ctor * ctor, int mode) { bool isPaused; - if (!tr_ctorGetPaused (ctor, mode, &isPaused)) + if (tr_ctorGetPaused (ctor, mode, &isPaused)) { tor->isRunning = !isPaused; ret |= TR_FR_RUN; @@ -880,14 +880,14 @@ setFromCtor (tr_torrent * tor, uint64_t fields, const tr_ctor * ctor, int mode) } if (fields & TR_FR_MAX_PEERS) - if (!tr_ctorGetPeerLimit (ctor, mode, &tor->maxConnectedPeers)) + if (tr_ctorGetPeerLimit (ctor, mode, &tor->maxConnectedPeers)) ret |= TR_FR_MAX_PEERS; if (fields & TR_FR_DOWNLOAD_DIR) { const char * path; - if (!tr_ctorGetDownloadDir (ctor, mode, &path) && path && *path) + if (tr_ctorGetDownloadDir (ctor, mode, &path) && path && *path) { ret |= TR_FR_DOWNLOAD_DIR; tr_free (tor->downloadDir); diff --git a/libtransmission/torrent-ctor.c b/libtransmission/torrent-ctor.c index 21b18fa35..134b37503 100644 --- a/libtransmission/torrent-ctor.c +++ b/libtransmission/torrent-ctor.c @@ -258,17 +258,17 @@ tr_ctorSetDeleteSource (tr_ctor * ctor, bool deleteSource) ctor->isSet_delete = true; } -int +bool tr_ctorGetDeleteSource (const tr_ctor * ctor, bool * setme) { - int err = 0; + bool ret = true; if (!ctor->isSet_delete) - err = 1; + ret = false; else if (setme) - *setme = ctor->doDelete ? 1 : 0; + *setme = ctor->doDelete; - return err; + return ret; } /*** @@ -283,7 +283,7 @@ tr_ctorSetSave (tr_ctor * ctor, bool saveInOurTorrentsDir) ctor->saveInOurTorrentsDir = saveInOurTorrentsDir; } -int +bool tr_ctorGetSave (const tr_ctor * ctor) { return ctor && ctor->saveInOurTorrentsDir; @@ -349,78 +349,78 @@ tr_ctorSetIncompleteDir (tr_ctor * ctor, const char * directory) ctor->incompleteDir = tr_strdup (directory); } -int +bool tr_ctorGetPeerLimit (const tr_ctor * ctor, tr_ctorMode mode, uint16_t * setmeCount) { - int err = 0; + bool ret = true; const struct optional_args * args = &ctor->optionalArgs[mode]; if (!args->isSet_connected) - err = 1; + ret = false; else if (setmeCount) *setmeCount = args->peerLimit; - return err; + return ret; } -int +bool tr_ctorGetPaused (const tr_ctor * ctor, tr_ctorMode mode, bool * setmeIsPaused) { - int err = 0; + bool ret = true; const struct optional_args * args = &ctor->optionalArgs[mode]; if (!args->isSet_paused) - err = 1; + ret = false; else if (setmeIsPaused) *setmeIsPaused = args->isPaused; - return err; + return ret; } -int +bool tr_ctorGetDownloadDir (const tr_ctor * ctor, tr_ctorMode mode, const char ** setmeDownloadDir) { - int err = 0; + bool ret = true; const struct optional_args * args = &ctor->optionalArgs[mode]; if (!args->isSet_downloadDir) - err = 1; + ret = false; else if (setmeDownloadDir) *setmeDownloadDir = args->downloadDir; - return err; + return ret; } -int +bool tr_ctorGetIncompleteDir (const tr_ctor * ctor, const char ** setmeIncompleteDir) { - int err = 0; + bool ret = true; if (ctor->incompleteDir == NULL) - err = 1; + ret = false; else *setmeIncompleteDir = ctor->incompleteDir; - return err; + return ret; } -int +bool tr_ctorGetMetainfo (const tr_ctor * ctor, const tr_variant ** setme) { - int err = 0; + bool ret = true; if (!ctor->isSet_metainfo) - err = 1; + ret = false; else if (setme) *setme = &ctor->metainfo; - return err; + return ret; } tr_session* diff --git a/libtransmission/torrent-magnet.c b/libtransmission/torrent-magnet.c index 8b40bc1d2..a2d0619c3 100644 --- a/libtransmission/torrent-magnet.c +++ b/libtransmission/torrent-magnet.c @@ -192,9 +192,9 @@ tr_torrentGetMetadataPiece (tr_torrent * tor, int piece, int * len) } } - assert (ret == NULL || *len > 0); + assert (ret == NULL || *len > 0); - return ret; + return ret; } void diff --git a/libtransmission/torrent.c b/libtransmission/torrent.c index 35d5217ee..eb0e27d38 100644 --- a/libtransmission/torrent.c +++ b/libtransmission/torrent.c @@ -632,7 +632,7 @@ initFilePieces (tr_info * info, file->lastPiece = getBytePiece (info, lastByte); } -static int +static bool pieceHasFile (tr_piece_index_t piece, const tr_file * file) { @@ -872,11 +872,11 @@ torrentInit (tr_torrent * tor, const tr_ctor * ctor) tor->info.hash, SHA_DIGEST_LENGTH, NULL); - if (!tr_ctorGetDownloadDir (ctor, TR_FORCE, &dir) || - !tr_ctorGetDownloadDir (ctor, TR_FALLBACK, &dir)) - tor->downloadDir = tr_strdup (dir); + if (tr_ctorGetDownloadDir (ctor, TR_FORCE, &dir) || + tr_ctorGetDownloadDir (ctor, TR_FALLBACK, &dir)) + tor->downloadDir = tr_strdup (dir); - if (tr_ctorGetIncompleteDir (ctor, &dir)) + if (!tr_ctorGetIncompleteDir (ctor, &dir)) dir = tr_sessionGetIncompleteDir (session); if (tr_sessionIsIncompleteDirEnabled (session)) tor->incompleteDir = tr_strdup (dir); @@ -952,7 +952,7 @@ torrentInit (tr_torrent * tor, const tr_ctor * ctor) if (tr_ctorGetSave (ctor)) { const tr_variant * val; - if (!tr_ctorGetMetainfo (ctor, &val)) + if (tr_ctorGetMetainfo (ctor, &val)) { const char * path = tor->info.torrent; const int err = tr_variantToFile (val, TR_VARIANT_FMT_BENC, path); @@ -996,7 +996,7 @@ torrentParseImpl (const tr_ctor * ctor, setmeInfo = &tmp; memset (setmeInfo, 0, sizeof (tr_info)); - if (tr_ctorGetMetainfo (ctor, &metainfo)) + if (!tr_ctorGetMetainfo (ctor, &metainfo)) return TR_PARSE_ERR; didParse = tr_metainfoParse (session, metainfo, setmeInfo, diff --git a/libtransmission/torrent.h b/libtransmission/torrent.h index 60a4c0839..c4e8d77b9 100644 --- a/libtransmission/torrent.h +++ b/libtransmission/torrent.h @@ -31,7 +31,7 @@ void tr_torrentFree (tr_torrent * tor); void tr_ctorSetSave (tr_ctor * ctor, bool saveMetadataInOurTorrentsDir); -int tr_ctorGetSave (const tr_ctor * ctor); +bool tr_ctorGetSave (const tr_ctor * ctor); void tr_ctorInitTorrentPriorities (const tr_ctor * ctor, tr_torrent * tor); diff --git a/libtransmission/tr-dht.c b/libtransmission/tr-dht.c index 3bb665cc6..305de71d9 100644 --- a/libtransmission/tr-dht.c +++ b/libtransmission/tr-dht.c @@ -77,7 +77,7 @@ struct bootstrap_closure { size_t len, len6; }; -static int +static bool bootstrap_done (tr_session *session, int af) { int status; @@ -460,7 +460,7 @@ tr_dhtPort (tr_session *ss) return tr_dhtEnabled (ss) ? ss->udp_port : 0; } -int +bool tr_dhtAddNode (tr_session * ss, const tr_address * address, tr_port port, @@ -469,14 +469,14 @@ tr_dhtAddNode (tr_session * ss, int af = address->type == TR_AF_INET ? AF_INET : AF_INET6; if (!tr_dhtEnabled (ss)) - return 0; + return false; /* Since we don't want to abuse our bootstrap nodes, * we don't ping them if the DHT is in a good state. */ if (bootstrap) { if (tr_dhtStatus (ss, af, NULL) >= TR_DHT_FIREWALLED) - return 0; + return false; } if (address->type == TR_AF_INET) { @@ -486,7 +486,7 @@ tr_dhtAddNode (tr_session * ss, memcpy (&sin.sin_addr, &address->addr.addr4, 4); sin.sin_port = htons (port); dht_ping_node ((struct sockaddr*)&sin, sizeof (sin)); - return 1; + return true; } else if (address->type == TR_AF_INET6) { struct sockaddr_in6 sin6; memset (&sin6, 0, sizeof (sin6)); @@ -494,10 +494,10 @@ tr_dhtAddNode (tr_session * ss, memcpy (&sin6.sin6_addr, &address->addr.addr6, 16); sin6.sin6_port = htons (port); dht_ping_node ((struct sockaddr*)&sin6, sizeof (sin6)); - return 1; + return true; } - return 0; + return false; } const char * diff --git a/libtransmission/tr-dht.h b/libtransmission/tr-dht.h index 7a28e3cc6..a6b101264 100644 --- a/libtransmission/tr-dht.h +++ b/libtransmission/tr-dht.h @@ -42,7 +42,7 @@ bool tr_dhtEnabled (const tr_session *); tr_port tr_dhtPort (tr_session *); int tr_dhtStatus (tr_session *, int af, int * setme_nodeCount); const char *tr_dhtPrintableStatus (int status); -int tr_dhtAddNode (tr_session *, const tr_address *, tr_port, bool bootstrap); +bool tr_dhtAddNode (tr_session *, const tr_address *, tr_port, bool bootstrap); void tr_dhtUpkeep (tr_session *); void tr_dhtCallback (unsigned char *buf, int buflen, struct sockaddr *from, socklen_t fromlen, diff --git a/libtransmission/tr-lpd.c b/libtransmission/tr-lpd.c index 0af708ea8..09733f42c 100644 --- a/libtransmission/tr-lpd.c +++ b/libtransmission/tr-lpd.c @@ -211,7 +211,7 @@ static const char* lpd_extractHeader (const char* s, struct lpd_protocolVersion* * - assemble search string "\r\nName: " and locate position * - copy back value from end to next "\r\n" */ -static int lpd_extractParam (const char* const str, const char* const name, int n, char* const val) +static bool lpd_extractParam (const char* const str, const char* const name, int n, char* const val) { /* configure maximum length of search string here */ enum { maxLength = 30 }; @@ -222,14 +222,14 @@ static int lpd_extractParam (const char* const str, const char* const name, int assert (val != NULL); if (strlen (name) > maxLength - strlen (CRLF ": ")) - return 0; + return false; /* compose the string token to search for */ tr_snprintf (sstr, maxLength, CRLF "%s: ", name); pos = strstr (str, sstr); if (pos == NULL) - return 0; /* search was not successful */ + return false; /* search was not successful */ { const char* const beg = pos + strlen (sstr); @@ -248,7 +248,7 @@ static int lpd_extractParam (const char* const str, const char* const name, int } /* we successfully returned the value string */ - return 1; + return true; } /** @@ -512,7 +512,7 @@ static int tr_lpdConsiderAnnounce (tr_pex* peer, const char* const msg) /* save the effort to check Host, which seems to be optional anyway */ - if (lpd_extractParam (params, "Port", maxValueLen, value) == 0) + if (!lpd_extractParam (params, "Port", maxValueLen, value)) return 0; /* determine announced peer port, refuse if value too large */ @@ -522,7 +522,7 @@ static int tr_lpdConsiderAnnounce (tr_pex* peer, const char* const msg) peer->port = htons (peerPort); res = -1; /* signal caller side-effect to peer->port via return != 0 */ - if (lpd_extractParam (params, "Infohash", maxHashLen, hashString) == 0) + if (!lpd_extractParam (params, "Infohash", maxHashLen, hashString)) return res; tor = tr_torrentFindFromHashString (session, hashString); diff --git a/libtransmission/transmission.h b/libtransmission/transmission.h index 91e8d67eb..95492fe29 100644 --- a/libtransmission/transmission.h +++ b/libtransmission/transmission.h @@ -1011,30 +1011,30 @@ void tr_ctorSetFilesWanted (tr_ctor * ctor, /** @brief Get this peer constructor's peer limit */ -int tr_ctorGetPeerLimit (const tr_ctor * ctor, +bool tr_ctorGetPeerLimit (const tr_ctor * ctor, tr_ctorMode mode, uint16_t * setmeCount); /** @brief Get the "isPaused" flag from this peer constructor */ -int tr_ctorGetPaused (const tr_ctor * ctor, +bool tr_ctorGetPaused (const tr_ctor * ctor, tr_ctorMode mode, bool * setmeIsPaused); /** @brief Get the download path from this peer constructor */ -int tr_ctorGetDownloadDir (const tr_ctor * ctor, +bool tr_ctorGetDownloadDir (const tr_ctor * ctor, tr_ctorMode mode, const char ** setmeDownloadDir); /** @brief Get the incomplete directory from this peer constructor */ -int tr_ctorGetIncompleteDir (const tr_ctor * ctor, +bool tr_ctorGetIncompleteDir (const tr_ctor * ctor, const char ** setmeIncompleteDir); /** @brief Get the metainfo from this peer constructor */ -int tr_ctorGetMetainfo (const tr_ctor * ctor, +bool tr_ctorGetMetainfo (const tr_ctor * ctor, const struct tr_variant ** setme); /** @brief Get the "delete .torrent file" flag from this peer constructor */ -int tr_ctorGetDeleteSource (const tr_ctor * ctor, +bool tr_ctorGetDeleteSource (const tr_ctor * ctor, bool * setmeDoDelete); /** @brief Get the tr_session poiner from this peer constructor */ diff --git a/libtransmission/web.c b/libtransmission/web.c index 6e6eca8d0..0572b14e3 100644 --- a/libtransmission/web.c +++ b/libtransmission/web.c @@ -638,7 +638,7 @@ tr_http_unescape (const char * str, int len) return ret; } -static int +static bool is_rfc2396_alnum (uint8_t ch) { return ('0' <= ch && ch <= '9') -- 2.40.0