|
3 | 3 | % output is string like "rwxrwxr--" |
4 | 4 |
|
5 | 5 | function p = get_permissions(f) |
6 | | -arguments |
7 | | - f (1,1) string |
8 | | -end |
| 6 | +% arguments |
| 7 | +% f (1,1) string |
| 8 | +% end |
9 | 9 |
|
10 | | -p = string.empty; |
| 10 | +p = ""; |
11 | 11 |
|
12 | 12 | % Get the permissions of a file or directory |
13 | 13 | [status, v] = fileattrib(f); |
|
17 | 17 |
|
18 | 18 | % Extract the permission string |
19 | 19 | p = "---------"; % Default permissions |
20 | | -if v.UserRead |
21 | | - p = replaceBetween(p, 1, 1, "r"); |
22 | | -end |
23 | | -if v.UserWrite |
24 | | - p = replaceBetween(p, 2, 2, "w"); |
25 | | -end |
26 | | -if v.UserExecute |
27 | | - p = replaceBetween(p, 3, 3, "x"); |
28 | | -end |
29 | | -if ~isnan(v.GroupRead) && logical(v.GroupRead) |
30 | | - p = replaceBetween(p, 4, 4, "r"); |
31 | | -end |
32 | | -if ~isnan(v.GroupWrite) && logical(v.GroupWrite) |
33 | | - p = replaceBetween(p, 5, 5, "w"); |
34 | | -end |
35 | | -if ~isnan(v.GroupExecute) && logical(v.GroupExecute) |
36 | | - p = replaceBetween(p, 6, 6, "x"); |
37 | | -end |
38 | | -if ~isnan(v.OtherRead) && logical(v.OtherRead) |
39 | | - p = replaceBetween(p, 7, 7, "r"); |
40 | | -end |
41 | | -if ~isnan(v.OtherWrite) && logical(v.OtherWrite) |
42 | | - p = replaceBetween(p, 8, 8, "w"); |
43 | | -end |
44 | | -if ~isnan(v.OtherExecute) && logical(v.OtherExecute) |
45 | | - p = replaceBetween(p, 9, 9, "x"); |
| 20 | + |
| 21 | +groupRead = ~isnan(v.GroupRead) && logical(v.GroupRead); |
| 22 | +groupWrite = ~isnan(v.GroupWrite) && logical(v.GroupWrite); |
| 23 | +groupExecute = ~isnan(v.GroupExecute) && logical(v.GroupExecute); |
| 24 | +otherRead = ~isnan(v.OtherRead) && logical(v.OtherRead); |
| 25 | +otherWrite = ~isnan(v.OtherWrite) && logical(v.OtherWrite); |
| 26 | +otherExecute = ~isnan(v.OtherExecute) && logical(v.OtherExecute); |
| 27 | + |
| 28 | +try |
| 29 | + |
| 30 | +if v.UserRead, p = replaceBetween(p, 1, 1, "r"); end |
| 31 | +if v.UserWrite, p = replaceBetween(p, 2, 2, "w"); end |
| 32 | +if v.UserExecute, p = replaceBetween(p, 3, 3, "x"); end |
| 33 | +if groupRead, p = replaceBetween(p, 4, 4, "r"); end |
| 34 | +if groupWrite, p = replaceBetween(p, 5, 5, "w"); end |
| 35 | +if groupExecute, p = replaceBetween(p, 6, 6, "x"); end |
| 36 | +if otherRead, p = replaceBetween(p, 7, 7, "r"); end |
| 37 | +if otherWrite, p = replaceBetween(p, 8, 8, "w"); end |
| 38 | +if otherExecute, p = replaceBetween(p, 9, 9, "x"); end |
| 39 | + |
| 40 | +catch e |
| 41 | + if ~strcmp(e.identifier, "Octave:undefined-function") |
| 42 | + rethrow(e) |
| 43 | + end |
| 44 | + |
| 45 | + if v.UserRead, p(1) = "r"; end |
| 46 | + if v.UserWrite, p(2) = "w"; end |
| 47 | + if v.UserExecute, p(3) = "x"; end |
| 48 | + if groupRead, p(4) = "r"; end |
| 49 | + if groupWrite, p(5) = "w"; end |
| 50 | + if groupExecute, p(6) = "x"; end |
| 51 | + if otherRead, p(7) = "r"; end |
| 52 | + if otherWrite, p(8) = "w"; end |
| 53 | + if otherExecute, p(9) = "x"; end |
46 | 54 | end |
47 | 55 |
|
48 | 56 | end |
| 57 | + |
| 58 | +%!assert(length(get_permissions('get_permissions.m')) == 9) |
0 commit comments