| 1 | /* |
|---|
| 2 | * (C) 2001 Clemson University and The University of Chicago |
|---|
| 3 | * |
|---|
| 4 | * See COPYING in top-level directory. |
|---|
| 5 | */ |
|---|
| 6 | |
|---|
| 7 | #include <stdio.h> |
|---|
| 8 | #include <unistd.h> |
|---|
| 9 | #include <sys/types.h> |
|---|
| 10 | #include "client.h" |
|---|
| 11 | #include "pvfs2-types.h" |
|---|
| 12 | #include "pvfs2-util.h" |
|---|
| 13 | #include "str-utils.h" |
|---|
| 14 | #include "pint-sysint-utils.h" |
|---|
| 15 | |
|---|
| 16 | int main(int argc,char **argv) |
|---|
| 17 | { |
|---|
| 18 | int ret = -1; |
|---|
| 19 | char old_buf[PVFS_SEGMENT_MAX] = {0}; |
|---|
| 20 | char new_buf[PVFS_SEGMENT_MAX] = {0}; |
|---|
| 21 | char *old_filename = (char *)0; |
|---|
| 22 | char *new_filename = (char *)0; |
|---|
| 23 | PVFS_fs_id cur_fs; |
|---|
| 24 | char* old_entry; |
|---|
| 25 | PVFS_object_ref old_parent_refn; |
|---|
| 26 | char* new_entry; |
|---|
| 27 | PVFS_object_ref new_parent_refn; |
|---|
| 28 | PVFS_credentials credentials; |
|---|
| 29 | |
|---|
| 30 | if (argc != 3) |
|---|
| 31 | { |
|---|
| 32 | printf("usage: %s old_pathname new_pathname\n", argv[0]); |
|---|
| 33 | return 1; |
|---|
| 34 | } |
|---|
| 35 | old_filename = argv[1]; |
|---|
| 36 | new_filename = argv[2]; |
|---|
| 37 | |
|---|
| 38 | ret = PVFS_util_init_defaults(); |
|---|
| 39 | if (ret < 0) |
|---|
| 40 | { |
|---|
| 41 | PVFS_perror("PVFS_util_init_defaults", ret); |
|---|
| 42 | return (-1); |
|---|
| 43 | } |
|---|
| 44 | ret = PVFS_util_get_default_fsid(&cur_fs); |
|---|
| 45 | if (ret < 0) |
|---|
| 46 | { |
|---|
| 47 | PVFS_perror("PVFS_util_get_default_fsid", ret); |
|---|
| 48 | return (-1); |
|---|
| 49 | } |
|---|
| 50 | |
|---|
| 51 | if (PINT_remove_base_dir(old_filename, old_buf, PVFS_SEGMENT_MAX)) |
|---|
| 52 | { |
|---|
| 53 | if (old_filename[0] != '/') |
|---|
| 54 | { |
|---|
| 55 | printf("You forgot the leading '/'\n"); |
|---|
| 56 | } |
|---|
| 57 | printf("Cannot retrieve entry name for %s\n", |
|---|
| 58 | old_filename); |
|---|
| 59 | return(-1); |
|---|
| 60 | } |
|---|
| 61 | printf("Old filename is %s\n", old_buf); |
|---|
| 62 | |
|---|
| 63 | if (PINT_remove_base_dir(new_filename, new_buf, PVFS_SEGMENT_MAX)) |
|---|
| 64 | { |
|---|
| 65 | if (new_filename[0] != '/') |
|---|
| 66 | { |
|---|
| 67 | printf("You forgot the leading '/'\n"); |
|---|
| 68 | } |
|---|
| 69 | printf("Cannot retrieve name %s\n", |
|---|
| 70 | new_filename); |
|---|
| 71 | return(-1); |
|---|
| 72 | } |
|---|
| 73 | printf("New filename is %s\n",new_buf); |
|---|
| 74 | |
|---|
| 75 | PVFS_util_gen_credentials(&credentials); |
|---|
| 76 | |
|---|
| 77 | old_entry = old_buf; |
|---|
| 78 | ret = PINT_lookup_parent(old_filename, cur_fs, &credentials, |
|---|
| 79 | &old_parent_refn.handle); |
|---|
| 80 | if(ret < 0) |
|---|
| 81 | { |
|---|
| 82 | PVFS_perror("PVFS_util_lookup_parent", ret); |
|---|
| 83 | return(-1); |
|---|
| 84 | } |
|---|
| 85 | old_parent_refn.fs_id = cur_fs; |
|---|
| 86 | new_entry = new_buf; |
|---|
| 87 | ret = PINT_lookup_parent(new_filename, cur_fs, &credentials, |
|---|
| 88 | &new_parent_refn.handle); |
|---|
| 89 | if(ret < 0) |
|---|
| 90 | { |
|---|
| 91 | PVFS_perror("PVFS_util_lookup_parent", ret); |
|---|
| 92 | return(-1); |
|---|
| 93 | } |
|---|
| 94 | new_parent_refn.fs_id = cur_fs; |
|---|
| 95 | |
|---|
| 96 | ret = PVFS_sys_rename(old_entry, old_parent_refn, new_entry, |
|---|
| 97 | new_parent_refn, &credentials, NULL); |
|---|
| 98 | if (ret < 0) |
|---|
| 99 | { |
|---|
| 100 | printf("rename failed with errcode = %d\n",ret); |
|---|
| 101 | return(-1); |
|---|
| 102 | } |
|---|
| 103 | |
|---|
| 104 | printf("===================================\n"); |
|---|
| 105 | printf("file named %s has been renamed to %s\n", |
|---|
| 106 | old_filename, new_filename); |
|---|
| 107 | |
|---|
| 108 | //close it down |
|---|
| 109 | ret = PVFS_sys_finalize(); |
|---|
| 110 | if (ret < 0) |
|---|
| 111 | { |
|---|
| 112 | printf("finalizing sysint failed with errcode = %d\n", ret); |
|---|
| 113 | return (-1); |
|---|
| 114 | } |
|---|
| 115 | |
|---|
| 116 | return(0); |
|---|
| 117 | } |
|---|