/** * 打开文件. * @param fn 文件路径 * @param flags as in open() * @param ... as in open() * @return 文件描述符 */ int AnalogFS::open(const char *fn, int flags, ...) { char tpath[MAX_PATHLEN]; va_list ap; char *tfn; int fd; strcpy(tpath, path); mergepath(tpath, fn); va_start(ap, flags); if ((flags & O_ACCMODE) == O_WRONLY) { tfn = assert_filename_inexist(tpath); if ((fd = ::open(tfn, flags, va_arg(ap, mode_t))) == -1) pwarning(_("Open() file \"%s\" failed, %s"), tfn, strerror(errno)); g_free(tfn); } else { if ((fd = ::open(tpath, flags, va_arg(ap, mode_t))) == -1) pwarning(_("Open() file \"%s\" failed, %s"), tpath, strerror(errno)); } va_end(ap); return fd; }
/** * 获取目录总大小. * @param dir_name 目录路径 * @return 目录大小 */ int64_t AnalogFS::ftwsize(const char *dir_name) { DIR* dir = opendir(dir_name); if(dir == NULL) { pwarning(_("opendir on \"%s\" failed: %s"), dir_name, strerror(errno)); return 0; } struct dirent *dirt; int64_t sumsize = 0; while(dirt = readdir(dir)) { if(strcmp(dirt->d_name, ".") == 0) { continue; } if(strcmp(dirt->d_name, "..") == 0) { continue; } char tpath[MAX_PATHLEN]; strcpy(tpath, dir_name); mergepath(tpath, dirt->d_name); struct stat64 st; if(stat64(tpath, &st) == -1) { continue; } if(S_ISDIR(st.st_mode)) { sumsize += ftwsize(tpath); } else if(S_ISREG(st.st_mode)) { sumsize += st.st_size; } else { // ignore other files } } return sumsize; }
/** * 获取文件状态. * @param fn 文件路径 * @param st a stat64 struct * @return 成功与否 */ int AnalogFS::stat(const char *fn, struct stat64 *st) { char tpath[MAX_PATHLEN]; int result; strcpy(tpath, path); mergepath(tpath, fn); if ((result = ::stat64(tpath, st)) != 0) pwarning(_("Stat64() file \"%s\" failed, %s"), tpath, strerror(errno)); return result; }
/** * 错误响应处理函数. */ void SoundSystem::ErrorMessageOccur(SoundSystem *sndsys, GstMessage *message) { GstElement *pipeline; GError *error; gst_message_parse_error(message, &error, NULL); pwarning(_("Failed to play the prompt tone, %s\n"), error->message); g_error_free(error); EosMessageOccur(sndsys); pipeline = GST_ELEMENT(g_datalist_get_data(&sndsys->eltset, "pipeline-element")); gst_element_set_state(pipeline, GST_STATE_NULL); }
/** * 打开目录. * @param dir 目录路径 * @return DIR */ DIR *AnalogFS::opendir(const char *dir) { char tpath[MAX_PATHLEN]; DIR *dirs; strcpy(tpath, path); mergepath(tpath, dir); if (!(dirs = ::opendir(tpath))) pwarning(_("Opendir() directory \"%s\" failed, %s"), tpath, strerror(errno)); return dirs; }
/** * 创建新的目录. * @param dir 目录路径 * @param mode as in mkdir() * @return 成功与否 */ int AnalogFS::mkdir(const char *dir, mode_t mode) { char tpath[MAX_PATHLEN]; int result; strcpy(tpath, path); mergepath(tpath, dir); if (::access(tpath, F_OK) == 0) return 0; if ((result = ::mkdir(tpath, mode)) != 0) pwarning(_("Mkdir() directory \"%s\" failed, %s"), tpath, strerror(errno)); return result; }
/** * 打开URL. * @param url url */ void iptux_open_url(const char *url) { int fd; if (fork() != 0) return; /* 关闭由iptux打开的所有可能的文件描述符 */ fd = 3; while (fd < FD_SETSIZE) { close(fd); fd++; } /* 脱离终端控制 */ setsid(); /* 打开URL */ execlp("xdg-open", "xdg-open", url, NULL); /* 测试系统中所有可能被安装的浏览器 */ execlp("firefox", "firefox", url, NULL); execlp("opera", "opera", url, NULL); execlp("konqueror", "konqueror", url, NULL); pwarning(_("Can't find any available web browser!\n")); }
int EventInit(char *server) { char buf[BUFSIZ], ipbuf[BUFSIZ]; struct hostent *he; struct in_addr myip; char *ipaddr; address_tuple_t tuple; if (server == NULL) { warning("no event server specified"); return 1; } if (gethostname(buf, sizeof(buf)) < 0) { pwarning("could not get hostname"); return 1; } if ((he = gethostbyname(buf)) == NULL) { warning("could not get IP address from hostname"); return 1; } memcpy((char *)&myip, he->h_addr, he->h_length); strcpy(ipbuf, inet_ntoa(myip)); ipaddr = ipbuf; /* * Hideous Hack Alert! * * Assume our hostname is of the form 'c-<num>.<domain>' * and use <num> to determine our client number. * We use that number later to compare to the MAXCLIENTS * field to determine if we should process an event. */ if (sscanf(buf, "c-%d.", &clientnum) != 1) { warning("could not determine client number from hostname %s", buf); return 1; } else if (debug) log("client number %d for event handling", clientnum); /* * Convert server/port to elvin thing. */ snprintf(buf, sizeof(buf), "elvin://%s", server); server = buf; /* * Construct an address tuple for subscribing to events for * this node. */ tuple = address_tuple_alloc(); if (tuple == NULL) { warning("could not allocate an address tuple"); return 1; } tuple->host = ADDRESSTUPLE_ANY; /* ipaddr; */ tuple->site = ADDRESSTUPLE_ANY; tuple->group = ADDRESSTUPLE_ANY; tuple->expt = ADDRESSTUPLE_ANY; tuple->objtype = TBDB_OBJECTTYPE_FRISBEE; tuple->objname = ADDRESSTUPLE_ANY; tuple->eventtype = ADDRESSTUPLE_ANY; /* * Register with the event system. */ ehandle = event_register(server, 0); if (ehandle == NULL) { warning("could not register with event system"); address_tuple_free(tuple); return 1; } /* * Subscribe to the event we specified above. */ if (!event_subscribe(ehandle, callback, tuple, NULL)) { warning("could not subscribe to FRISBEE events"); address_tuple_free(tuple); return 1; } address_tuple_free(tuple); return 0; }
/* * XXX multithread (socket read, file write) */ static int recv_file() { /* volatiles are for setjmp/longjmp */ conn * volatile conn = NULL; char * volatile wbuf = NULL; volatile int fd = -1; volatile uint64_t remaining = maxsize; struct timeval st, et; int rv = 1; char *stat; if (timeout > 0) { struct itimerval it; if (sigsetjmp(toenv, 1)) { rv = 2; goto done; } it.it_value.tv_sec = timeout; it.it_value.tv_usec = 0; it.it_interval = it.it_value; signal(SIGALRM, send_timeout); setitimer(ITIMER_REAL, &it, NULL); } wbuf = malloc(bufsize); if (wbuf == NULL) { error("Could not allocate %d byte buffer, try using -b", bufsize); goto done; } if (strcmp(path, "-") == 0) fd = STDOUT_FILENO; else fd = open(path, O_WRONLY|O_CREAT, 0644); if (fd < 0) { pwarning(path); goto done; } conn = conn_accept_tcp(sock, &clientip); if (conn == NULL) { error("Error accepting from %s", inet_ntoa(clientip)); goto done; } log("%s: upload from %s started", path, inet_ntoa(clientip)); gettimeofday(&st, NULL); while (maxsize == 0 || remaining > 0) { ssize_t cc, ncc; if (maxsize == 0 || remaining > bufsize) cc = bufsize; else cc = remaining; ncc = conn_read(conn, wbuf, cc); if (ncc < 0) { pwarning("socket read"); goto done; } if (ncc == 0) break; cc = write(fd, wbuf, ncc); if (cc < 0) { pwarning("file write"); goto done; } remaining -= cc; if (cc != ncc) { error("short write on file (%d != %d)", cc, ncc); goto done; } } /* Note that coming up short (remaining > 0) is not an error */ rv = 0; done: gettimeofday(&et, NULL); if (timeout) { struct itimerval it; it.it_value.tv_sec = 0; it.it_value.tv_usec = 0; setitimer(ITIMER_REAL, &it, NULL); } if (conn != NULL) conn_close(conn); if (fd >= 0) { if (rv == 0 && fsync(fd) != 0) { perror(path); rv = 1; } close(fd); } if (wbuf != NULL) free(wbuf); timersub(&et, &st, &et); stat = rv == 0 ? "completed" : (rv == 1 ? "terminated" : "timed-out"); if (maxsize && remaining) log("%s: upload %s after %llu (of max %llu) bytes " "in %d.%03d seconds", path, stat, maxsize-remaining, maxsize, et.tv_sec, et.tv_usec/1000); else log("%s: upload %s after %llu bytes in %d.%03d seconds", path, stat, maxsize-remaining, et.tv_sec, et.tv_usec/1000); /* XXX unlink file on error? */ return rv; }
/* * Create sockets on specified port. */ static int makesockets(int *portnum, int *udpsockp, int *tcpsockp) { struct sockaddr_in name; socklen_t length; int i, udpsock, tcpsock; /* * Setup TCP socket for incoming connections. */ /* Create socket from which to read. */ tcpsock = socket(AF_INET, SOCK_STREAM, 0); if (tcpsock < 0) { pfatal("opening stream socket"); } i = 1; if (setsockopt(tcpsock, SOL_SOCKET, SO_REUSEADDR, (char *)&i, sizeof(i)) < 0) pwarning("setsockopt(SO_REUSEADDR)");; /* Create name. */ name.sin_family = AF_INET; name.sin_addr.s_addr = INADDR_ANY; /* Start with default portnumber, and then retry if taken. */ if (*portnum == 0) { name.sin_port = htons((u_short) SERVER_PORTNUM); if (! bind(tcpsock, (struct sockaddr *) &name, sizeof(name))) { /* Got the default port */ goto bound; } if (errno != EADDRINUSE) pfatal("binding stream socket"); /* Retry below with portnum=0 */ } name.sin_port = htons((u_short) *portnum); if (bind(tcpsock, (struct sockaddr *) &name, sizeof(name))) { pfatal("binding stream socket"); } bound: /* Find assigned port value and print it out. */ length = sizeof(name); if (getsockname(tcpsock, (struct sockaddr *) &name, &length)) { pfatal("getsockname"); } if (listen(tcpsock, 128) < 0) { pfatal("listen"); } *portnum = ntohs(name.sin_port); info("listening on TCP port %d\n", *portnum); /* * Setup UDP socket */ /* Create socket from which to read. */ udpsock = socket(AF_INET, SOCK_DGRAM, 0); if (udpsock < 0) { pfatal("opening dgram socket"); } i = 1; if (setsockopt(udpsock, SOL_SOCKET, SO_REUSEADDR, (char *)&i, sizeof(i)) < 0) pwarning("setsockopt(SO_REUSEADDR)");; i = SOCKBUFSIZE; if (setsockopt(udpsock, SOL_SOCKET, SO_SNDBUF, &i, sizeof(i)) < 0) pwarning("Could not increase send socket buffer size to %d", SOCKBUFSIZE); i = SOCKBUFSIZE; if (setsockopt(udpsock, SOL_SOCKET, SO_RCVBUF, &i, sizeof(i)) < 0) pwarning("Could not increase recv socket buffer size to %d", SOCKBUFSIZE); /* Create name. */ name.sin_family = AF_INET; name.sin_addr.s_addr = INADDR_ANY; name.sin_port = htons((u_short) *portnum); if (bind(udpsock, (struct sockaddr *) &name, sizeof(name))) { pfatal("binding dgram socket"); } /* Find assigned port value and print it out. */ length = sizeof(name); if (getsockname(udpsock, (struct sockaddr *) &name, &length)) { pfatal("getsockname"); } info("listening on UDP port %d\n", ntohs(name.sin_port)); *tcpsockp = tcpsock; *udpsockp = udpsock; return 0; }