aboutsummaryrefslogtreecommitdiffstats
path: root/erts/preloaded/src/prim_file.erl
diff options
context:
space:
mode:
authorBjörn-Egil Dahlberg <[email protected]>2011-12-06 20:12:30 +0100
committerBjörn-Egil Dahlberg <[email protected]>2011-12-08 14:12:02 +0100
commit58c4c46694982b59dc52d0443c426009c0910e53 (patch)
tree260f7d1877ac43485e7c28127d00f1527357a41c /erts/preloaded/src/prim_file.erl
parentc27463eecbe3ff8ee3416d7b6f3607d25e1bf01d (diff)
downloadotp-58c4c46694982b59dc52d0443c426009c0910e53.tar.gz
otp-58c4c46694982b59dc52d0443c426009c0910e53.tar.bz2
otp-58c4c46694982b59dc52d0443c426009c0910e53.zip
Catch errors from prim_file:*_file_info
We do not want to crash the file server.
Diffstat (limited to 'erts/preloaded/src/prim_file.erl')
-rw-r--r--erts/preloaded/src/prim_file.erl213
1 files changed, 43 insertions, 170 deletions
diff --git a/erts/preloaded/src/prim_file.erl b/erts/preloaded/src/prim_file.erl
index 507ac9c360..2567388a6a 100644
--- a/erts/preloaded/src/prim_file.erl
+++ b/erts/preloaded/src/prim_file.erl
@@ -739,13 +739,17 @@ read_file_info(Port, File, Opts) when is_port(Port) ->
read_file_info_int(Port, File, plgv(time, Opts, local)).
read_file_info_int(Port, File, TimeType) ->
- case drv_command(Port, [?FILE_FSTAT, pathname(File)]) of
- {ok, FI} -> {ok, FI#file_info{
- ctime = from_seconds(FI#file_info.ctime, TimeType),
- mtime = from_seconds(FI#file_info.mtime, TimeType),
- atime = from_seconds(FI#file_info.atime, TimeType)
- }};
- Error -> Error
+ try
+ case drv_command(Port, [?FILE_FSTAT, pathname(File)]) of
+ {ok, FI} -> {ok, FI#file_info{
+ ctime = from_seconds(FI#file_info.ctime, TimeType),
+ mtime = from_seconds(FI#file_info.mtime, TimeType),
+ atime = from_seconds(FI#file_info.atime, TimeType)
+ }};
+ Error -> Error
+ end
+ catch
+ error:_ -> {error, badarg}
end.
@@ -787,24 +791,28 @@ write_file_info_int(Port, File,
% - use atime as mtime if mtime is undefined
% - use mtime as ctime if ctime is undefined
- Atime = file_info_validate_atime(Atime0, TimeType),
- Mtime = file_info_validate_mtime(Mtime0, Atime),
- Ctime = file_info_validate_ctime(Ctime0, Mtime),
-
- drv_command(Port, [?FILE_WRITE_INFO,
- int_to_int32bytes(Mode),
- int_to_int32bytes(Uid),
- int_to_int32bytes(Gid),
- int_to_int64bytes(to_seconds(Atime, TimeType)),
- int_to_int64bytes(to_seconds(Mtime, TimeType)),
- int_to_int64bytes(to_seconds(Ctime, TimeType)),
- pathname(File)]).
+ try
+ Atime = file_info_validate_atime(Atime0, TimeType),
+ Mtime = file_info_validate_mtime(Mtime0, Atime),
+ Ctime = file_info_validate_ctime(Ctime0, Mtime),
+
+ drv_command(Port, [?FILE_WRITE_INFO,
+ int_to_int32bytes(Mode),
+ int_to_int32bytes(Uid),
+ int_to_int32bytes(Gid),
+ int_to_int64bytes(to_seconds(Atime, TimeType)),
+ int_to_int64bytes(to_seconds(Mtime, TimeType)),
+ int_to_int64bytes(to_seconds(Ctime, TimeType)),
+ pathname(File)])
+ catch
+ error:_ -> {error, badarg}
+ end.
file_info_validate_atime(Atime, _) when Atime =/= undefined -> Atime;
file_info_validate_atime(undefined, local) -> erlang:localtime();
file_info_validate_atime(undefined, utc) -> erlang:universaltime();
-file_info_validate_atime(undefined, epoch) -> datetime_to_epoch(erlang:universaltime()).
+file_info_validate_atime(undefined, epoch) -> erlang:universaltime_to_seconds((erlang:universaltime())).
file_info_validate_mtime(undefined, Atime) -> Atime;
file_info_validate_mtime(Mtime, _) -> Mtime.
@@ -867,13 +875,17 @@ read_link_info(Port, Link, Opts) when is_port(Port) ->
read_link_info_int(Port, Link, TimeType) ->
- case drv_command(Port, [?FILE_LSTAT, pathname(Link)]) of
- {ok, FI} -> {ok, FI#file_info{
- ctime = from_seconds(FI#file_info.ctime, TimeType),
- mtime = from_seconds(FI#file_info.mtime, TimeType),
- atime = from_seconds(FI#file_info.atime, TimeType)
- }};
- Error -> Error
+ try
+ case drv_command(Port, [?FILE_LSTAT, pathname(Link)]) of
+ {ok, FI} -> {ok, FI#file_info{
+ ctime = from_seconds(FI#file_info.ctime, TimeType),
+ mtime = from_seconds(FI#file_info.mtime, TimeType),
+ atime = from_seconds(FI#file_info.atime, TimeType)
+ }};
+ Error -> Error
+ end
+ catch
+ error:_ -> {error, badarg}
end.
%% list_dir/{1,2}
@@ -1329,160 +1341,21 @@ plgv(K, [{K, V}|_], _) -> V;
plgv(K, [_|KVs], D) -> plgv(K, KVs, D);
plgv(_, [], D) -> D.
-
%%
%% We don't actually want this here
%% We want to use epochs in all prim but erl_prim_loader makes that tricky
%% It is probably needed to redo the whole erl_prim_loader
--define(DAYS_FROM_0_TO_1970, 719528).
--define(SECONDS_PER_DAY, 86400).
--define(SECONDS_PER_HOUR, 3600).
--define(SECONDS_PER_MINUTE, 60).
--define(DAYS_PER_YEAR, 365).
--define(DAYS_PER_LEAP_YEAR, 366).
-
from_seconds(Seconds, epoch) when is_integer(Seconds) ->
Seconds;
from_seconds(Seconds, utc) when is_integer(Seconds) ->
- epoch_to_datetime(Seconds);
+ erlang:seconds_to_universaltime(Seconds);
from_seconds(Seconds, local) when is_integer(Seconds) ->
- erlang:universaltime_to_localtime(epoch_to_datetime(Seconds)).
-
-epoch_to_datetime(Seconds) ->
- gregorian_seconds_to_datetime(Seconds + ?SECONDS_PER_DAY * ?DAYS_FROM_0_TO_1970).
+ erlang:universaltime_to_localtime(erlang:seconds_to_universaltime(Seconds)).
to_seconds(Seconds, epoch) when is_integer(Seconds) ->
Seconds;
to_seconds({_,_} = Datetime, utc) ->
- datetime_to_epoch(Datetime);
+ erlang:universaltime_to_seconds(Datetime);
to_seconds({_,_} = Datetime, local) ->
- datetime_to_epoch(erlang:localtime_to_universaltime(Datetime));
-to_seconds(undefined, _) ->
- to_seconds(erlang:universaltime(), utc).
-
-datetime_to_epoch(Datetime) ->
- datetime_to_gregorian_seconds(Datetime) - ?SECONDS_PER_DAY * ?DAYS_FROM_0_TO_1970.
-
-
-%% erl_tar uses this convention (investigate)
-%%
-%% posix_time(Time) ->
-%% EpochStart = {{1970,1,1},{0,0,0}},
-%% {Days,{Hour,Min,Sec}} = calendar:time_difference(EpochStart, Time),
-%% 86400*Days + 3600*Hour + 60*Min + Sec.
-%%
-%% posix_to_erlang_time(Sec) ->
-%% OneMillion = 1000000,
-%% Time = calendar:now_to_datetime({Sec div OneMillion, Sec rem OneMillion, 0}),
-%% erlang:universaltime_to_localtime(Time).
-
-
-% from calendar, slightly modified
-
-%% calendar:datetime_to_gregorian_seconds/1
-
-datetime_to_gregorian_seconds({Date, Time}) ->
- ?SECONDS_PER_DAY*date_to_gregorian_days(Date) + time_to_seconds(Time).
-
-
-time_to_seconds({H, M, S}) when is_integer(H), is_integer(M), is_integer(S) ->
- H * ?SECONDS_PER_HOUR + M * ?SECONDS_PER_MINUTE + S.
-
-date_to_gregorian_days({Year, Month, Day}) ->
- date_to_gregorian_days(Year, Month, Day).
-
-date_to_gregorian_days(Year, Month, Day) when is_integer(Day), Day > 0 ->
- Last = last_day_of_the_month(Year, Month),
- if
- Day =< Last ->
- dy(Year) + dm(Month) + df(Year, Month) + Day - 1
- end.
-
-last_day_of_the_month(Y, M) when is_integer(Y), Y >= 0 ->
- last_day_of_the_month1(Y, M).
-
-last_day_of_the_month1(_, 4) -> 30;
-last_day_of_the_month1(_, 6) -> 30;
-last_day_of_the_month1(_, 9) -> 30;
-last_day_of_the_month1(_,11) -> 30;
-last_day_of_the_month1(Y, 2) ->
- case is_leap_year(Y) of
- true -> 29;
- _ -> 28
- end;
-last_day_of_the_month1(_, M) when is_integer(M), M > 0, M < 13 ->
- 31.
-
-dm(1) -> 0; dm(2) -> 31; dm(3) -> 59; dm(4) -> 90;
-dm(5) -> 120; dm(6) -> 151; dm(7) -> 181; dm(8) -> 212;
-dm(9) -> 243; dm(10) -> 273; dm(11) -> 304; dm(12) -> 334.
-
-df(_, Month) when Month < 3 -> 0;
-df(Year, _) ->
- case is_leap_year(Year) of
- true -> 1;
- false -> 0
- end.
-
-
-
-%% calendar:gregorian_seconds_to_datetime/1
-
-gregorian_seconds_to_datetime(Secs) when Secs >= 0 ->
- Days = Secs div ?SECONDS_PER_DAY,
- Rest = Secs rem ?SECONDS_PER_DAY,
- {gregorian_days_to_date(Days), seconds_to_time(Rest)}.
-
-seconds_to_time(Secs0) when Secs0 >= 0, Secs0 < ?SECONDS_PER_DAY ->
- Hour = Secs0 div ?SECONDS_PER_HOUR,
- Secs1 = Secs0 rem ?SECONDS_PER_HOUR,
- Minute = Secs1 div ?SECONDS_PER_MINUTE,
- Second = Secs1 rem ?SECONDS_PER_MINUTE,
- {Hour, Minute, Second}.
-
-gregorian_days_to_date(Days) ->
- {Year, DayOfYear} = day_to_year(Days),
- {Month, DayOfMonth} = year_day_to_date(Year, DayOfYear),
- {Year, Month, DayOfMonth}.
-
-day_to_year(DayOfEpoch) when DayOfEpoch >= 0 ->
- Y0 = DayOfEpoch div ?DAYS_PER_YEAR,
- {Y1, D1} = dty(Y0, DayOfEpoch, dy(Y0)),
- {Y1, DayOfEpoch - D1}.
-
-dty(Y, D1, D2) when D1 < D2 -> dty(Y-1, D1, dy(Y-1));
-dty(Y, _D1, D2) -> {Y, D2}.
-
-dy(Y) when Y =< 0 -> 0;
-dy(Y) ->
- X = Y - 1,
- (X div 4) - (X div 100) + (X div 400) + X*?DAYS_PER_YEAR + ?DAYS_PER_LEAP_YEAR.
-
-year_day_to_date(Year, DayOfYear) ->
- ExtraDay = case is_leap_year(Year) of
- true -> 1;
- false -> 0
- end,
- {Month, Day} = year_day_to_date2(ExtraDay, DayOfYear),
- {Month, Day + 1}.
-
-year_day_to_date2(_, Day) when Day < 31 -> {1, Day};
-year_day_to_date2(E, Day) when 31 =< Day, Day < 59 + E -> {2, Day - 31};
-year_day_to_date2(E, Day) when 59 + E =< Day, Day < 90 + E -> { 3, Day - ( 59 + E)};
-year_day_to_date2(E, Day) when 90 + E =< Day, Day < 120 + E -> { 4, Day - ( 90 + E)};
-year_day_to_date2(E, Day) when 120 + E =< Day, Day < 151 + E -> { 5, Day - (120 + E)};
-year_day_to_date2(E, Day) when 151 + E =< Day, Day < 181 + E -> { 6, Day - (151 + E)};
-year_day_to_date2(E, Day) when 181 + E =< Day, Day < 212 + E -> { 7, Day - (181 + E)};
-year_day_to_date2(E, Day) when 212 + E =< Day, Day < 243 + E -> { 8, Day - (212 + E)};
-year_day_to_date2(E, Day) when 243 + E =< Day, Day < 273 + E -> { 9, Day - (243 + E)};
-year_day_to_date2(E, Day) when 273 + E =< Day, Day < 304 + E -> {10, Day - (273 + E)};
-year_day_to_date2(E, Day) when 304 + E =< Day, Day < 334 + E -> {11, Day - (304 + E)};
-year_day_to_date2(E, Day) when 334 + E =< Day -> {12, Day - (334 + E)}.
-
-is_leap_year(Y) when is_integer(Y), Y >= 0 -> is_leap_year1(Y).
-is_leap_year1(Year) when Year rem 4 =:= 0, Year rem 100 > 0 -> true;
-is_leap_year1(Year) when Year rem 400 =:= 0 -> true;
-is_leap_year1(_) -> false.
-
-
+ erlang:universaltime_to_seconds(erlang:localtime_to_universaltime(Datetime)).