Fix bare-filename crashes in timesheet and loadhist
timesheet --file ts.csv crashed in write_entries: os.makedirs('')
on the empty dirname. Only makedirs when a directory part exists.
loadhist --file loadhist.log ran ensure_dir which did mkdir -p
'loadhist.log', creating a directory named after the log file.
Only create the directory when the path has a directory component.
This commit is contained in:
1 parent
b106368869
commit
62bf71a72e
2 files changed
+4
-2
No files matched your search
+3
-1
@@ -19,7 +19,9 @@ def read_entries(path):
|
||||
return entries
|
||||
|
||||
def write_entries(path, entries):
|
||||
os.makedirs(os.path.dirname(path), exist_ok=True)
|
||||
d = os.path.dirname(path)
|
||||
if d:
|
||||
os.makedirs(d, exist_ok=True)
|
||||
with open(path, "w") as f:
|
||||
for e in entries:
|
||||
f.write("\t".join(e) + "\n")
|
||||
|
||||
+1
-1
@@ -20,7 +20,7 @@ static void defpath(char*buf,size_t n){const char*h=getenv("HOME");
|
||||
if(h)snprintf(buf,n,"%s/.cache/fileutils/loadhist.log",h);
|
||||
else snprintf(buf,n,"/tmp/loadhist.log");}
|
||||
static void ensure_dir(const char*path){char d[1024]; snprintf(d,sizeof d,"%s",path);
|
||||
char*s=strrchr(d,'/'); if(s)*s=0; char cmd[1100]; snprintf(cmd,sizeof cmd,"mkdir -p '%s'",d); (void)!system(cmd);}
|
||||
char*s=strrchr(d,'/'); if(!s)return; *s=0; char cmd[1100]; snprintf(cmd,sizeof cmd,"mkdir -p '%s'",d); (void)!system(cmd);}
|
||||
static int show(const char*path,int n){
|
||||
FILE*f=fopen(path,"r"); if(!f){printf("loadhist: no history yet at %s (run --record first)\n",path); return 0;}
|
||||
double *v=NULL; size_t cnt=0,cap=0; time_t *t=NULL; char line[256];
|
||||
|
||||
Reference in new issue
Block a user