home *** CD-ROM | disk | FTP | other *** search
- unit FtpCache;
-
- {Cache implementation of Monster FTP}
-
- interface
-
- uses Classes, SysUtils, Windows, FtpData, ftpmisc;
-
- {$I mftp.inc}
-
- procedure InitCache;
- function GetCacheFilename(Server, FUser, FDirectory: String; Port: Integer; Folder: Boolean): String;
-
- function LoadFromCache(F: String; D: TMFtpFileInfoList; Expire: Integer): Boolean;
- procedure SaveToCache(F: String; D: TMFtpFileInfoList);
-
- function BuildDateValue(DT: TDateTime): Longint;
-
- function CleanCache(CleanDirectory, CleanFile: Boolean; Expire: Integer): Integer;
-
- implementation
-
- {$ifdef USE_MSDPE}
- uses ImageHlp;
- {$endif}
-
- var CacheDirectory: String;
- DayValue: Longint;
-
- procedure InitCache;
- begin
- CacheDirectory := GetWindowsDirectory + '\ftpcache\';
- {$ifndef USE_MSDPE}
- CreateDirectoryA(PChar(CacheDirectory), nil); {ignore errors}
- {$else}
- MakeSureDirectoryPathExists(PChar(CacheDirectory));
- {$endif}
- FileSetAttr(CacheDirectory, faHidden or faSysFile);
-
- DayValue := BuildDateValue(Now);
- end;
-
- function GetCacheFilename;
- var S: String;
- begin
- if (LowerCase(FUser) = 'ftp') or (LowerCase(FUser) = 'anonymous') then
- S := Server + '[' + IntToStr(Port) + ']-' + FDirectory
- else
- S := Server + '[' + IntToStr(Port) + ']-' + '(' + FUser + ')-' + FDirectory;
-
- Result := CacheDirectory + ReplaceInvalidChars(S, '~') + '.cache';
-
- if Folder then
- Result := Result + 'd'
- else
- Result := Result + 'f';
- end;
-
- function LoadFromCache;
- var Count, i: Integer;
- R: TSearchRec;
- S: Array[0..7] of String;
- C: TextFile;
- FD: TDateTime;
- begin
- try
- if FindFirst(F, faHidden + faSysFile, R) <> 0 then
- begin
- Result := False;
- Exit;
- end;
-
- if Expire > 0 then
- begin
- FD := FileDateToDateTime(R.Time);
- if BuildDateValue(FD) + Expire <= DayValue then
- begin
- Result := False;
- Exit;
- end;
- end;
-
- AssignFile(C, F);
- Reset(C);
-
- D.Clear;
- Readln(C, Count);
- for i := 0 to Count - 1 do
- begin
- Readln(C, S[0]);
- Readln(C, S[1]);
- Readln(C, S[2]);
- Readln(C, S[3]);
- Readln(C, S[4]);
- Readln(C, S[5]);
- Readln(C, S[6]);
- Readln(C, S[7]);
- D.Add(S[0], S[1], S[2], S[3], S[4], S[5], S[6], S[7]);
- end;
-
- CloseFile(C);
- Result := True;
- except
- Result := False;
- end;
- end;
-
- procedure SaveToCache;
- var i: Integer;
- C: TextFile;
- begin
- try
- AssignFile(C, F);
- Rewrite(C);
-
- Writeln(C, D.Count);
- for i := 0 to D.Count - 1 do
- begin
- Writeln(C, D[i].Filename);
- Writeln(C, D[i].Attrib);
- Writeln(C, D[i].DateTime);
- Writeln(C, D[i].Size);
- Writeln(C, D[i].SymbolLink);
- Writeln(C, D[i].Owner);
- Writeln(C, D[i].Group);
- Writeln(C, D[i].Description);
- end;
-
- CloseFile(C);
- // if not Win32Platform <> VER_PLATFORM_WIN32_NT then FileSetAttr(F, faHidden or faSysFile);
- except;
- InitCache;
- end;
- end;
-
- function CleanCache;
- var i: Integer;
- FD: TDateTime;
- R: TSearchRec;
- S: String;
- begin
- Result := 0;
- if (CleanFile = False) and (CleanDirectory = False) then Exit;
-
- i := FindFirst(CacheDirectory + '*.*', faHidden + faSysFile, R);
-
- while i = 0 do
- begin
- if ((CleanDirectory) and (Pos('.cached', R.Name) > 0)) or
- ((CleanFile) and (Pos('.cachef', R.Name) > 0)) then
- begin
- if Expire > 0 then
- begin
- FD := FileDateToDateTime(R.Time);
- if BuildDateValue(FD) + Expire <= DayValue then
- begin
- S := CacheDirectory + String(R.Name);
- if DeleteFile(PChar(S)) then Inc(Result);
- end;
- end;
- end;
-
- i := FindNext(R);
- end;
- end;
-
- function BuildDateValue(DT: TDateTime): Longint;
- var Year, Month, Day:Word;
- begin
- {$ifdef OPTIMIZATION}
- optimizedDecodeDate(DT, Year, Month, Day);
- {$else}
- DecodeDate(DT, Year, Month, Day);
- {$endif}
- Result := Year * 10000 + Month * 100 + Day;
- end;
-
- initialization
- FileMode := 2;
- InitCache;
- end.
-