Sun, 05 Apr 2020 10:06:38 +0200
look up temp location + coherent return codes
dav/main.c | file | annotate | diff | comparison | revisions |
--- a/dav/main.c Sun Apr 05 09:34:58 2020 +0200 +++ b/dav/main.c Sun Apr 05 10:06:38 2020 +0200 @@ -1225,10 +1225,43 @@ } } - // open temporary file - // TODO: look up the location of the temp directory, maybe add sys_* funcs - char outfile[24]; - strncpy(outfile, ".dav-edit-XXXXXX", 24); + // get temp dir + char* envtmp = getenv("TMPDIR"); + char* outfile; + if(envtmp) { + size_t len = strlen(envtmp); + outfile = malloc(len+24); + memcpy(outfile, envtmp, len+1); + if(outfile[len-1] != '/') { + outfile[len] = '/'; + outfile[len+1] = 0; + } + } else { + outfile = malloc(24); + strncpy(outfile, "/tmp/", 24); + } + + // check temp dir and fall back to $HOME + if(access(outfile, W_OK)) { + char* home = getenv("HOME"); + if(home) { + size_t len = strlen(envtmp); + outfile = malloc(len+24); + memcpy(outfile, home, len+1); + if(outfile[len-1] != '/') { + outfile[len] = '/'; + outfile[len+1] = 0; + } + } else { + // fallback did not work, report last error from access() + perror("Cannot write to temporary location"); + free(outfile); + return -1; + } + } + + // create temp file and open it + outfile = strncat(outfile, ".dav-edit-XXXXXX", 23); int tmp_fd = mkstemp(outfile); if(tmp_fd < 0) { perror("Cannot open temporary file"); @@ -1240,11 +1273,13 @@ FILE* tmp_stream = sys_fopen(outfile, "wb"); if(!tmp_stream) { perror("Cannot open temporary file"); + free(outfile); close(tmp_fd); return -1; } if(dav_get_content(res, tmp_stream, (dav_write_func)fwrite)) { print_resource_error(sn, path); + free(outfile); close(tmp_fd); sys_unlink(outfile); return -1; @@ -1256,6 +1291,7 @@ SYS_STAT tmp_stat; if(sys_stat(outfile, &tmp_stat)) { perror("Cannot stat temporary file"); + free(outfile); close(tmp_fd); sys_unlink(outfile); return -1; @@ -1272,11 +1308,11 @@ pid_t pid = fork(); if(pid < 0) { perror("Cannot create process for editor"); - ret = 1; + ret = -1; } else if(pid == 0) { if(execvp(viargs[0], viargs)) { perror("Opening the editor failed"); - return 1; + return -1; } } else { int status = -1; @@ -1286,29 +1322,29 @@ } else if(WEXITSTATUS(status)) { fprintf(stderr, "Editor closed abnormally - file will not be uploaded.\n"); - ret = 1; + ret = -1; } else { // check if the file has changed if (sys_stat(outfile, &tmp_stat)) { // very rare case when someone concurrently changes permissions perror("Cannot stat temporary file"); - ret = 1; + ret = -1; } else if (dl_mtime < tmp_stat.st_mtime) { // upload changed file FILE* tmp_stream = sys_fopen(outfile, "rb"); if(!tmp_stream) { perror("Cannot open temporary file"); - close(tmp_fd); - return -1; - } - dav_set_content(res, tmp_stream, - (dav_read_func)fread, - (dav_seek_func)file_seek); - dav_set_content_length(res, tmp_stat.st_size); - ret = dav_store(res); - fclose(tmp_stream); - if(ret) { - print_resource_error(sn, path); + ret = -1; + } else { + dav_set_content(res, tmp_stream, + (dav_read_func)fread, + (dav_seek_func)file_seek); + dav_set_content_length(res, tmp_stat.st_size); + ret = dav_store(res); + fclose(tmp_stream); + if(ret) { + print_resource_error(sn, path); + } } } else { printf("No changes by user - file will not be uploaded.\n"); @@ -1323,6 +1359,7 @@ } else { sys_unlink(outfile); } + free(outfile); free(path); return ret;