Skip to content

Commit

Permalink
is_driftnet_file: Refactor file name checking code (import from Debian)
Browse files Browse the repository at this point in the history
  • Loading branch information
rbu committed Mar 23, 2009
1 parent 95ce296 commit 02b3985
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 3 deletions.
4 changes: 1 addition & 3 deletions driftnet.c
Original file line number Diff line number Diff line change
Expand Up @@ -89,9 +89,7 @@ void clean_temporary_directory(void) {
buf = xmalloc(buflen = strlen(tmpdir) + 64);

while ((de = readdir(d))) {
char *p;
p = strrchr(de->d_name, '.');
if (!tmpdir_specified || (p && strncmp(de->d_name, "driftnet-", 9) == 0 && (strcmp(p, ".jpeg") == 0 || strcmp(p, ".gif") == 0 || strcmp(p, ".mp3") == 0))) {
if (!tmpdir_specified || is_driftnet_file(de->d_name)) {
if (buflen < strlen(tmpdir) + strlen(de->d_name) + 1)
buf = xrealloc(buf, buflen = strlen(tmpdir) + strlen(de->d_name) + 64);

Expand Down
1 change: 1 addition & 0 deletions driftnet.h
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ connection *find_connection(const struct in_addr *src, const struct in_addr *dst

/* media.c */
void connection_extract_media(connection c, const enum mediatype T);
int is_driftnet_file(char *filename);

/* util.c */
void *xmalloc(size_t n);
Expand Down
10 changes: 10 additions & 0 deletions media.c
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,16 @@ void dispatch_http_req(const char *mname, const unsigned char *data, const size_
/* playaudio.c */
void mpeg_submit_chunk(const unsigned char *data, const size_t len);

int is_driftnet_file(char *filename) {
if (strncmp(filename, "driftnet-", 9) != 0) return 0;
char *p = strrchr(filename, '.');
if (p == 0) return 0;
return (strcmp(p, ".jpeg") == 0 ||
strcmp(p, ".gif") == 0 ||
strcmp(p, ".png") == 0 ||
strcmp(p, ".mp3") == 0);
}

/* count_temporary_files:
* How many of our files remain in the temporary directory? We do this a
* maximum of once every five seconds. */
Expand Down

0 comments on commit 02b3985

Please sign in to comment.