/run Torrent client/writer.erl
Erlang | 109 lines | 60 code | 12 blank | 37 comment | 0 complexity | 7db93aae6ac7d55f78c56b577704a6c6 MD5 | raw file
1-module(writer). 2-export([write_pieces/6,write_Lpiece/5,write_files/7,write_file/5]). 3-record (files_data,{filename,path,size,passed_bytes}). 4-include("defs.hrl"). 5 6 7 write_files(DefPath,Bin_Piece,Piece_index,PieceSize,Files_dict,N,false)-> % it is not the last piece 8 %% io:format("in the write files function ~n"), 9 %% io:format("DefPath: ~p~n",[DefPath]), 10 %% io:format("Bin_Piece: ~p~n",[Bin_Piece]), 11 %% io:format("Piece_index: ~p~n",[Piece_index]), 12 %% io:format("PieceSize: ~p~n",[PieceSize]), 13 %% io:format("Files_dict: ~p~n",[Files_dict]), 14 %% io:format("N: ~p~n",[N]), 15 write_pieces(DefPath,Bin_Piece,Piece_index,PieceSize,Files_dict,N); 16 write_files(DefPath,Bin_Piece,Piece_index,PieceSize,Files_dict,_N,true)-> % it is the last piece 17 write_Lpiece(DefPath,Bin_Piece,Piece_index,PieceSize,Files_dict)% Piece size has to be a normal one even here!!!!! 18 . 19 20 write_Lpiece(DefPath,Bin_Piece,Piece_index,PieceSize,Files_dict)-> % The starting position has to be based on a normal piece even here!!!!!!! 21 %% this is the last piece 22 {ok,Rec} = dict:find(dict:size(Files_dict),Files_dict), 23 Path = Rec#files_data.path, 24 Name = Rec#files_data.filename, 25 {ok,Io} = file:open(DefPath++"/"++ Path ++ "/"++Name,[read,write,raw]), 26 file:pwrite(Io,Piece_index * PieceSize,Bin_Piece), 27 file:close(Io). 28 29 write_pieces(DefPath,Bin_Piece,Piece_index,PieceSize,Files_dict,N)-> 30 %% io:format("in the write_pieces~n"), 31 %% io:format("DefPath: ~p~n",[DefPath]), 32 %% io:format("Bin_Piece: ~p~n",[Bin_Piece]), 33 %% io:format("Piece_index: ~p~n",[Piece_index]), 34 %% io:format("PieceSize: ~p~n",[PieceSize]), 35 %% io:format("Files_dict: ~p~n",[Files_dict]), 36 %% io:format("N: ~p~n",[N]), 37 %% this is not the last piece 38 Start_Position = Piece_index * PieceSize, 39 % io:format("Start_position: ~p~n",[Start_Position]), 40 {ok,Rec} = dict:find(N,Files_dict), 41 % io:format("FileRec: ~p~n",[Rec]), 42 43 case Start_Position =< ((Rec#files_data.passed_bytes + Rec#files_data.size) - 1) of 44 45 %% if this is the right file to write to. 46 47 true -> 48% io:format("this is the right file to write to ~n"), 49 case ((Start_Position + PieceSize)=<(Rec#files_data.passed_bytes + Rec#files_data.size)) of 50 %% if the file can include the whole piece 51 true -> 52% io:format("file can include the whole piece ~n"), 53 Path = Rec#files_data.path, 54 Name = Rec#files_data.filename, 55 {ok,Io} = file:open(DefPath++"/"++ Path ++ "/"++Name,[read,write,raw]), 56 file:pwrite(Io,Start_Position,Bin_Piece), 57 % io:format("in the write_pieces: piece was written ~n"), 58 file:close(Io) 59 ; 60 %% if the file can include some of the piece 61 false -> 62 Allowed = ((Rec#files_data.passed_bytes + Rec#files_data.size) - Start_Position), 63 % io:format("allowed bytes: ~p~n",[Allowed]), 64 case Allowed /= PieceSize-1 of 65 true -> 66 Part1 = binary:part(Bin_Piece,0,Allowed), 67% io:format("before part2"), 68 Part2 = binary:part(Bin_Piece,Allowed,PieceSize-Allowed), 69% io:format("after part2"), 70 Path = Rec#files_data.path, 71 Name = Rec#files_data.filename, 72 {ok,Io} = file:open(DefPath++"/"++ Path ++ "/"++Name,[read,write,raw]), 73 file:pwrite(Io,Start_Position,Part1), 74 file:close(Io), 75 write_pieces(DefPath,Part2,0,PieceSize,Files_dict,N+1); 76 false -> 77 Part1 = binary:part(Bin_Piece,0,Allowed), 78 Part2 = binary:last(Bin_Piece), 79 Path = Rec#files_data.path, 80 Name = Rec#files_data.filename, 81 {ok,Io} = file:open(DefPath++"/"++ Path ++ "/"++Name,[read,write,raw]), 82 file:pwrite(Io,Start_Position,Part1), 83 file:close(Io), 84 write_pieces(DefPath,Part2,0,PieceSize,Files_dict,N+1) 85 end 86 87 end; 88 %% if this is not the right file to write to. 89 false -> 90% io:format("this is not the write file to write to"), 91 write_pieces(DefPath,Bin_Piece,Piece_index,PieceSize,Files_dict,N+1) 92 end . 93 94 95 write_file(DefPath,Bin_Piece,Piece_index,PieceSize,FileName)-> 96 % io:format("in the writefile: ~n"), 97 Start_Position = Piece_index * PieceSize, 98 % io:format("Start_Position: ~p~n",[Start_Position]), 99 % io:format("DefPath: ~p~n" , [DefPath]), 100 % io:format("Bin_Piece: ~p~n", [Bin_Piece]), 101 %% io:format("Piece_index: ~p~n",[PieceSize]), 102 %% io:format("PieceSize: ~p~n",[PieceSize]), 103 %% io:format("FileName: ~p~n",[FileName]), 104 105 {ok,Io} = file:open(DefPath++"/"++FileName,[read,write,raw]), 106 file:pwrite(Io,Start_Position,Bin_Piece), 107 % io:format("Piece was written: ~n"), 108 file:close(Io). 109