Skip to content

Commit ae03e10

Browse files
author
José Valim
committed
Merge pull request #1300 from jdrexler/issue-1285
Added chgrp!, chown!, and chmod! to module File
2 parents 208fbaf + 5df40f7 commit ae03e10

File tree

2 files changed

+146
-0
lines changed

2 files changed

+146
-0
lines changed

lib/elixir/lib/file.ex

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1019,6 +1019,66 @@ defmodule File do
10191019
end
10201020
end
10211021

1022+
@doc """
1023+
Changes the unix file `mode` for a given `file`.
1024+
Returns `:ok` on success, or `{:error, reason}`
1025+
on failure.
1026+
"""
1027+
def chmod(file, mode) do
1028+
F.change_mode(file, mode)
1029+
end
1030+
1031+
@doc """
1032+
Same as `chmod/2`, but raises an exception in case of failure. Otherwise `:ok`.
1033+
"""
1034+
def chmod!(file, mode) do
1035+
case chmod(file, mode) do
1036+
:ok -> :ok
1037+
{ :error, reason } ->
1038+
raise File.Error, reason: reason, action: "change mode for", path: :unicode.characters_to_binary(file)
1039+
end
1040+
end
1041+
1042+
@doc """
1043+
Changes the user group given by the group id `gid`
1044+
for a given `file`. Returns `:ok` on success, or
1045+
`{:error, reason}` on failure.
1046+
"""
1047+
def chgrp(file, gid) do
1048+
F.change_group(file, gid)
1049+
end
1050+
1051+
@doc """
1052+
Same as `chgrp/2`, but raises an exception in case of failure. Otherwise `:ok`.
1053+
"""
1054+
def chgrp!(file, gid) do
1055+
case chgrp(file, gid) do
1056+
:ok -> :ok
1057+
{ :error, reason } ->
1058+
raise File.Error, reason: reason, action: "change group for", path: :unicode.characters_to_binary(file)
1059+
end
1060+
end
1061+
1062+
@doc """
1063+
Changes the owner given by the user id `gid`
1064+
for a given `file`. Returns `:ok` on success,
1065+
or `{:error, reason}` on failure.
1066+
"""
1067+
def chown(file, uid) do
1068+
F.change_owner(file, uid)
1069+
end
1070+
1071+
@doc """
1072+
Same as `chown/2`, but raises an exception in case of failure. Otherwise `:ok`.
1073+
"""
1074+
def chown!(file, gid) do
1075+
case chown(file, gid) do
1076+
:ok -> :ok
1077+
{ :error, reason } ->
1078+
raise File.Error, reason: reason, action: "change owner for", path: :unicode.characters_to_binary(file)
1079+
end
1080+
end
1081+
10221082
## Helpers
10231083

10241084
defp open_defaults([:charlist|t], _add_binary) do

lib/elixir/test/elixir/file_test.exs

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1115,6 +1115,92 @@ defmodule FileTest do
11151115
end
11161116
end
11171117

1118+
test :chmod_with_success do
1119+
fixture = tmp_path("tmp_test.txt")
1120+
1121+
File.touch(fixture)
1122+
try do
1123+
assert File.chmod(fixture, 0100666) == :ok
1124+
stat = File.stat!(fixture)
1125+
assert stat.mode == 0100666
1126+
1127+
assert File.chmod(fixture, 0100777) == :ok
1128+
stat = File.stat!(fixture)
1129+
assert stat.mode == 0100777
1130+
after
1131+
File.rm(fixture)
1132+
end
1133+
end
1134+
1135+
test :chmod_with_success! do
1136+
fixture = tmp_path("tmp_test.txt")
1137+
1138+
File.touch(fixture)
1139+
try do
1140+
assert File.chmod!(fixture, 0100666) == :ok
1141+
stat = File.stat!(fixture)
1142+
assert stat.mode == 0100666
1143+
1144+
assert File.chmod!(fixture, 0100777) == :ok
1145+
stat = File.stat!(fixture)
1146+
assert stat.mode == 0100777
1147+
after
1148+
File.rm(fixture)
1149+
end
1150+
end
1151+
1152+
test :chmod_with_failue do
1153+
fixture = tmp_path("tmp_test.txt")
1154+
File.rm(fixture)
1155+
1156+
assert File.chmod(fixture, 0100777) == {:error,:enoent}
1157+
end
1158+
1159+
test :chmod_with_failue! do
1160+
fixture = tmp_path("tmp_test.txt")
1161+
File.rm(fixture)
1162+
1163+
message = %r"could not change mode for #{escape fixture}: no such file or directory"
1164+
assert_raise File.Error, message, fn ->
1165+
File.chmod!(fixture, 0100777)
1166+
end
1167+
end
1168+
1169+
test :chgrp_with_failue do
1170+
fixture = tmp_path("tmp_test.txt")
1171+
File.rm(fixture)
1172+
1173+
assert File.chgrp(fixture, 1) == {:error,:enoent}
1174+
end
1175+
1176+
test :chgrp_with_failue! do
1177+
fixture = tmp_path("tmp_test.txt")
1178+
File.rm(fixture)
1179+
1180+
message = %r"could not change group for #{escape fixture}: no such file or directory"
1181+
assert_raise File.Error, message, fn ->
1182+
File.chgrp!(fixture, 1)
1183+
end
1184+
end
1185+
1186+
test :chown_with_failue do
1187+
fixture = tmp_path("tmp_test.txt")
1188+
File.rm(fixture)
1189+
1190+
assert File.chown(fixture, 1) == {:error,:enoent}
1191+
end
1192+
1193+
test :chown_with_failue! do
1194+
fixture = tmp_path("tmp_test.txt")
1195+
File.rm(fixture)
1196+
1197+
message = %r"could not change owner for #{escape fixture}: no such file or directory"
1198+
assert_raise File.Error, message, fn ->
1199+
File.chown!(fixture, 1)
1200+
end
1201+
end
1202+
1203+
11181204
defp last_year do
11191205
last_year :calendar.local_time
11201206
end

0 commit comments

Comments
 (0)