couchdb-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From nsla...@apache.org
Subject [22/30] git commit: improve file I/O error logging and handling
Date Sat, 17 Nov 2012 19:52:50 GMT
improve file I/O error logging and handling

It's better to let these errors bubble and/or not give them special
treatment when file:format_error/1 can do a better job of describing
the failure.

This is a forward-port of work done on 1.2.x, encompassing the
following commits:

04c4a1041de06e8e39983cf9616c8baf9d889106
ede9482fc3c9e629572ab1376d88f4499c4c8beb
cd238b42d1333cece5ab899c35c3b83ada8d448a
af9e1c883ca4accb209cccc7ac9d26efa4daf1fd
ba8fa86890087650a02a8ebaaea2537468999472


Project: http://git-wip-us.apache.org/repos/asf/couchdb/repo
Commit: http://git-wip-us.apache.org/repos/asf/couchdb/commit/4b6475da
Tree: http://git-wip-us.apache.org/repos/asf/couchdb/tree/4b6475da
Diff: http://git-wip-us.apache.org/repos/asf/couchdb/diff/4b6475da

Branch: refs/heads/docs
Commit: 4b6475da2cd9ff392736e52a6b834e7827fe3ee3
Parents: b99ec79
Author: Randall Leeds <randall@apache.org>
Authored: Sun Mar 18 20:19:27 2012 -0700
Committer: Randall Leeds <randall@apache.org>
Committed: Thu Oct 25 09:41:08 2012 -0700

----------------------------------------------------------------------
 src/couchdb/couch_config.erl        |   11 ++++-------
 src/couchdb/couch_config_writer.erl |    8 +++++---
 src/couchdb/couch_file.erl          |   14 ++++++++------
 src/couchdb/couch_log.erl           |    8 ++++----
 src/couchdb/couch_server.erl        |    2 ++
 src/couchdb/couch_server_sup.erl    |   14 +++++++-------
 6 files changed, 30 insertions(+), 27 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/couchdb/blob/4b6475da/src/couchdb/couch_config.erl
----------------------------------------------------------------------
diff --git a/src/couchdb/couch_config.erl b/src/couchdb/couch_config.erl
index 79effda..96fabba 100644
--- a/src/couchdb/couch_config.erl
+++ b/src/couchdb/couch_config.erl
@@ -187,13 +187,10 @@ parse_ini_file(IniFile) ->
     case file:read_file(IniFilename) of
         {ok, IniBin0} ->
             IniBin0;
-        {error, eacces} ->
-            throw({file_permission_error, IniFile});
-        {error, enoent} ->
-            Fmt = "Couldn't find server configuration file ~s.",
-            Msg = ?l2b(io_lib:format(Fmt, [IniFilename])),
-            ?LOG_ERROR("~s~n", [Msg]),
-            throw({startup_error, Msg})
+        {error, Reason} = Error ->
+            ?LOG_ERROR("Could not read server configuration file ~s: ~s",
+                [IniFilename, file:format_error(Reason)]),
+            throw(Error)
     end,
 
     Lines = re:split(IniBin, "\r\n|\n|\r|\032", [{return, list}]),

http://git-wip-us.apache.org/repos/asf/couchdb/blob/4b6475da/src/couchdb/couch_config_writer.erl
----------------------------------------------------------------------
diff --git a/src/couchdb/couch_config_writer.erl b/src/couchdb/couch_config_writer.erl
index decd269..21f1c3f 100644
--- a/src/couchdb/couch_config_writer.erl
+++ b/src/couchdb/couch_config_writer.erl
@@ -22,6 +22,8 @@
 
 -export([save_to_file/2]).
 
+-include("couch_db.hrl").
+
 %% @spec save_to_file(
 %%           Config::{{Section::string(), Option::string()}, Value::string()},
 %%           File::filename()) -> ok
@@ -38,9 +40,9 @@ save_to_file({{Section, Key}, Value}, File) ->
     case file:write_file(File, NewFileContents) of
     ok ->
         ok;
-    {error, eacces} ->
-        {file_permission_error, File};
-    Error ->
+    {error, Reason} = Error ->
+        ?LOG_ERROR("Could not write config file ~s: ~s",
+            [File, file:format_error(Reason)]),
         Error
     end.
 

http://git-wip-us.apache.org/repos/asf/couchdb/blob/4b6475da/src/couchdb/couch_file.erl
----------------------------------------------------------------------
diff --git a/src/couchdb/couch_file.erl b/src/couchdb/couch_file.erl
index 2c2f11a..e00b0f0 100644
--- a/src/couchdb/couch_file.erl
+++ b/src/couchdb/couch_file.erl
@@ -53,17 +53,19 @@ open(Filepath, Options) ->
     ignore ->
         % get the error
         receive
