static tr_watchdir_status onFileAdded(tr_watchdir_t dir, char const* name, void* context) { tr_session* session = context; if (!tr_str_has_suffix(name, ".torrent")) { return TR_WATCHDIR_IGNORE; } char* filename = tr_buildPath(tr_watchdir_get_path(dir), name, NULL); tr_ctor* ctor = tr_ctorNew(session); int err = tr_ctorSetMetainfoFromFile(ctor, filename); if (err == 0) { tr_torrentNew(ctor, &err, NULL); if (err == TR_PARSE_ERR) { tr_logAddError("Error parsing .torrent file \"%s\"", name); } else { bool trash = false; bool const test = tr_ctorGetDeleteSource(ctor, &trash); tr_logAddInfo("Parsing .torrent file successful \"%s\"", name); if (test && trash) { tr_error* error = NULL; tr_logAddInfo("Deleting input .torrent file \"%s\"", name); if (!tr_sys_path_remove(filename, &error)) { tr_logAddError("Error deleting .torrent file: %s", error->message); tr_error_free(error); } } else { char* new_filename = tr_strdup_printf("%s.added", filename); tr_sys_path_rename(filename, new_filename, NULL); tr_free(new_filename); } } } else { err = TR_PARSE_ERR; } tr_ctorFree(ctor); tr_free(filename); return err == TR_PARSE_ERR ? TR_WATCHDIR_RETRY : TR_WATCHDIR_ACCEPT; }
static void watchdir_new_impl (dtr_watchdir * w) { int i; DIR * odir; w->inotify_fd = inotify_init (); if (w->inotify_fd < 0) { i = -1; } else { tr_inf ("Using inotify to watch directory \"%s\"", w->dir); i = inotify_add_watch (w->inotify_fd, w->dir, DTR_INOTIFY_MASK); } if (i < 0) { tr_err ("Unable to watch \"%s\": %s", w->dir, tr_strerror (errno)); } else if ((odir = opendir (w->dir))) { struct dirent * d; while ((d = readdir (odir))) { const char * name = d->d_name; if (!tr_str_has_suffix (name, ".torrent")) /* skip non-torrents */ continue; tr_inf ("Found new .torrent file \"%s\" in watchdir \"%s\"", name, w->dir); w->callback (w->session, w->dir, name); } closedir (odir); } }
static void watchdir_update_impl (dtr_watchdir * w) { int ret; fd_set rfds; struct timeval time; const int fd = w->inotify_fd; /* timeout after one second */ time.tv_sec = 1; time.tv_usec = 0; /* make the fd_set hold the inotify fd */ FD_ZERO (&rfds); FD_SET (fd, &rfds); /* check for added files */ ret = select (fd+1, &rfds, NULL, NULL, &time); if (ret < 0) { perror ("select"); } else if (!ret) { /* timed out! */ } else if (FD_ISSET (fd, &rfds)) { int i = 0; char buf[BUF_LEN]; int len = read (fd, buf, sizeof (buf)); while (i < len) { struct inotify_event * event = (struct inotify_event *) &buf[i]; const char * name = event->name; if (tr_str_has_suffix (name, ".torrent")) { tr_inf ("Found new .torrent file \"%s\" in watchdir \"%s\"", name, w->dir); w->callback (w->session, w->dir, name); } i += EVENT_SIZE + event->len; } } }
static void watchdir_new_impl (dtr_watchdir * w) { int i; tr_sys_dir_t odir; w->inotify_fd = inotify_init (); if (w->inotify_fd < 0) { i = -1; } else { tr_logAddInfo ("Using inotify to watch directory \"%s\"", w->dir); i = inotify_add_watch (w->inotify_fd, w->dir, DTR_INOTIFY_MASK); } if (i < 0) { tr_logAddError ("Unable to watch \"%s\": %s", w->dir, tr_strerror (errno)); } else if ((odir = tr_sys_dir_open (w->dir, NULL)) != TR_BAD_SYS_DIR) { const char * name; while ((name = tr_sys_dir_read_name (odir, NULL)) != NULL) { if (!tr_str_has_suffix (name, ".torrent")) /* skip non-torrents */ continue; tr_logAddInfo ("Found new .torrent file \"%s\" in watchdir \"%s\"", name, w->dir); w->callback (w->session, w->dir, name); } tr_sys_dir_close (odir, NULL); } }