Skip to content

Commit

Permalink
Implement rename.
Browse files Browse the repository at this point in the history
  • Loading branch information
nirvdrum committed May 5, 2015
1 parent e8ee031 commit 49c2a86
Show file tree
Hide file tree
Showing 7 changed files with 43 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/main/java/jnr/posix/BaseNativePOSIX.java
Expand Up @@ -601,6 +601,10 @@ public int ftruncate(int fd, long offset) {
return libc().ftruncate(fd, offset);
}

public int rename(CharSequence oldName, CharSequence newName) {
return libc().rename(oldName, newName);
}

public String getcwd() {
byte[] cwd = new byte[1024];
long result = libc().getcwd(cwd, 1024);
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/jnr/posix/CheckedPOSIX.java
Expand Up @@ -459,6 +459,10 @@ public int ftruncate(int fd, long offset) {
try {return posix.ftruncate(fd, offset); } catch (UnsatisfiedLinkError ule) { return unimplementedInt(); }
}

public int rename(CharSequence oldName, CharSequence newName) {
try {return posix.rename(oldName, newName); } catch (UnsatisfiedLinkError ule) { return unimplementedInt(); }
}

public String getcwd() {
try {return posix.getcwd(); } catch (UnsatisfiedLinkError ule) { return unimplementedString(); }
}
Expand Down
13 changes: 13 additions & 0 deletions src/main/java/jnr/posix/JavaPOSIX.java
Expand Up @@ -586,6 +586,19 @@ public int ftruncate(int fd, long offset) {
return -1;
}

public int rename(CharSequence oldName, CharSequence newName) {
// Very basic support. This might not work well with rename's semantics regarding symlinks.

File oldFile = new File(oldName.toString());
File newFile = new File(newName.toString());

if (oldFile.renameTo(newFile)) {
return 0;
}

return -1;
}

public String getcwd() {
return System.getProperty("user.dir");
}
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/jnr/posix/LazyPOSIX.java
Expand Up @@ -455,6 +455,10 @@ public int ftruncate(int fd, long offset) {
return posix().ftruncate(fd, offset);
}

public int rename(CharSequence oldName, CharSequence newName) {
return posix().rename(oldName, newName);
}

public String getcwd() {
return posix().getcwd();
}
Expand Down
1 change: 1 addition & 0 deletions src/main/java/jnr/posix/LibC.java
Expand Up @@ -134,6 +134,7 @@ public interface LibCSignalHandler {
int open(CharSequence path, int flags, int perm);
int pipe(@Out int[] fds);
int ftruncate(int fd, long offset);
int rename(CharSequence oldName, CharSequence newName);
long getcwd(byte[] cwd, int len);
int fsync(int fd);
int fdatasync(int fd);
Expand Down
1 change: 1 addition & 0 deletions src/main/java/jnr/posix/POSIX.java
Expand Up @@ -140,6 +140,7 @@ public long posix_spawnp(String path, Collection<? extends SpawnFileAction> file
int lseek(int fd, long offset, int whence);
int pipe(int[] fds);
int ftruncate(int fd, long offset);
int rename(CharSequence oldName, CharSequence newName);
String getcwd();

int socketpair(int domain, int type, int protocol, int[] fds);
Expand Down
16 changes: 16 additions & 0 deletions src/test/java/jnr/posix/FileTest.java
Expand Up @@ -323,4 +323,20 @@ public void fchmodTest() throws IOException {
tmp.delete();
}


@Test
public void renameTest() throws IOException {
File oldFile = File.createTempFile("jnr-posix-rename-test", "tmp");
File newFile = new File(oldFile.getParent() + File.separatorChar + "jnr-posix-rename-test-new");

assertTrue(oldFile.exists());
assertFalse(newFile.exists());

posix.rename(oldFile.getCanonicalPath(), newFile.getCanonicalPath());

assertFalse(oldFile.exists());
assertTrue(newFile.exists());

newFile.delete();
}
}

0 comments on commit 49c2a86

Please sign in to comment.