static void monitor_home(csiebox_client* client) { fd_set master, readfds; FD_ZERO(&master); FD_SET(client->conn_fd, &master); FD_SET(client->inotify_fd, &master); int maxfd = client->conn_fd; if (client->inotify_fd > maxfd) maxfd = client->inotify_fd; while (1) { memcpy(&readfds, &master, sizeof(fd_set)); select(maxfd+1, &readfds, NULL, NULL, NULL); if (FD_ISSET(client->conn_fd, &readfds)) download(client); if (FD_ISSET(client->inotify_fd, &readfds)) handle_inotify(client); } }
static int prepare_and_sync(csiebox_client* client) { char* cwd = (char*)malloc(sizeof(char) * PATH_MAX); memset(cwd, 0, sizeof(cwd)); if (getcwd(cwd, PATH_MAX) == 0) { fprintf(stderr, "getcwd fail\n"); fprintf(stderr, "code: %s\n", strerror(errno)); free(cwd); return 0; } if (chdir(client->arg.path) != 0) { fprintf(stderr, "invalid client path\n"); free(cwd); return 0; } max_level = 0; char* longest_path = (char*)malloc(sizeof(char) * PATH_MAX); sync_all(client, longest_path, 0); // create longestPath.txt FILE *fp = fopen("longestPath.txt", "w+"); int i = 0, len = strlen(longest_path); for (; i<len-1; i++) { longest_path[i] = longest_path[i+1]; } longest_path[len-1] = 0; fwrite(longest_path, 1, strlen(longest_path), fp); fclose(fp); free(longest_path); handle_inotify(client); // send sync end message csiebox_protocol_header header; memset(&header, 0, sizeof(header)); header.req.magic = CSIEBOX_PROTOCOL_MAGIC_REQ; header.req.op = CSIEBOX_PROTOCOL_OP_SYNC_END; header.req.client_id = client->client_id; send_message(client->conn_fd, &header, sizeof(header)); chdir(cwd); free(cwd); return 1; }
int main (int argc, char *argv[]) { int sock, conn; char *sockname; struct sockaddr_un addr, peer; socklen_t peer_sz; fd_set rfds; int ret; int maxfd; read_ignore_file(); ifd = inotify_init(); if (ifd < 0) die_errno("inotify_init"); setup_watches(expanduser("~")); setup_watches("/media"); sockname = expanduser(SOCKNAME); unlink(sockname); /* errors deliberately ignored */ sock = socket(AF_UNIX, SOCK_STREAM, 0); if (sock < 0) die_errno("socket"); memset(&addr, 0, sizeof(addr)); addr.sun_family = AF_UNIX; strncpy(addr.sun_path, sockname, sizeof(addr.sun_path)-1); if (bind(sock, (struct sockaddr *) &addr, sizeof(addr))) die_errno("bind"); if (listen(sock, 10)) die_errno("listen"); maxfd = sock; if (ifd > maxfd) maxfd = ifd; maxfd++; while (1) { FD_ZERO(&rfds); FD_SET(ifd, &rfds); FD_SET(sock, &rfds); ret = select(maxfd, &rfds, NULL, NULL, NULL); if (ret == -1) die_errno("select"); if (!ret) continue; if (FD_ISSET(ifd, &rfds)) handle_inotify(); if (FD_ISSET(sock, &rfds)) { conn = accept(sock, (struct sockaddr *) &peer, &peer_sz); if (conn < 0) die_errno("accept"); send_lru(conn); close(conn); /* errors ignored */ } } /* we never get here, but meh */ if (close(sock) < 0) die_errno("close"); return 0; }
static void monitor_home(csiebox_client* client) { while (1) { handle_inotify(client); } }