Changeset 950e2da
- Timestamp:
- Nov 19, 2010, 2:15:49 AM (14 years ago)
- Branches:
- master, release-1.10, release-1.7, release-1.8, release-1.9
- Children:
- 1dd285b
- Parents:
- 5fca55f
- git-author:
- Jason Gross <jgross@MIT.EDU> (11/17/10 02:18:48)
- git-committer:
- Karl Ramm <kcr@1ts.org> (11/19/10 02:15:49)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
util.c
r5fca55f r950e2da 7 7 #include <sys/stat.h> 8 8 #include <sys/types.h> 9 9 #include <assert.h> 10 #include <glib.h> 11 #include <glib/gstdio.h> 10 12 #include <glib-object.h> 11 13 … … 376 378 } 377 379 380 381 /* If filename is a link, recursively resolve symlinks. Otherwise, return the filename 382 * unchanged. On error, call owl_function_error and return NULL. 383 * 384 * This function assumes that filename eventually resolves to an acutal file. 385 * If you want to check this, you should stat() the file first. 386 * 387 * The caller of this function is responsible for freeing the return value. 388 * 389 * Error conditions are the same as g_file_read_link. 390 */ 391 gchar *owl_util_recursive_resolve_link(const char *filename) 392 { 393 gchar *last_filename = g_strdup(filename); 394 GError *err = NULL; 395 while (last_filename != NULL && g_file_test(last_filename, G_FILE_TEST_IS_SYMLINK)) { 396 gchar *cur_filename = g_file_read_link(last_filename, &err); 397 g_free(last_filename); 398 last_filename = cur_filename; 399 } 400 if (last_filename == NULL) { 401 owl_function_error("Cannot resolve symlinked file %s: %s", 402 filename, err->message); 403 g_error_free(err); 404 } 405 return last_filename; 406 } 407 378 408 /* Delete all lines matching "line" from the named file. If no such 379 409 * line is found the file is left intact. If backup==1 then leave a … … 386 416 { 387 417 char *backupfile, *newfile, *buf = NULL; 418 gchar *actual_filename; /* gchar; we need to g_free it */ 388 419 FILE *old, *new; 389 420 struct stat st; … … 401 432 } 402 433 403 newfile = owl_sprintf("%s.new", filename); 434 /* resolve symlinks, because link() fails on symlinks, at least on AFS */ 435 actual_filename = owl_util_recursive_resolve_link(filename); 436 if (actual_filename == NULL) 437 return -1; /* resolving the symlink failed, but we already logged this error */ 438 439 newfile = owl_sprintf("%s.new", actual_filename); 404 440 if ((new = fopen(newfile, "w")) == NULL) { 405 441 owl_function_error("Cannot open %s (for writing): %s", 406 filename, strerror(errno));442 actual_filename, strerror(errno)); 407 443 owl_free(newfile); 408 444 fclose(old); 445 free(actual_filename); 409 446 return -1; 410 447 } … … 412 449 if (fchmod(fileno(new), st.st_mode & 0777) != 0) { 413 450 owl_function_error("Cannot set permissions on %s: %s", 414 filename, strerror(errno));451 actual_filename, strerror(errno)); 415 452 unlink(newfile); 416 453 fclose(new); 417 454 owl_free(newfile); 418 455 fclose(old); 456 free(actual_filename); 419 457 return -1; 420 458 } … … 431 469 432 470 if (backup) { 433 backupfile = owl_sprintf("%s.backup", filename);471 backupfile = owl_sprintf("%s.backup", actual_filename); 434 472 unlink(backupfile); 435 if (link( filename, backupfile) != 0) {473 if (link(actual_filename, backupfile) != 0) { 436 474 owl_function_error("Cannot link %s: %s", backupfile, strerror(errno)); 437 475 owl_free(backupfile); … … 443 481 } 444 482 445 if (rename(newfile, filename) != 0) {483 if (rename(newfile, actual_filename) != 0) { 446 484 owl_function_error("Cannot move %s to %s: %s", 447 newfile, filename, strerror(errno));485 newfile, actual_filename, strerror(errno)); 448 486 numremoved = -1; 449 487 } … … 451 489 unlink(newfile); 452 490 owl_free(newfile); 491 492 g_free(actual_filename); 453 493 454 494 return numremoved;
Note: See TracChangeset
for help on using the changeset viewer.