//## int System.select(int[] readsock, int[] writesock, int[] exceptsock, long timeoutSec, long timeoutUSec); static KMETHOD System_Select(KonohaContext *kctx, KonohaStack* sfp) { kArray *a1 = sfp[1].asArray; kArray *a2 = sfp[2].asArray; kArray *a3 = sfp[3].asArray; int nfd = getNfd(a1, a2, a3 ); fd_set rfds, wfds, efds; fd_set *rfd = toFd(&rfds, a1 ); fd_set *wfd = toFd(&wfds, a2 ); fd_set *efd = toFd(&efds, a3 ); struct timeval tv; tv.tv_sec = (long)sfp[4].intValue; tv.tv_usec = (long)sfp[5].intValue; int ret = select(nfd+1, rfd, wfd, efd, &tv ); if(ret > 0) { fromFd(kctx, rfd, a1 ); fromFd(kctx, wfd, a2 ); fromFd(kctx, efd, a3 ); } else { if(ret < 0 ) { OLDTRACE_SWITCH_TO_KTrace(_SystemFault, LogText("@", "select"), LogText("perror", strerror(errno)) ); } // TODO::error or timeout is socket list all clear [pending] KLIB kArray_Clear(kctx, a1, 0); KLIB kArray_Clear(kctx, a2, 0); KLIB kArray_Clear(kctx, a3, 0); } KReturnUnboxValue(ret); }
int copyFile(const char *fromPath, const char *toPath) { auto start = std::chrono::steady_clock::now(); android::base::unique_fd fromFd(open(fromPath, O_RDONLY)); if (fromFd == -1) { PLOG(ERROR) << "Failed to open copy from " << fromPath; return -1; } android::base::unique_fd toFd(open(toPath, O_CREAT | O_WRONLY, FILE_PERM)); if (toFd == -1) { PLOG(ERROR) << "Failed to open copy to " << toPath; return -1; } off_t offset = 0; struct stat sstat = {}; if (stat(fromPath, &sstat) == -1) return -1; off_t length = sstat.st_size; int ret = 0; while (offset < length) { ssize_t transfer_length = std::min(length - offset, (off_t) FILE_COPY_SIZE); ret = sendfile(toFd, fromFd, &offset, transfer_length); if (ret != transfer_length) { ret = -1; PLOG(ERROR) << "Copying failed!"; break; } } auto end = std::chrono::steady_clock::now(); std::chrono::duration<double> diff = end - start; LOG(DEBUG) << "Copied a file with MTP. Time: " << diff.count() << " s, Size: " << length << ", Rate: " << ((double) length) / diff.count() << " bytes/s"; chown(toPath, getuid(), FILE_GROUP); return ret == -1 ? -1 : 0; }