-        {Ref, Pid, Error} ->
+        {Ref, Pid, {error, Reason} = Error} ->
             case process_info(self(), trap_exit) of
             {trap_exit, true} -> receive {'EXIT', Pid, _} -> ok end;
             {trap_exit, false} -> ok
             end,
-            case Error of
-            {error, eacces} -> {file_permission_error, Filepath};
-            _ -> Error
-            end
+            ?LOG_DEBUG("Could not open file ~s: ~s",
+                [Filepath, file:format_error(Reason)]),
+            Error
         end;
     Error ->
+        % We can't say much here, because it could be any kind of error.
+        % Just let it bubble and an encapsulating subcomponent can perhaps
+        % be more informative. It will likely appear in the SASL log, anyway.
         Error
     end.
 
@@ -290,7 +292,7 @@ init({Filepath, Options, ReturnPid, Ref}) ->
                     {ok, #file{fd=Fd}};
                 false ->
                     ok = file:close(Fd),
-                    init_status_error(ReturnPid, Ref, file_exists)
+                    init_status_error(ReturnPid, Ref, {error, eexist})
                 end;
             false ->
                 maybe_track_open_os_files(Options),

http://git-wip-us.apache.org/repos/asf/couchdb/blob/4b6475da/src/couchdb/couch_log.erl
----------------------------------------------------------------------
diff --git a/src/couchdb/couch_log.erl b/src/couchdb/couch_log.erl
index a38c38a..fc7b393 100644
--- a/src/couchdb/couch_log.erl
+++ b/src/couchdb/couch_log.erl
@@ -89,10 +89,10 @@ init([]) ->
     case file:open(Filename, [append]) of
     {ok, Fd} ->
         {ok, #state{fd = Fd, level = Level, sasl = Sasl}};
-    {error, eacces} ->
-        {stop, {file_permission_error, Filename}};
-    Error ->
-        {stop, Error}
+    {error, Reason} ->
+        ReasonStr = file:format_error(Reason),
+        io:format("Error opening log file ~s: ~s", [Filename, ReasonStr]),
+        {stop, {error, ReasonStr, Filename}}
     end.
 
 debug_on() ->

http://git-wip-us.apache.org/repos/asf/couchdb/blob/4b6475da/src/couchdb/couch_server.erl
----------------------------------------------------------------------
diff --git a/src/couchdb/couch_server.erl b/src/couchdb/couch_server.erl
index 0f40c51..694daee 100644
--- a/src/couchdb/couch_server.erl
+++ b/src/couchdb/couch_server.erl
@@ -319,6 +319,8 @@ handle_call({open_result, DbName, {ok, OpenedDbPid}, Options}, _From,
Server) ->
         ok
     end,
     {reply, ok, Server};
+handle_call({open_result, DbName, {error, eexist}, Options}, From, Server) ->
+    handle_call({open_result, DbName, file_exists, Options}, From, Server);
 handle_call({open_result, DbName, Error, Options}, _From, Server) ->
     [{DbName, {opening,Opener,Froms}}] = ets:lookup(couch_dbs_by_name, DbName),
     lists:foreach(fun(From) ->

http://git-wip-us.apache.org/repos/asf/couchdb/blob/4b6475da/src/couchdb/couch_server_sup.erl
----------------------------------------------------------------------
diff --git a/src/couchdb/couch_server_sup.erl b/src/couchdb/couch_server_sup.erl
index 7baede3..be3c3a3 100644
--- a/src/couchdb/couch_server_sup.erl
+++ b/src/couchdb/couch_server_sup.erl
@@ -46,7 +46,9 @@ start_server(IniFiles) ->
     {ok, [PidFile]} ->
         case file:write_file(PidFile, os:getpid()) of
         ok -> ok;
-        Error -> io:format("Failed to write PID file ~s, error: ~p", [PidFile, Error])
+        {error, Reason} ->
+            io:format("Failed to write PID file ~s: ~s",
+                [PidFile, file:format_error(Reason)])
         end;
     _ -> ok
     end,
@@ -121,12 +123,10 @@ start_server(IniFiles) ->
             end end || Uri <- Uris],
         case file:write_file(UriFile, Lines) of
         ok -> ok;
-        {error, eacces} ->
-            ?LOG_ERROR("Permission error when writing to URI file ~s", [UriFile]),
-            throw({file_permission_error, UriFile});
-        Error2 ->
-            ?LOG_ERROR("Failed to write to URI file ~s: ~p~n", [UriFile, Error2]),
-            throw(Error2)
+        {error, Reason2} = Error ->
+            ?LOG_ERROR("Failed to write to URI file ~s: ~s",
+                [UriFile, file:format_error(Reason2)]),
+            throw(Error)
         end
     end,
 


Mime
View raw message