int copy(char *dest, char *src) { FILE *in, *out; int ret; char *actualdest = dest; if(samefile(dest, src)){ eprintf("`%s' and `%s' are the same file", dest, src); return 1; } if(!(in = fopen(src, "r"))){ perrorf("open (for read): `%s'", src); return 1; } /* TODO: make dir if it doesn't exist */ if(!(out = fopen(dest, "w"))){ if(errno == EISDIR){ char *srcbase = strrchr(src, '/'); if(!srcbase) srcbase = src; actualdest = alloca(strlen(dest) + strlen(srcbase) + 2); sprintf(actualdest, "%s/%s", dest, srcbase); if(samefile(actualdest, src)){ eprintf("`%s' and `%s' are the same file", actualdest, src); fclose(in); return 1; } out = fopen(actualdest, "w"); if(!out){ perrorf("open (for write): `%s'", actualdest); fclose(in); return 1; } }else{ perrorf("open (for write): `%s'", dest); fclose(in); return 1; } } ret = filecopy(in, out, actualdest, filelen(src)); fclose(in); fclose(out); if(!i_am_cp && remove(src)) perrorf("non-fatal: remove: `%s'", src); return ret; }
int copyFiles(struct CopySource *h) { char *fnam, *ext, *dst; int differ, rc; if(destIsDir) { if(!dfnsplit(h->fnam, 0, 0, &fnam, &ext)) { error_out_of_memory(); return 0; } dst = dfnmerge(0, 0, destFile, fnam, ext); free(fnam); free(ext); if(!dst) { error_out_of_memory(); return 0; } } else dst = destFile; rc = 0; if((differ = samefile(h->fnam, dst)) < 0) error_out_of_memory(); else if(!differ) rc = copy(dst, h->fnam, h, 'w'); else if(h->app) rc = copy(dst, h->fnam, h->app, 'a'); else error_selfcopy(dst); if(destIsDir) free(dst); return rc; }
int main(int argc, char *argv[]) { struct stat putfd_1_stat, putfd_2_stat, getfd_1_stat, getfd_2_stat; int fd[2], putfd_1, putfd_2, getfd_1, getfd_2; const char *test; /* * First test: put a temporary file into a UNIX domain socket, then * take it out and make sure it's the same file. First time around, * don't close the reference after sending. */ test = "test1-simplesendfd"; printf("beginning %s\n", test); domainsocketpair(test, fd); tempfile(test, &putfd_1); dofstat(test, putfd_1, &putfd_1_stat); sendfd(test, fd[0], putfd_1); recvfd(test, fd[1], &getfd_1); dofstat(test, getfd_1, &getfd_1_stat); samefile(test, &putfd_1_stat, &getfd_1_stat); close(putfd_1); close(getfd_1); closesocketpair(fd); printf("%s passed\n", test); /* * Second test: same as first, only close the file reference after * sending, so that the only reference is the descriptor in the UNIX * domain socket buffer. */ test = "test2-sendandclose"; printf("beginning %s\n", test); domainsocketpair(test, fd); tempfile(test, &putfd_1); dofstat(test, putfd_1, &putfd_1_stat); sendfd(test, fd[0], putfd_1); close(putfd_1); recvfd(test, fd[1], &getfd_1); dofstat(test, getfd_1, &getfd_1_stat); samefile(test, &putfd_1_stat, &getfd_1_stat); close(getfd_1); closesocketpair(fd); printf("%s passed\n", test); /* * Third test: put a temporary file into a UNIX domain socket, then * close both endpoints causing garbage collection to kick off. */ test = "test3-sendandcancel"; printf("beginning %s\n", test); domainsocketpair(test, fd); tempfile(test, &putfd_1); sendfd(test, fd[0], putfd_1); close(putfd_1); closesocketpair(fd); printf("%s passed\n", test); /* * Send two files. Then receive them. Make sure they are returned * in the right order, and both get there. */ test = "test4-twofile"; printf("beginning %s\n", test); domainsocketpair(test, fd); tempfile(test, &putfd_1); tempfile(test, &putfd_2); dofstat(test, putfd_1, &putfd_1_stat); dofstat(test, putfd_2, &putfd_2_stat); sendfd(test, fd[0], putfd_1); sendfd(test, fd[0], putfd_2); close(putfd_1); close(putfd_2); recvfd(test, fd[1], &getfd_1); recvfd(test, fd[1], &getfd_2); dofstat(test, getfd_1, &getfd_1_stat); dofstat(test, getfd_2, &getfd_2_stat); samefile(test, &putfd_1_stat, &getfd_1_stat); samefile(test, &putfd_2_stat, &getfd_2_stat); close(getfd_1); close(getfd_2); closesocketpair(fd); printf("%s passed\n", test); /* * Big bundling test. Send an endpoint of the UNIX domain socket * over itself, closing the door behind it. */ test = "test5-bundle"; printf("beginning %s\n", test); domainsocketpair(test, fd); sendfd(test, fd[0], fd[0]); close(fd[0]); recvfd(test, fd[1], &getfd_1); close(getfd_1); close(fd[1]); printf("%s passed\n", test); /* * Big bundling test part two: Send an endpoint of the UNIX domain * socket over itself, close the door behind it, and never remove it * from the other end. */ test = "test6-bundlecancel"; printf("beginning %s\n", test); domainsocketpair(test, fd); sendfd(test, fd[0], fd[0]); sendfd(test, fd[1], fd[0]); closesocketpair(fd); printf("%s passed\n", test); /* * Test for PR 151758: Send an character device over the UNIX * domain socket and then close both sockets to orphan the * device. */ test = "test7-devfsorphan"; printf("beginning %s\n", test); domainsocketpair(test, fd); devnull(test, &putfd_1); sendfd(test, fd[0], putfd_1); close(putfd_1); closesocketpair(fd); printf("%s passed\n", test); return (0); }
void copy(char *from, char *to, int todir) { Dir *dirb, dirt; char name[256]; int fdf, fdt, mode; if(todir){ char *s, *elem; elem=s=from; while(*s++) if(s[-1]=='/') elem=s; sprint(name, "%s/%s", to, elem); to=name; } if((dirb=dirstat(from))==nil){ fprint(2,"fcp: can't stat %s: %r\n", from); failed = 1; return; } mode = dirb->mode; if(mode&DMDIR){ fprint(2, "fcp: %s is a directory\n", from); free(dirb); failed = 1; return; } if(samefile(dirb, from, to)){ free(dirb); failed = 1; return; } mode &= 0777; fdf=open(from, OREAD); if(fdf<0){ fprint(2, "fcp: can't open %s: %r\n", from); free(dirb); failed = 1; return; } fdt=create(to, OWRITE, mode); if(fdt<0){ fprint(2, "fcp: can't create %s: %r\n", to); close(fdf); free(dirb); failed = 1; return; } if(copy1(fdf, fdt, from, to)==0 && (xflag || gflag || uflag)){ nulldir(&dirt); if(xflag){ dirt.mtime = dirb->mtime; dirt.mode = dirb->mode; } if(uflag) dirt.uid = dirb->uid; if(gflag) dirt.gid = dirb->gid; if(dirfwstat(fdt, &dirt) < 0) fprint(2, "fcp: warning: can't wstat %s: %r\n", to); } free(dirb); close(fdf); close(fdt); }
int copy(char *dst, char *pattern, struct CopySource *src , int openMode) { char mode[3], *p; struct ffblk ff; struct CopySource *h; char *rDest, *rSrc; FILE *fin, *fout; int rc, asc; char *buf; size_t len; assert(dst); assert(pattern); assert(src); if(FINDFIRST(pattern, &ff, FA_RDONLY | FA_ARCH) != 0) { error_sfile_not_found(pattern); return 0; } mode[2] = '\0'; do { if((rDest = fillFnam(dst, ff.ff_name)) == 0) return 0; h = src; do { /* to prevent to open a source file for writing, e.g. for COPY *.c *.? */ if((rSrc = fillFnam(h->fnam, ff.ff_name)) == 0) { free(rDest); return 0; } rc = samefile(rDest, rSrc); free(rSrc); if(rc < 0) { error_out_of_memory(); free(rDest); return 0; } else if(rc) { error_selfcopy(rDest); free(rDest); return 0; } } while((h = h->app) != 0); if(interactive_command /* Suppress prompt if in batch file */ && openMode != 'a' && !optY && (fout = fopen(rDest, "rb")) != 0) { int destIsDevice = isadev(fileno(fout)); fclose(fout); if(!destIsDevice) { /* Devices do always exist */ switch(userprompt(PROMPT_OVERWRITE_FILE, rDest)) { default: /* Error */ case 4: /* Quit */ free(rDest); return 0; case 3: /* All */ optY = 1; case 1: /* Yes */ break; case 2: /* No */ free(rDest); continue; } } } if(cbreak) { free(rDest); return 0; } mode[0] = openMode; mode[1] = 'b'; if((fout = fdevopen(rDest, mode)) == 0) { error_open_file(rDest); free(rDest); return 0; } mode[0] = 'r'; h = src; do { if((rSrc = fillFnam(h->fnam, ff.ff_name)) == 0) { fclose(fout); free(rDest); return 0; } mode[1] = (asc = h->flags & ASCII) != 0? 't': 'b'; reOpenIn: if((fin = fdevopen(rSrc, mode)) == 0) { error_open_file(rSrc); fclose(fout); free(rSrc); free(rDest); return 0; } if(isadev(fileno(fin)) && mode[1] != 't' && (h->flags & BINARY) == 0) { /* character devices are opened in textmode by default */ fclose(fin); mode[1] = 't'; goto reOpenIn; } dispCopy(rSrc, rDest, openMode == 'a' || h != src); if(cbreak) { fclose(fin); fclose(fout); free(rSrc); free(rDest); return 0; } /* Now copy the file */ rc = 1; if(mode[1] != 't') { /* binary file */ if(Fcopy(fout, fin) != 0) { if(ferror(fin)) { error_read_file(rSrc); } else if(ferror(fout)) { error_write_file(rDest); } else error_copy(); rc = 0; } } else { /* text file, manually transform '\n' */ if(Fmaxbuf((byte**)&buf, &len) == 0) { if(len > INT_MAX) len = INT_MAX; while(fgets(buf, len, fin)) { p = strchr(buf, '\0'); if(*--p == '\n') { *p = '\0'; fputs(buf, fout); putc('\r', fout); putc('\n', fout); } else fputs(buf, fout); } free(buf); } else { error_out_of_memory(); rc = 0; } } if(rc) if(ferror(fin)) { error_read_file(rSrc); rc = 0; } else if(ferror(fout)) { error_write_file(rDest); rc = 0; } if(cbreak) rc = 0; fclose(fin); free(rSrc); if(!rc) { fclose(fout); free(rDest); return 0; } } while((h = h->app) != 0); if(asc) { /* append the ^Z as we copied in ASCII mode */ putc(0x1a, fout); } rc = ferror(fout); fclose(fout); if(rc) { error_write_file(rDest); free(rDest); return 0; } free(rDest); } while(FINDNEXT(&ff) == 0); return 1; }
int mv1(char *from, Dir *dirb, char *todir, char *toelem) { int fdf, fdt, i, j, stat; char toname[4096], fromname[4096]; char *fromdir, *fromelem; Dir *dirt, null; strncpy(fromname, from, sizeof fromname); split(from, &fromdir, &fromelem); if(toelem == 0) toelem = fromelem; i = strlen(toelem); if(i==0){ fprint(2, "mv: null last name element moving %s\n", fromname); return -1; } j = strlen(todir); if(i + j + 2 > sizeof toname){ fprint(2, "mv: path too big (max %d): %s/%s\n", sizeof toname, todir, toelem); return -1; } memmove(toname, todir, j); toname[j] = '/'; memmove(toname+j+1, toelem, i); toname[i+j+1] = 0; if(samefile(fromdir, todir)){ if(samefile(fromname, toname)){ fprint(2, "mv: %s and %s are the same\n", fromname, toname); return -1; } /* remove target if present */ dirt = dirstat(toname); if(dirt != nil) { hardremove(toname); free(dirt); } /* try wstat */ nulldir(&null); null.name = toelem; if(dirwstat(fromname, &null) >= 0) return 0; if(dirb->mode & DMDIR){ fprint(2, "mv: can't rename directory %s: %r\n", fromname); return -1; } } /* * Renaming won't work --- must copy */ if(dirb->mode & DMDIR){ fprint(2, "mv: %s is a directory, not copied to %s\n", fromname, toname); return -1; } fdf = open(fromname, OREAD); if(fdf < 0){ fprint(2, "mv: can't open %s: %r\n", fromname); return -1; } dirt = dirstat(toname); if(dirt != nil && (dirt->mode & DMAPPEND)) hardremove(toname); /* because create() won't truncate file */ free(dirt); fdt = create(toname, OWRITE, dirb->mode); if(fdt < 0){ fprint(2, "mv: can't create %s: %r\n", toname); close(fdf); return -1; } stat = copy1(fdf, fdt, fromname, toname); close(fdf); if(stat >= 0){ nulldir(&null); null.mtime = dirb->mtime; null.mode = dirb->mode; dirfwstat(fdt, &null); /* ignore errors; e.g. user none always fails */ if(remove(fromname) < 0){ fprint(2, "mv: can't remove %s: %r\n", fromname); stat = -1; } } close(fdt); return stat; }
int main( int argc, char** argv ) { ctmbstr prog = argv[0]; ctmbstr cfgfil = NULL, errfil = NULL, htmlfil = NULL; TidyDoc tdoc = tidyCreate(); int status = 0; uint contentErrors = 0; uint contentWarnings = 0; uint accessWarnings = 0; errout = stderr; /* initialize to stderr */ status = 0; #ifdef TIDY_CONFIG_FILE if ( tidyFileExists( tdoc, TIDY_CONFIG_FILE) ) { status = tidyLoadConfig( tdoc, TIDY_CONFIG_FILE ); if ( status != 0 ) fprintf(errout, "Loading config file \"%s\" failed, err = %d\n", TIDY_CONFIG_FILE, status); } #endif /* TIDY_CONFIG_FILE */ /* look for env var "HTML_TIDY" */ /* then for ~/.tidyrc (on platforms defining $HOME) */ if ( (cfgfil = getenv("HTML_TIDY")) != NULL ) { status = tidyLoadConfig( tdoc, cfgfil ); if ( status != 0 ) fprintf(errout, "Loading config file \"%s\" failed, err = %d\n", cfgfil, status); } #ifdef TIDY_USER_CONFIG_FILE else if ( tidyFileExists( tdoc, TIDY_USER_CONFIG_FILE) ) { status = tidyLoadConfig( tdoc, TIDY_USER_CONFIG_FILE ); if ( status != 0 ) fprintf(errout, "Loading config file \"%s\" failed, err = %d\n", TIDY_USER_CONFIG_FILE, status); } #endif /* TIDY_USER_CONFIG_FILE */ /* read command line */ while ( argc > 0 ) { if (argc > 1 && argv[1][0] == '-') { /* support -foo and --foo */ ctmbstr arg = argv[1] + 1; if ( strcasecmp(arg, "xml") == 0) tidyOptSetBool( tdoc, TidyXmlTags, yes ); else if ( strcasecmp(arg, "asxml") == 0 || strcasecmp(arg, "asxhtml") == 0 ) { tidyOptSetBool( tdoc, TidyXhtmlOut, yes ); } else if ( strcasecmp(arg, "ashtml") == 0 ) tidyOptSetBool( tdoc, TidyHtmlOut, yes ); else if ( strcasecmp(arg, "indent") == 0 ) { tidyOptSetInt( tdoc, TidyIndentContent, TidyAutoState ); if ( tidyOptGetInt(tdoc, TidyIndentSpaces) == 0 ) tidyOptResetToDefault( tdoc, TidyIndentSpaces ); } else if ( strcasecmp(arg, "omit") == 0 ) tidyOptSetBool( tdoc, TidyHideEndTags, yes ); else if ( strcasecmp(arg, "upper") == 0 ) tidyOptSetBool( tdoc, TidyUpperCaseTags, yes ); else if ( strcasecmp(arg, "clean") == 0 ) tidyOptSetBool( tdoc, TidyMakeClean, yes ); else if ( strcasecmp(arg, "bare") == 0 ) tidyOptSetBool( tdoc, TidyMakeBare, yes ); else if ( strcasecmp(arg, "raw") == 0 || strcasecmp(arg, "ascii") == 0 || strcasecmp(arg, "latin0") == 0 || strcasecmp(arg, "latin1") == 0 || strcasecmp(arg, "utf8") == 0 || #ifndef NO_NATIVE_ISO2022_SUPPORT strcasecmp(arg, "iso2022") == 0 || #endif #if SUPPORT_UTF16_ENCODINGS strcasecmp(arg, "utf16le") == 0 || strcasecmp(arg, "utf16be") == 0 || strcasecmp(arg, "utf16") == 0 || #endif #if SUPPORT_ASIAN_ENCODINGS strcasecmp(arg, "shiftjis") == 0 || strcasecmp(arg, "big5") == 0 || #endif strcasecmp(arg, "mac") == 0 || strcasecmp(arg, "win1252") == 0 || strcasecmp(arg, "ibm858") == 0 ) { tidySetCharEncoding( tdoc, arg ); } else if ( strcasecmp(arg, "numeric") == 0 ) tidyOptSetBool( tdoc, TidyNumEntities, yes ); else if ( strcasecmp(arg, "modify") == 0 || strcasecmp(arg, "change") == 0 || /* obsolete */ strcasecmp(arg, "update") == 0 ) /* obsolete */ { tidyOptSetBool( tdoc, TidyWriteBack, yes ); } else if ( strcasecmp(arg, "errors") == 0 ) tidyOptSetBool( tdoc, TidyShowMarkup, no ); else if ( strcasecmp(arg, "quiet") == 0 ) tidyOptSetBool( tdoc, TidyQuiet, yes ); else if ( strcasecmp(arg, "help") == 0 || strcasecmp(arg, "h") == 0 || *arg == '?' ) { help( prog ); tidyRelease( tdoc ); return 0; /* success */ } else if ( strcasecmp(arg, "xml-help") == 0) { xml_help( ); tidyRelease( tdoc ); return 0; /* success */ } else if ( strcasecmp(arg, "help-config") == 0 ) { optionhelp( tdoc ); tidyRelease( tdoc ); return 0; /* success */ } else if ( strcasecmp(arg, "xml-config") == 0 ) { XMLoptionhelp( tdoc ); tidyRelease( tdoc ); return 0; /* success */ } else if ( strcasecmp(arg, "show-config") == 0 ) { optionvalues( tdoc ); tidyRelease( tdoc ); return 0; /* success */ } else if ( strcasecmp(arg, "config") == 0 ) { if ( argc >= 3 ) { ctmbstr post; tidyLoadConfig( tdoc, argv[2] ); /* Set new error output stream if setting changed */ post = tidyOptGetValue( tdoc, TidyErrFile ); if ( post && (!errfil || !samefile(errfil, post)) ) { errfil = post; errout = tidySetErrorFile( tdoc, post ); } --argc; ++argv; } } #if SUPPORT_ASIAN_ENCODINGS else if ( strcasecmp(arg, "language") == 0 || strcasecmp(arg, "lang") == 0 ) { if ( argc >= 3 ) { tidyOptSetValue( tdoc, TidyLanguage, argv[2] ); --argc; ++argv; } } #endif else if ( strcasecmp(arg, "output") == 0 || strcasecmp(arg, "-output-file") == 0 || strcasecmp(arg, "o") == 0 ) { if ( argc >= 3 ) { tidyOptSetValue( tdoc, TidyOutFile, argv[2] ); --argc; ++argv; } } else if ( strcasecmp(arg, "file") == 0 || strcasecmp(arg, "-file") == 0 || strcasecmp(arg, "f") == 0 ) { if ( argc >= 3 ) { errfil = argv[2]; errout = tidySetErrorFile( tdoc, errfil ); --argc; ++argv; } } else if ( strcasecmp(arg, "wrap") == 0 || strcasecmp(arg, "-wrap") == 0 || strcasecmp(arg, "w") == 0 ) { if ( argc >= 3 ) { uint wraplen = 0; int nfields = sscanf( argv[2], "%u", &wraplen ); tidyOptSetInt( tdoc, TidyWrapLen, wraplen ); if (nfields > 0) { --argc; ++argv; } } } else if ( strcasecmp(arg, "version") == 0 || strcasecmp(arg, "-version") == 0 || strcasecmp(arg, "v") == 0 ) { version(); tidyRelease( tdoc ); return 0; /* success */ } else if ( strncmp(argv[1], "--", 2 ) == 0) { if ( tidyOptParseValue(tdoc, argv[1]+2, argv[2]) ) { /* Set new error output stream if setting changed */ ctmbstr post = tidyOptGetValue( tdoc, TidyErrFile ); if ( post && (!errfil || !samefile(errfil, post)) ) { errfil = post; errout = tidySetErrorFile( tdoc, post ); } ++argv; --argc; } } #if SUPPORT_ACCESSIBILITY_CHECKS else if ( strcasecmp(arg, "access") == 0 ) { if ( argc >= 3 ) { uint acclvl = 0; int nfields = sscanf( argv[2], "%u", &acclvl ); tidyOptSetInt( tdoc, TidyAccessibilityCheckLevel, acclvl ); if (nfields > 0) { --argc; ++argv; } } } #endif else { uint c; ctmbstr s = argv[1]; while ( (c = *++s) != '\0' ) { switch ( c ) { case 'i': tidyOptSetInt( tdoc, TidyIndentContent, TidyAutoState ); if ( tidyOptGetInt(tdoc, TidyIndentSpaces) == 0 ) tidyOptResetToDefault( tdoc, TidyIndentSpaces ); break; /* Usurp -o for output file. Anyone hiding end tags? case 'o': tidyOptSetBool( tdoc, TidyHideEndTags, yes ); break; */ case 'u': tidyOptSetBool( tdoc, TidyUpperCaseTags, yes ); break; case 'c': tidyOptSetBool( tdoc, TidyMakeClean, yes ); break; case 'b': tidyOptSetBool( tdoc, TidyMakeBare, yes ); break; case 'n': tidyOptSetBool( tdoc, TidyNumEntities, yes ); break; case 'm': tidyOptSetBool( tdoc, TidyWriteBack, yes ); break; case 'e': tidyOptSetBool( tdoc, TidyShowMarkup, no ); break; case 'q': tidyOptSetBool( tdoc, TidyQuiet, yes ); break; default: unknownOption( c ); break; } } } --argc; ++argv; continue; } if ( argc > 1 ) { htmlfil = argv[1]; if ( tidyOptGetBool(tdoc, TidyEmacs) ) tidyOptSetValue( tdoc, TidyEmacsFile, htmlfil ); status = tidyParseFile( tdoc, htmlfil ); } else { htmlfil = "stdin"; status = tidyParseStdin( tdoc ); } if ( status >= 0 ) status = tidyCleanAndRepair( tdoc ); if ( status >= 0 ) status = tidyRunDiagnostics( tdoc ); if ( status > 1 ) /* If errors, do we want to force output? */ status = ( tidyOptGetBool(tdoc, TidyForceOutput) ? status : -1 ); if ( status >= 0 && tidyOptGetBool(tdoc, TidyShowMarkup) ) { if ( tidyOptGetBool(tdoc, TidyWriteBack) && argc > 1 ) status = tidySaveFile( tdoc, htmlfil ); else { ctmbstr outfil = tidyOptGetValue( tdoc, TidyOutFile ); if ( outfil ) status = tidySaveFile( tdoc, outfil ); else status = tidySaveStdout( tdoc ); } } contentErrors += tidyErrorCount( tdoc ); contentWarnings += tidyWarningCount( tdoc ); accessWarnings += tidyAccessWarningCount( tdoc ); --argc; ++argv; if ( argc <= 1 ) break; } if (!tidyOptGetBool(tdoc, TidyQuiet) && errout == stderr && !contentErrors) fprintf(errout, "\n"); if (contentErrors + contentWarnings > 0 && !tidyOptGetBool(tdoc, TidyQuiet)) tidyErrorSummary(tdoc); if (!tidyOptGetBool(tdoc, TidyQuiet)) tidyGeneralInfo(tdoc); /* called to free hash tables etc. */ tidyRelease( tdoc ); /* return status can be used by scripts */ if ( contentErrors > 0 ) return 2; if ( contentWarnings > 0 ) return 1; /* 0 signifies all is ok */ return 0; }
int copy_file(const char *srcpath, const char *destpath) { const unsigned PAGESIZE = sysconf(_SC_PAGESIZE) * BUFSIZE; int srcfd = open(srcpath, O_RDONLY); if(srcfd == ERROR) { strexit(srcpath); } struct stat* st = (struct stat*) malloc(sizeof(struct stat)); if(fstat(srcfd, st) == ERROR) { strexit("FSTAT"); } int destfd = ERROR; if(S_ISDIR(st->st_mode)) { if(mkdir(destpath, st->st_mode) == ERROR) { strexit(destpath); } return 0; } else { if((destfd = open(destpath, O_WRONLY | O_CREAT | O_APPEND, st->st_mode)) == ERROR) { strexit(destpath); } } if(samefile(srcfd, destfd)) { return true; } if(ftruncate(destfd, 0) == ERROR) { strexit("TRUNCATE"); } if(st->st_size > 0 && fallocate(destfd, FALLOC_FL_KEEP_SIZE, 0, st->st_size) == ERROR) { strexit("FALLOCATE"); } // AIO Read / Write Information struct fileaio* arg = (struct fileaio*) malloc(sizeof(struct fileaio)); if(!arg) { strexit("FILEAIO MALLOC"); } arg->srcfd = srcfd; arg->destfd = destfd; arg->filesize = st->st_size; arg->offset = PAGESIZE; arg->aio = (struct aiocb*) calloc(1, sizeof(struct aiocb)); if(!arg->aio) { strexit("AIOCB CALLOC"); } arg->aio->aio_fildes = srcfd; arg->aio->aio_buf = malloc(PAGESIZE); if(!arg->aio->aio_buf) { strexit("AIOCB BUFFER"); } arg->aio->aio_nbytes = PAGESIZE; arg->aio->aio_offset = 0; // Setup sigevent handler for when AIO finishes arg->aio->aio_sigevent.sigev_notify = SIGEV_THREAD; arg->aio->aio_sigevent.sigev_notify_function = aio_handler; arg->aio->aio_sigevent.sigev_notify_attributes = NULL; arg->aio->aio_sigevent.sigev_value.sival_ptr = arg; if(aio_read(arg->aio) == ERROR) { strexit("READ LIO_LISTIO"); } ++outstanding_aio; return 0; }