From c6af6ce578ce5e99ee7fea61ced259012f71b7bf Mon Sep 17 00:00:00 2001 From: Allan Bowe Date: Thu, 6 Oct 2022 12:08:25 +0000 Subject: [PATCH] fix: ignoring empty files in mp_hashdirectory. Closes #314 --- all.sas | 17 +++++++++++++++-- base/mp_hashdirectory.sas | 17 +++++++++++++++-- 2 files changed, 30 insertions(+), 4 deletions(-) diff --git a/all.sas b/all.sas index cc05c58..2058575 100644 --- a/all.sas +++ b/all.sas @@ -8659,7 +8659,7 @@ run; %put %sysfunc(hashing_file(md5,/path/to/file.blob,0)); - Usage: + Actual usage: %let fpath=/some/directory; @@ -8681,6 +8681,7 @@ run; the folder hashes cascade upwards so you know immediately if there is a change in a sub/sub directory @li If the folder has no content (empty) then it is ignored. No hash created. + @li If the file is empty, it is also ignored / no hash created.

SAS Macros

@li mp_dirlist.sas @@ -8736,7 +8737,19 @@ data &outds; ts=datetime(); if file_or_folder='file' then do; - file_hash=hashing_file("&method",cats(file_path),0); + /* if file is empty, hashing_file will break - so ignore / delete */ + length fname val $8; + drop fname val fid is_empty; + rc=filename(fname,file_path); + fid=fopen(fname); + if fid > 0 then do; + rc=fread(fid); + is_empty=fget(fid,val); + end; + rc=fclose(fid); + rc=filename(fname); + if is_empty ne 0 then delete; + else file_hash=hashing_file("&method",cats(file_path),0); end; hash_duration=datetime()-ts; run; diff --git a/base/mp_hashdirectory.sas b/base/mp_hashdirectory.sas index a3db040..01e31af 100644 --- a/base/mp_hashdirectory.sas +++ b/base/mp_hashdirectory.sas @@ -9,7 +9,7 @@ %put %sysfunc(hashing_file(md5,/path/to/file.blob,0)); - Usage: + Actual usage: %let fpath=/some/directory; @@ -31,6 +31,7 @@ the folder hashes cascade upwards so you know immediately if there is a change in a sub/sub directory @li If the folder has no content (empty) then it is ignored. No hash created. + @li If the file is empty, it is also ignored / no hash created.

SAS Macros

@li mp_dirlist.sas @@ -86,7 +87,19 @@ data &outds; ts=datetime(); if file_or_folder='file' then do; - file_hash=hashing_file("&method",cats(file_path),0); + /* if file is empty, hashing_file will break - so ignore / delete */ + length fname val $8; + drop fname val fid is_empty; + rc=filename(fname,file_path); + fid=fopen(fname); + if fid > 0 then do; + rc=fread(fid); + is_empty=fget(fid,val); + end; + rc=fclose(fid); + rc=filename(fname); + if is_empty ne 0 then delete; + else file_hash=hashing_file("&method",cats(file_path),0); end; hash_duration=datetime()-ts; run;