Commit 047ca2f8 authored by Rob Swindell's avatar Rob Swindell 💬
Browse files

Replace fcopy with CopyFile (Win32 API function) on Windows.

Thanks to Wilfred van Velzen (2:280/464) for the tip. CopyFile() reduced
the 1GB file copy test (from and to CIFS/SMB) duration from 37 seconds
to 5 seconds with very low CPU utilization!
Created a wrapper for non-Windows OSes in xpdev/dirwrap. This is where
non-Windows-OS-specific optimized versions may appear in the future,
but for now it's just the previous fcopy() implementation (using a 256KB
stack buffer). sbbs doesn't actually copy files very often, so this
isn't as big of a deal as one might assume. The JavaScript global
method: file_copy() also benefits from these improvements, so any
scripts that use it (e.g. tickit.js) will also benefit. binkit.js has
its own file-copy logic (using a 2MB buffer), so no change there.
parent a2e4d573
Pipeline #96 passed with stage
in 9 minutes and 59 seconds
...@@ -3047,7 +3047,7 @@ js_fcopy(JSContext *cx, uintN argc, jsval *arglist) ...@@ -3047,7 +3047,7 @@ js_fcopy(JSContext *cx, uintN argc, jsval *arglist)
} }
rc=JS_SUSPENDREQUEST(cx); rc=JS_SUSPENDREQUEST(cx);
ret=fcopy(src,dest); ret = CopyFile(src, dest, /* failIfExists: */FALSE);
free(src); free(src);
free(dest); free(dest);
JS_RESUMEREQUEST(cx, rc); JS_RESUMEREQUEST(cx, rc);
......
...@@ -148,38 +148,6 @@ BOOL fmutex(const char* fname, const char* text, long max_age) ...@@ -148,38 +148,6 @@ BOOL fmutex(const char* fname, const char* text, long max_age)
return TRUE; return TRUE;
} }
BOOL fcopy(const char* src, const char* dest)
{
uint8_t buf[256 * 1024];
ulong count=0;
FILE* in;
FILE* out;
BOOL success=TRUE;
if((in=fopen(src,"rb"))==NULL)
return FALSE;
if((out=fopen(dest,"wb"))==NULL) {
fclose(in);
return FALSE;
}
while(!feof(in)) {
size_t rd = fread(buf, sizeof(uint8_t), sizeof(buf), in);
if(rd < 1)
break;
if(fwrite(buf, sizeof(uint8_t), rd, out) != rd) {
success = FALSE;
break;
}
MAYBE_YIELD();
}
fclose(in);
fclose(out);
return(success);
}
BOOL fcompare(const char* fn1, const char* fn2) BOOL fcompare(const char* fn1, const char* fn2)
{ {
FILE* fp1; FILE* fp1;
...@@ -240,7 +208,7 @@ BOOL backup(const char *fname, int backup_level, BOOL ren) ...@@ -240,7 +208,7 @@ BOOL backup(const char *fname, int backup_level, BOOL ren)
/* preserve the original time stamp */ /* preserve the original time stamp */
ut.modtime = fdate(fname); ut.modtime = fdate(fname);
if(!fcopy(fname,newname)) if(!CopyFile(fname, newname, /* failIfExists: */FALSE))
return FALSE; return FALSE;
ut.actime = time(NULL); ut.actime = time(NULL);
......
...@@ -47,7 +47,6 @@ int nopen(const char* str, int access); ...@@ -47,7 +47,6 @@ int nopen(const char* str, int access);
FILE * fnopen(int* file, const char* str, int access); FILE * fnopen(int* file, const char* str, int access);
BOOL ftouch(const char* fname); BOOL ftouch(const char* fname);
BOOL fmutex(const char* fname, const char* text, long max_age); BOOL fmutex(const char* fname, const char* text, long max_age);
BOOL fcopy(const char* src, const char* dest);
BOOL fcompare(const char* fn1, const char* fn2); BOOL fcompare(const char* fn1, const char* fn2);
BOOL backup(const char* org, int backup_level, BOOL ren); BOOL backup(const char* org, int backup_level, BOOL ren);
......
...@@ -1236,7 +1236,7 @@ bool sbbs_t::editfile(char *fname, bool msg) ...@@ -1236,7 +1236,7 @@ bool sbbs_t::editfile(char *fname, bool msg)
if(stricmp(msgtmp,path)) { if(stricmp(msgtmp,path)) {
removecase(msgtmp); removecase(msgtmp);
if(fexistcase(path)) if(fexistcase(path))
fcopy(path, msgtmp); CopyFile(path, msgtmp, /* failIfExists: */FALSE);
} }
editor_inf(useron_xedit,/* to: */fname,/* from: */nulstr,/* subj: */nulstr,/* mode: */0,INVALID_SUB,/* tagfile: */NULL); editor_inf(useron_xedit,/* to: */fname,/* from: */nulstr,/* subj: */nulstr,/* mode: */0,INVALID_SUB,/* tagfile: */NULL);
......
...@@ -1218,3 +1218,39 @@ int DLLCALL mkpath(const char* path) ...@@ -1218,3 +1218,39 @@ int DLLCALL mkpath(const char* path)
return(result); return(result);
} }
#if !defined _WIN32
BOOL CopyFile(const char* src, const char* dest, BOOL failIfExists)
{
uint8_t buf[256 * 1024];
ulong count=0;
FILE* in;
FILE* out;
BOOL success=TRUE;
if(failIfExists && fexist(dest))
return FALSE;
if((in=fopen(src,"rb"))==NULL)
return FALSE;
if((out=fopen(dest,"wb"))==NULL) {
fclose(in);
return FALSE;
}
while(!feof(in)) {
size_t rd = fread(buf, sizeof(uint8_t), sizeof(buf), in);
if(rd < 1)
break;
if(fwrite(buf, sizeof(uint8_t), rd, out) != rd) {
success = FALSE;
break;
}
MAYBE_YIELD();
}
fclose(in);
fclose(out);
return success;
}
#endif
...@@ -250,6 +250,10 @@ DLLEXPORT int DLLCALL removecase(const char *path); ...@@ -250,6 +250,10 @@ DLLEXPORT int DLLCALL removecase(const char *path);
#define removecase(x) remove(x) #define removecase(x) remove(x)
#endif #endif
#if !defined _WIN32
DLLEXPORT BOOL CopyFile(const char* src, const char* dest, BOOL failIfExists);
#endif
#if defined(__cplusplus) #if defined(__cplusplus)
} }
#endif #endif
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment