From 368bb0f7b4a388583c490e694e5fe05807965bc4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lo=C3=AFc=20Hoguin?= Date: Wed, 4 Jan 2012 00:52:41 +0100 Subject: [PATCH] egs_patch: Use code:priv_dir/1 to get the location of the configuration --- apps/egs_patch/src/egs_patch_app.erl | 5 ++-- apps/egs_patch/src/egs_patch_files_db.erl | 31 ++++++++++++----------- apps/egs_patch/src/egs_patch_protocol.erl | 10 +++++--- apps/egs_patch/src/egs_patch_sup.erl | 4 +-- 4 files changed, 25 insertions(+), 25 deletions(-) diff --git a/apps/egs_patch/src/egs_patch_app.erl b/apps/egs_patch/src/egs_patch_app.erl index 6b2e7e6..ad949c0 100644 --- a/apps/egs_patch/src/egs_patch_app.erl +++ b/apps/egs_patch/src/egs_patch_app.erl @@ -1,6 +1,4 @@ -%% @author Loïc Hoguin -%% @copyright 2011 Loïc Hoguin. -%% @doc Callbacks for the egs_patch application. +%% Copyright (c) 2011, Loïc Hoguin %% %% This file is part of EGS. %% @@ -18,6 +16,7 @@ %% along with EGS. If not, see . -module(egs_patch_app). + -behaviour(application). -export([start/2, stop/1]). %% API. diff --git a/apps/egs_patch/src/egs_patch_files_db.erl b/apps/egs_patch/src/egs_patch_files_db.erl index b733fb4..e479eff 100644 --- a/apps/egs_patch/src/egs_patch_files_db.erl +++ b/apps/egs_patch/src/egs_patch_files_db.erl @@ -108,21 +108,22 @@ code_change(_OldVsn, State, _Extra) -> build_state() -> {ok, App} = application:get_application(), - {ok, Terms} = file:consult([code:priv_dir(App), "/patch.conf"]), + PrivDir = code:priv_dir(App), + {ok, Terms} = file:consult([PrivDir, "/patch.conf"]), Folders = proplists:get_value(folders, Terms), - {ListBin, Files} = build_list_bin(Folders, Terms), + {ListBin, Files} = build_list_bin(Folders, Terms, [PrivDir, "/patch/"]), #state{list_bin=ListBin, files=Files}. %% The file number must start at 0. -build_list_bin(Folders, Terms) -> - build_list_bin(Folders, Terms, 0, [], []). -build_list_bin([], _Terms, _N, Acc, FilesAcc) -> +build_list_bin(Folders, Terms, PatchDir) -> + build_list_bin(Folders, Terms, PatchDir, 0, [], []). +build_list_bin([], _Terms, _PatchDir, _N, Acc, FilesAcc) -> Bin = list_to_binary(lists:reverse(Acc)), Bin2 = << 16#08:32/little, 16#06:32/little, Bin/binary, 16#08:32/little, 16#08:32/little >>, {Bin2, lists:flatten(FilesAcc)}; -build_list_bin([Folder|Tail], Terms, N, Acc, FilesAcc) -> +build_list_bin([Folder|Tail], Terms, PatchDir, N, Acc, FilesAcc) -> Filenames = proplists:get_value({folder, Folder}, Terms), - {BinFiles, Files, N2} = build_files_bin(Folder, Filenames, N), + {BinFiles, Files, N2} = build_files_bin(Folder, Filenames, PatchDir, N), BinFiles2 = case Folder of root -> BinFiles; _Any -> @@ -131,17 +132,17 @@ build_list_bin([Folder|Tail], Terms, N, Acc, FilesAcc) -> << 16#48:32/little, 16#09:32/little, FolderBin/binary, 0:Padding, BinFiles/binary, 16#08:32/little, 16#0a:32/little >> end, - build_list_bin(Tail, Terms, N2, [BinFiles2|Acc], [Files|FilesAcc]). + build_list_bin(Tail, Terms, PatchDir, N2, [BinFiles2|Acc], [Files|FilesAcc]). -build_files_bin(Folder, Filenames, N) -> - build_files_bin(Folder, Filenames, N, [], []). -build_files_bin(_Folder, [], N, Acc, FilesAcc) -> +build_files_bin(Folder, Filenames, PatchDir, N) -> + build_files_bin(Folder, Filenames, PatchDir, N, [], []). +build_files_bin(_Folder, [], _PatchDir, N, Acc, FilesAcc) -> Bin = list_to_binary(lists:reverse(Acc)), {Bin, FilesAcc, N}; -build_files_bin(Folder, [Filename|Tail], N, Acc, FilesAcc) -> +build_files_bin(Folder, [Filename|Tail], PatchDir, N, Acc, FilesAcc) -> FullFilename = case Folder of - root -> ["priv/patch/"|Filename]; - _Any -> ["priv/patch/",Folder,"/"|Filename] + root -> [PatchDir|Filename]; + _Any -> [PatchDir,Folder,"/"|Filename] end, Size = file_get_size(FullFilename), CRC = file_get_crc(FullFilename), @@ -149,7 +150,7 @@ build_files_bin(Folder, [Filename|Tail], N, Acc, FilesAcc) -> Padding = 8 * (64 - length(Filename)), FilenameBin2 = << FilenameBin/binary, 0:Padding >>, Bin = << 16#4c:32/little, 16#07:32/little, N:32/little, FilenameBin2/binary >>, - build_files_bin(Folder, Tail, N + 1, [Bin|Acc], [{N, #file{crc=CRC, size=Size, folder=Folder, filename_bin=FilenameBin2, full_filename=FullFilename}}|FilesAcc]). + build_files_bin(Folder, Tail, PatchDir, N + 1, [Bin|Acc], [{N, #file{crc=CRC, size=Size, folder=Folder, filename_bin=FilenameBin2, full_filename=FullFilename}}|FilesAcc]). file_get_size(Filename) -> {ok, FileInfo} = file:read_file_info(Filename), diff --git a/apps/egs_patch/src/egs_patch_protocol.erl b/apps/egs_patch/src/egs_patch_protocol.erl index 9df46d6..3ed3e0e 100644 --- a/apps/egs_patch/src/egs_patch_protocol.erl +++ b/apps/egs_patch/src/egs_patch_protocol.erl @@ -1,6 +1,4 @@ -%% @author Loïc Hoguin -%% @copyright 2010-2011 Loïc Hoguin. -%% @doc Cowboy protocol module for the patch server. +%% Copyright (c) 2011, Loïc Hoguin %% %% This file is part of EGS. %% @@ -17,9 +15,12 @@ %% You should have received a copy of the GNU Affero General Public License %% along with EGS. If not, see . +%% @doc Cowboy protocol module for the patch server. -module(egs_patch_protocol). + -export([start_link/4, init/2]). +%% @todo Move that in a configuration file. -define(TIMEOUT, 5000). -record(state, { @@ -177,7 +178,8 @@ send_0f(#state{socket=Socket, transport=Transport, files=Files}) -> Size = lists:foldl( fun(N, Acc) -> Acc + egs_patch_files_db:get_size(N) end, 0, Files), NbFiles = length(Files), - Bin = << 16#10:32/little, 16#0f:32/little, Size:32/little, NbFiles:32/little >>, + Bin = << 16#10:32/little, 16#0f:32/little, + Size:32/little, NbFiles:32/little >>, ok = Transport:send(Socket, Bin). -spec send_10(state(), non_neg_integer(), binary()) -> ok. diff --git a/apps/egs_patch/src/egs_patch_sup.erl b/apps/egs_patch/src/egs_patch_sup.erl index efbe2ab..7b79fc9 100644 --- a/apps/egs_patch/src/egs_patch_sup.erl +++ b/apps/egs_patch/src/egs_patch_sup.erl @@ -1,6 +1,4 @@ -%% @author Loïc Hoguin -%% @copyright 2011 Loïc Hoguin. -%% @doc Top-level supervisor for the egs_patch application. +%% Copyright (c) 2011, Loïc Hoguin %% %% This file is part of EGS. %%