Преглед изворни кода

implemented FileSetDate, improved FileCreate

git-svn-id: trunk@24869 -
Károly Balogh пре 12 година
родитељ
комит
cdf3f3266b
1 измењених фајлова са 67 додато и 13 уклоњено
  1. 67 13
      rtl/morphos/sysutils.pp

+ 67 - 13
rtl/morphos/sysutils.pp

@@ -25,6 +25,7 @@ interface
 { force ansistrings }
 { force ansistrings }
 {$H+}
 {$H+}
 
 
+{$DEFINE OS_FILESETDATEBYNAME}
 {$DEFINE HAS_SLEEP}
 {$DEFINE HAS_SLEEP}
 {$DEFINE HAS_OSERROR}
 {$DEFINE HAS_OSERROR}
 { Include platform independent interface part }
 { Include platform independent interface part }
@@ -75,7 +76,6 @@ begin
   dosLock:=Lock(buffer,accessmode);
   dosLock:=Lock(buffer,accessmode);
 end;
 end;
 
 
-
 function AmigaFileDateToDateTime(aDate: TDateStamp; out success: boolean): TDateTime;
 function AmigaFileDateToDateTime(aDate: TDateStamp; out success: boolean): TDateTime;
 var
 var
   tmpSecs: DWord;
   tmpSecs: DWord;
@@ -87,7 +87,7 @@ begin
     tmpSecs:=(ds_Days * (24 * 60 * 60)) + (ds_Minute * 60) + (ds_Tick div TICKS_PER_SECOND);
     tmpSecs:=(ds_Days * (24 * 60 * 60)) + (ds_Minute * 60) + (ds_Tick div TICKS_PER_SECOND);
 
 
   Amiga2Date(tmpSecs,@clockData);
   Amiga2Date(tmpSecs,@clockData);
-{$WARNING TODO: implement msec values, if possible}
+{$HINT TODO: implement msec values, if possible}
   with clockData do begin
   with clockData do begin
      success:=TryEncodeDate(year,month,mday,tmpDate) and
      success:=TryEncodeDate(year,month,mday,tmpDate) and
               TryEncodeTime(hour,min,sec,0,tmpTime);
               TryEncodeTime(hour,min,sec,0,tmpTime);
@@ -96,6 +96,26 @@ begin
   result:=ComposeDateTime(tmpDate,tmpTime);
   result:=ComposeDateTime(tmpDate,tmpTime);
 end;
 end;
 
 
+function DateTimeToAmigaDateStamp(dateTime: TDateTime): TDateStamp;
+var
+  tmpSecs: DWord;
+  clockData: TClockData;
+  tmpMSec: Word;
+begin
+{$HINT TODO: implement msec values, if possible}
+  with clockData do begin
+     DecodeDate(dateTime,year,month,mday);
+     DecodeTime(dateTime,hour,min,sec,tmpMSec);
+  end;
+
+  tmpSecs:=Date2Amiga(@clockData);
+
+  with result do begin
+     ds_Days:= tmpSecs div (24 * 60 * 60);
+     ds_Minute:= (tmpSecs div 60) mod ds_Days;
+     ds_Tick:= (((tmpSecs mod 60) mod ds_Minute) mod ds_Days) * TICKS_PER_SECOND;
+  end;
+end;
 
 
 
 
 {****************************************************************************
 {****************************************************************************
@@ -142,14 +162,40 @@ begin
   if validFile then
   if validFile then
     result:=DateTimeToFileDate(tmpDateTime)
     result:=DateTimeToFileDate(tmpDateTime)
   else
   else
-    result:=-1;   
+    result:=-1;
 end;
 end;
 
 
 
 
 function FileSetDate(Handle, Age: LongInt) : LongInt;
 function FileSetDate(Handle, Age: LongInt) : LongInt;
+var
+  tmpDateStamp: TDateStamp;
+  tmpName: array[0..255] of char;
 begin
 begin
-  // Impossible under unix from FileHandle !!
-  FileSetDate:=-1;
+  result:=0;
+  if (Handle <> 0) then begin
+    if (NameFromFH(Handle, @tmpName, 256) = dosTrue) then begin
+      tmpDateStamp:=DateTimeToAmigaDateStamp(FileDateToDateTime(Age));
+      if not SetFileDate(@tmpName,@tmpDateStamp) then begin
+        IoErr(); // dump the error code for now (TODO)
+        result:=-1;
+      end;
+    end;
+  end;
+end;
+
+
+function FileSetDate(const FileName: string; Age: LongInt) : LongInt;
+var
+  tmpDateStamp: TDateStamp;
+  tmpName: array[0..255] of char;
+begin
+  result:=0;
+  tmpName:=PathConv(FileName)+#0;
+  tmpDateStamp:=DateTimeToAmigaDateStamp(FileDateToDateTime(Age));
+  if not SetFileDate(@tmpName,@tmpDateStamp) then begin
+    IoErr(); // dump the error code for now (TODO)
+    result:=-1;
+  end;
 end;
 end;
 
 
 
 
@@ -158,16 +204,24 @@ var
   dosResult: LongInt;
   dosResult: LongInt;
   tmpStr   : array[0..255] of char;
   tmpStr   : array[0..255] of char;
 begin
 begin
- tmpStr:=PathConv(FileName)+#0;
- dosResult:=Open(@tmpStr,MODE_NEWFILE);
- if dosResult=0 then
-   dosResult:=-1
- else
-   AddToList(MOS_fileList,dosResult);
+  dosResult:=-1;
 
 
- FileCreate:=dosResult;
-end;
+  { Open file in MODDE_READWRITE, then truncate it by hand rather than
+    opening it in MODE_NEWFILE, because that returns an exclusive lock 
+    so some operations might fail with it (KB) }
+  tmpStr:=PathConv(FileName)+#0;
+  dosResult:=Open(@tmpStr,MODE_READWRITE);
+  if dosResult = 0 then exit;
+
+  if SetFileSize(dosResult, 0, OFFSET_BEGINNING) = 0 then 
+    AddToList(MOS_fileList,dosResult)
+  else begin
+    dosClose(dosResult);
+    dosResult:=-1;
+  end;
 
 
+  FileCreate:=dosResult;
+end;
 
 
 function FileCreate(const FileName: string; Rights: integer): LongInt;
 function FileCreate(const FileName: string; Rights: integer): LongInt;
 begin
 begin