1223 dav_resource_free_all(res); |
1223 dav_resource_free_all(res); |
1224 res = vres; |
1224 res = vres; |
1225 } |
1225 } |
1226 } |
1226 } |
1227 |
1227 |
1228 // open temporary file |
1228 // get temp dir |
1229 // TODO: look up the location of the temp directory, maybe add sys_* funcs |
1229 char* envtmp = getenv("TMPDIR"); |
1230 char outfile[24]; |
1230 char* outfile; |
1231 strncpy(outfile, ".dav-edit-XXXXXX", 24); |
1231 if(envtmp) { |
|
1232 size_t len = strlen(envtmp); |
|
1233 outfile = malloc(len+24); |
|
1234 memcpy(outfile, envtmp, len+1); |
|
1235 if(outfile[len-1] != '/') { |
|
1236 outfile[len] = '/'; |
|
1237 outfile[len+1] = 0; |
|
1238 } |
|
1239 } else { |
|
1240 outfile = malloc(24); |
|
1241 strncpy(outfile, "/tmp/", 24); |
|
1242 } |
|
1243 |
|
1244 // check temp dir and fall back to $HOME |
|
1245 if(access(outfile, W_OK)) { |
|
1246 char* home = getenv("HOME"); |
|
1247 if(home) { |
|
1248 size_t len = strlen(envtmp); |
|
1249 outfile = malloc(len+24); |
|
1250 memcpy(outfile, home, len+1); |
|
1251 if(outfile[len-1] != '/') { |
|
1252 outfile[len] = '/'; |
|
1253 outfile[len+1] = 0; |
|
1254 } |
|
1255 } else { |
|
1256 // fallback did not work, report last error from access() |
|
1257 perror("Cannot write to temporary location"); |
|
1258 free(outfile); |
|
1259 return -1; |
|
1260 } |
|
1261 } |
|
1262 |
|
1263 // create temp file and open it |
|
1264 outfile = strncat(outfile, ".dav-edit-XXXXXX", 23); |
1232 int tmp_fd = mkstemp(outfile); |
1265 int tmp_fd = mkstemp(outfile); |
1233 if(tmp_fd < 0) { |
1266 if(tmp_fd < 0) { |
1234 perror("Cannot open temporary file"); |
1267 perror("Cannot open temporary file"); |
1235 return -1; |
1268 return -1; |
1236 } |
1269 } |
1238 // get resource |
1271 // get resource |
1239 if(!fresh_resource) { |
1272 if(!fresh_resource) { |
1240 FILE* tmp_stream = sys_fopen(outfile, "wb"); |
1273 FILE* tmp_stream = sys_fopen(outfile, "wb"); |
1241 if(!tmp_stream) { |
1274 if(!tmp_stream) { |
1242 perror("Cannot open temporary file"); |
1275 perror("Cannot open temporary file"); |
|
1276 free(outfile); |
1243 close(tmp_fd); |
1277 close(tmp_fd); |
1244 return -1; |
1278 return -1; |
1245 } |
1279 } |
1246 if(dav_get_content(res, tmp_stream, (dav_write_func)fwrite)) { |
1280 if(dav_get_content(res, tmp_stream, (dav_write_func)fwrite)) { |
1247 print_resource_error(sn, path); |
1281 print_resource_error(sn, path); |
|
1282 free(outfile); |
1248 close(tmp_fd); |
1283 close(tmp_fd); |
1249 sys_unlink(outfile); |
1284 sys_unlink(outfile); |
1250 return -1; |
1285 return -1; |
1251 } |
1286 } |
1252 fclose(tmp_stream); |
1287 fclose(tmp_stream); |
1254 |
1289 |
1255 // remember time s.t. we can later check if the file has changed |
1290 // remember time s.t. we can later check if the file has changed |
1256 SYS_STAT tmp_stat; |
1291 SYS_STAT tmp_stat; |
1257 if(sys_stat(outfile, &tmp_stat)) { |
1292 if(sys_stat(outfile, &tmp_stat)) { |
1258 perror("Cannot stat temporary file"); |
1293 perror("Cannot stat temporary file"); |
|
1294 free(outfile); |
1259 close(tmp_fd); |
1295 close(tmp_fd); |
1260 sys_unlink(outfile); |
1296 sys_unlink(outfile); |
1261 return -1; |
1297 return -1; |
1262 } |
1298 } |
1263 time_t dl_mtime = tmp_stat.st_mtime; |
1299 time_t dl_mtime = tmp_stat.st_mtime; |
1270 |
1306 |
1271 int ret = 0; |
1307 int ret = 0; |
1272 pid_t pid = fork(); |
1308 pid_t pid = fork(); |
1273 if(pid < 0) { |
1309 if(pid < 0) { |
1274 perror("Cannot create process for editor"); |
1310 perror("Cannot create process for editor"); |
1275 ret = 1; |
1311 ret = -1; |
1276 } else if(pid == 0) { |
1312 } else if(pid == 0) { |
1277 if(execvp(viargs[0], viargs)) { |
1313 if(execvp(viargs[0], viargs)) { |
1278 perror("Opening the editor failed"); |
1314 perror("Opening the editor failed"); |
1279 return 1; |
1315 return -1; |
1280 } |
1316 } |
1281 } else { |
1317 } else { |
1282 int status = -1; |
1318 int status = -1; |
1283 ret = waitpid(pid, &status, 0); |
1319 ret = waitpid(pid, &status, 0); |
1284 if(ret < 0) { |
1320 if(ret < 0) { |
1285 perror("Error waiting for editor"); |
1321 perror("Error waiting for editor"); |
1286 } else if(WEXITSTATUS(status)) { |
1322 } else if(WEXITSTATUS(status)) { |
1287 fprintf(stderr, |
1323 fprintf(stderr, |
1288 "Editor closed abnormally - file will not be uploaded.\n"); |
1324 "Editor closed abnormally - file will not be uploaded.\n"); |
1289 ret = 1; |
1325 ret = -1; |
1290 } else { |
1326 } else { |
1291 // check if the file has changed |
1327 // check if the file has changed |
1292 if (sys_stat(outfile, &tmp_stat)) { |
1328 if (sys_stat(outfile, &tmp_stat)) { |
1293 // very rare case when someone concurrently changes permissions |
1329 // very rare case when someone concurrently changes permissions |
1294 perror("Cannot stat temporary file"); |
1330 perror("Cannot stat temporary file"); |
1295 ret = 1; |
1331 ret = -1; |
1296 } else if (dl_mtime < tmp_stat.st_mtime) { |
1332 } else if (dl_mtime < tmp_stat.st_mtime) { |
1297 // upload changed file |
1333 // upload changed file |
1298 FILE* tmp_stream = sys_fopen(outfile, "rb"); |
1334 FILE* tmp_stream = sys_fopen(outfile, "rb"); |
1299 if(!tmp_stream) { |
1335 if(!tmp_stream) { |
1300 perror("Cannot open temporary file"); |
1336 perror("Cannot open temporary file"); |
1301 close(tmp_fd); |
1337 ret = -1; |
1302 return -1; |
1338 } else { |
1303 } |
1339 dav_set_content(res, tmp_stream, |
1304 dav_set_content(res, tmp_stream, |
1340 (dav_read_func)fread, |
1305 (dav_read_func)fread, |
1341 (dav_seek_func)file_seek); |
1306 (dav_seek_func)file_seek); |
1342 dav_set_content_length(res, tmp_stat.st_size); |
1307 dav_set_content_length(res, tmp_stat.st_size); |
1343 ret = dav_store(res); |
1308 ret = dav_store(res); |
1344 fclose(tmp_stream); |
1309 fclose(tmp_stream); |
1345 if(ret) { |
1310 if(ret) { |
1346 print_resource_error(sn, path); |
1311 print_resource_error(sn, path); |
1347 } |
1312 } |
1348 } |
1313 } else { |
1349 } else { |
1314 printf("No changes by user - file will not be uploaded.\n"); |
1350 printf("No changes by user - file will not be uploaded.\n"); |
1315 } |
1351 } |
1316 } |
1352 } |