author | Claus Gittinger <cg@exept.de> |
Sun, 03 May 2020 13:20:12 +0200 | |
changeset 25379 | 609d492f29a7 |
parent 25377 | b3ee83b86f9b |
permissions | -rw-r--r-- |
1 | 1 |
" |
5 | 2 |
COPYRIGHT (c) 1989 by Claus Gittinger |
7872 | 3 |
All Rights Reserved |
1 | 4 |
|
5 |
This software is furnished under a license and may be used |
|
6 |
only in accordance with the terms of that license and with the |
|
7 |
inclusion of the above copyright notice. This software may not |
|
8 |
be provided or otherwise made available to, or used by, any |
|
9 |
other person. No title to or ownership of the software is |
|
10 |
hereby transferred. |
|
11 |
" |
|
5439 | 12 |
"{ Package: 'stx:libbasic' }" |
13 |
||
17309 | 14 |
"{ NameSpace: Smalltalk }" |
15 |
||
1 | 16 |
ExternalStream subclass:#FileStream |
7872 | 17 |
instanceVariableNames:'pathName canPosition removeOnClose' |
18 |
classVariableNames:'UserInitiatedFileSaveQuerySignal' |
|
19 |
poolDictionaries:'' |
|
20 |
category:'Streams-External' |
|
1 | 21 |
! |
22 |
||
216 | 23 |
!FileStream primitiveDefinitions! |
1 | 24 |
%{ |
17078 | 25 |
#include "stxOSDefs.h" |
26 |
||
14632 | 27 |
#undef String |
28 |
#undef Character |
|
29 |
||
1 | 30 |
#include <stdio.h> |
437 | 31 |
#define _STDIO_H_INCLUDED_ |
32 |
||
10 | 33 |
#include <errno.h> |
437 | 34 |
#define _ERRNO_H_INCLUDED_ |
1 | 35 |
|
36 |
#ifdef transputer |
|
37 |
# include <iocntrl.h> |
|
38 |
# ifndef fileno |
|
39 |
/* kludge: inmos forgot fileno */ |
|
40 |
# define fileno(f) ((f)->__file) |
|
8627
5a86b95bebbb
gcc3.3.1 insn-scheduling bug workaround
Claus Gittinger <cg@exept.de>
parents:
8604
diff
changeset
|
41 |
# endif |
4910
33b0edbb1920
merged back FileStream_win32 into FileStream
Claus Gittinger <cg@exept.de>
parents:
4907
diff
changeset
|
42 |
|
33b0edbb1920
merged back FileStream_win32 into FileStream
Claus Gittinger <cg@exept.de>
parents:
4907
diff
changeset
|
43 |
#else /* not transputer */ |
33b0edbb1920
merged back FileStream_win32 into FileStream
Claus Gittinger <cg@exept.de>
parents:
4907
diff
changeset
|
44 |
|
1 | 45 |
# include <sys/types.h> |
46 |
# include <sys/stat.h> |
|
4910
33b0edbb1920
merged back FileStream_win32 into FileStream
Claus Gittinger <cg@exept.de>
parents:
4907
diff
changeset
|
47 |
|
33b0edbb1920
merged back FileStream_win32 into FileStream
Claus Gittinger <cg@exept.de>
parents:
4907
diff
changeset
|
48 |
#endif /* not transputer */ |
10 | 49 |
|
42 | 50 |
#ifdef hpux |
68 | 51 |
# define fileno(f) ((f->__fileH << 8) | (f->__fileL)) |
42 | 52 |
#endif |
53 |
||
2921 | 54 |
#ifdef __VMS__ |
55 |
# include <rms.h> |
|
56 |
#endif |
|
57 |
||
14635
aa0d74813208
Add __ before STX_String, STX_Character
Stefan Vogel <sv@exept.de>
parents:
14633
diff
changeset
|
58 |
#define String __STX_String |
aa0d74813208
Add __ before STX_String, STX_Character
Stefan Vogel <sv@exept.de>
parents:
14633
diff
changeset
|
59 |
#define Character __STX_Character |
14632 | 60 |
|
19860
324edacff5cc
unified cpu and os defines;
Claus Gittinger <cg@exept.de>
parents:
19638
diff
changeset
|
61 |
#ifdef __win32__ |
4910
33b0edbb1920
merged back FileStream_win32 into FileStream
Claus Gittinger <cg@exept.de>
parents:
4907
diff
changeset
|
62 |
# define NO_STDIO |
19860
324edacff5cc
unified cpu and os defines;
Claus Gittinger <cg@exept.de>
parents:
19638
diff
changeset
|
63 |
# if defined(__i386__) || defined(__x86__) |
4910
33b0edbb1920
merged back FileStream_win32 into FileStream
Claus Gittinger <cg@exept.de>
parents:
4907
diff
changeset
|
64 |
# define _X86_ |
33b0edbb1920
merged back FileStream_win32 into FileStream
Claus Gittinger <cg@exept.de>
parents:
4907
diff
changeset
|
65 |
# endif |
33b0edbb1920
merged back FileStream_win32 into FileStream
Claus Gittinger <cg@exept.de>
parents:
4907
diff
changeset
|
66 |
|
33b0edbb1920
merged back FileStream_win32 into FileStream
Claus Gittinger <cg@exept.de>
parents:
4907
diff
changeset
|
67 |
# undef INT |
10345 | 68 |
# undef UINT |
4910
33b0edbb1920
merged back FileStream_win32 into FileStream
Claus Gittinger <cg@exept.de>
parents:
4907
diff
changeset
|
69 |
# undef Array |
33b0edbb1920
merged back FileStream_win32 into FileStream
Claus Gittinger <cg@exept.de>
parents:
4907
diff
changeset
|
70 |
# undef Number |
33b0edbb1920
merged back FileStream_win32 into FileStream
Claus Gittinger <cg@exept.de>
parents:
4907
diff
changeset
|
71 |
# undef Method |
33b0edbb1920
merged back FileStream_win32 into FileStream
Claus Gittinger <cg@exept.de>
parents:
4907
diff
changeset
|
72 |
# undef Point |
33b0edbb1920
merged back FileStream_win32 into FileStream
Claus Gittinger <cg@exept.de>
parents:
4907
diff
changeset
|
73 |
# undef Rectangle |
33b0edbb1920
merged back FileStream_win32 into FileStream
Claus Gittinger <cg@exept.de>
parents:
4907
diff
changeset
|
74 |
# undef Block |
33b0edbb1920
merged back FileStream_win32 into FileStream
Claus Gittinger <cg@exept.de>
parents:
4907
diff
changeset
|
75 |
# undef Time |
33b0edbb1920
merged back FileStream_win32 into FileStream
Claus Gittinger <cg@exept.de>
parents:
4907
diff
changeset
|
76 |
# undef Date |
5120
13143fbcbdf8
introduced __threadErrno (for native threads)
Claus Gittinger <cg@exept.de>
parents:
4910
diff
changeset
|
77 |
# undef Set |
5910 | 78 |
# undef Signal |
5120
13143fbcbdf8
introduced __threadErrno (for native threads)
Claus Gittinger <cg@exept.de>
parents:
4910
diff
changeset
|
79 |
# undef Delay |
5910 | 80 |
# undef Context |
9025 | 81 |
# undef Message |
82 |
# undef Process |
|
10342 | 83 |
# undef Processor |
14632 | 84 |
# undef String |
85 |
# undef Character |
|
4910
33b0edbb1920
merged back FileStream_win32 into FileStream
Claus Gittinger <cg@exept.de>
parents:
4907
diff
changeset
|
86 |
|
33b0edbb1920
merged back FileStream_win32 into FileStream
Claus Gittinger <cg@exept.de>
parents:
4907
diff
changeset
|
87 |
# define NOATOM |
33b0edbb1920
merged back FileStream_win32 into FileStream
Claus Gittinger <cg@exept.de>
parents:
4907
diff
changeset
|
88 |
# define NOGDICAPMASKS |
33b0edbb1920
merged back FileStream_win32 into FileStream
Claus Gittinger <cg@exept.de>
parents:
4907
diff
changeset
|
89 |
# define NOMETAFILE |
33b0edbb1920
merged back FileStream_win32 into FileStream
Claus Gittinger <cg@exept.de>
parents:
4907
diff
changeset
|
90 |
# define NOMINMAX |
33b0edbb1920
merged back FileStream_win32 into FileStream
Claus Gittinger <cg@exept.de>
parents:
4907
diff
changeset
|
91 |
//# define NOOPENFILE |
33b0edbb1920
merged back FileStream_win32 into FileStream
Claus Gittinger <cg@exept.de>
parents:
4907
diff
changeset
|
92 |
# define NOSOUND |
33b0edbb1920
merged back FileStream_win32 into FileStream
Claus Gittinger <cg@exept.de>
parents:
4907
diff
changeset
|
93 |
# define NOWH |
33b0edbb1920
merged back FileStream_win32 into FileStream
Claus Gittinger <cg@exept.de>
parents:
4907
diff
changeset
|
94 |
//# define NOCOMM |
33b0edbb1920
merged back FileStream_win32 into FileStream
Claus Gittinger <cg@exept.de>
parents:
4907
diff
changeset
|
95 |
# define NOKANJI |
33b0edbb1920
merged back FileStream_win32 into FileStream
Claus Gittinger <cg@exept.de>
parents:
4907
diff
changeset
|
96 |
# define NOCRYPT |
33b0edbb1920
merged back FileStream_win32 into FileStream
Claus Gittinger <cg@exept.de>
parents:
4907
diff
changeset
|
97 |
//# define NOMCX |
33b0edbb1920
merged back FileStream_win32 into FileStream
Claus Gittinger <cg@exept.de>
parents:
4907
diff
changeset
|
98 |
# define WIN32_LEAN_AND_MEAN |
33b0edbb1920
merged back FileStream_win32 into FileStream
Claus Gittinger <cg@exept.de>
parents:
4907
diff
changeset
|
99 |
|
33b0edbb1920
merged back FileStream_win32 into FileStream
Claus Gittinger <cg@exept.de>
parents:
4907
diff
changeset
|
100 |
# include <windows.h> |
33b0edbb1920
merged back FileStream_win32 into FileStream
Claus Gittinger <cg@exept.de>
parents:
4907
diff
changeset
|
101 |
|
14633 | 102 |
# if 1 // defined(__MINGW64__) |
14632 | 103 |
# include <fcntl.h> |
104 |
# endif |
|
105 |
||
10450
bb40b56ed6b6
Remove unused stat() redefinition
Stefan Vogel <sv@exept.de>
parents:
10407
diff
changeset
|
106 |
# if defined(__BORLANDC__) |
bb40b56ed6b6
Remove unused stat() redefinition
Stefan Vogel <sv@exept.de>
parents:
10407
diff
changeset
|
107 |
# include <io.h> |
bb40b56ed6b6
Remove unused stat() redefinition
Stefan Vogel <sv@exept.de>
parents:
10407
diff
changeset
|
108 |
# else |
10342 | 109 |
# define lseek _lseek |
4910
33b0edbb1920
merged back FileStream_win32 into FileStream
Claus Gittinger <cg@exept.de>
parents:
4907
diff
changeset
|
110 |
# endif |
33b0edbb1920
merged back FileStream_win32 into FileStream
Claus Gittinger <cg@exept.de>
parents:
4907
diff
changeset
|
111 |
|
33b0edbb1920
merged back FileStream_win32 into FileStream
Claus Gittinger <cg@exept.de>
parents:
4907
diff
changeset
|
112 |
# ifdef __DEF_Array |
33b0edbb1920
merged back FileStream_win32 into FileStream
Claus Gittinger <cg@exept.de>
parents:
4907
diff
changeset
|
113 |
# define Array __DEF_Array |
33b0edbb1920
merged back FileStream_win32 into FileStream
Claus Gittinger <cg@exept.de>
parents:
4907
diff
changeset
|
114 |
# endif |
33b0edbb1920
merged back FileStream_win32 into FileStream
Claus Gittinger <cg@exept.de>
parents:
4907
diff
changeset
|
115 |
# ifdef __DEF_Number |
33b0edbb1920
merged back FileStream_win32 into FileStream
Claus Gittinger <cg@exept.de>
parents:
4907
diff
changeset
|
116 |
# define Number __DEF_Number |
33b0edbb1920
merged back FileStream_win32 into FileStream
Claus Gittinger <cg@exept.de>
parents:
4907
diff
changeset
|
117 |
# endif |
33b0edbb1920
merged back FileStream_win32 into FileStream
Claus Gittinger <cg@exept.de>
parents:
4907
diff
changeset
|
118 |
# ifdef __DEF_Method |
33b0edbb1920
merged back FileStream_win32 into FileStream
Claus Gittinger <cg@exept.de>
parents:
4907
diff
changeset
|
119 |
# define Method __DEF_Method |
33b0edbb1920
merged back FileStream_win32 into FileStream
Claus Gittinger <cg@exept.de>
parents:
4907
diff
changeset
|
120 |
# endif |
33b0edbb1920
merged back FileStream_win32 into FileStream
Claus Gittinger <cg@exept.de>
parents:
4907
diff
changeset
|
121 |
# ifdef __DEF_Point |
33b0edbb1920
merged back FileStream_win32 into FileStream
Claus Gittinger <cg@exept.de>
parents:
4907
diff
changeset
|
122 |
# define Point __DEF_Point |
33b0edbb1920
merged back FileStream_win32 into FileStream
Claus Gittinger <cg@exept.de>
parents:
4907
diff
changeset
|
123 |
# endif |
33b0edbb1920
merged back FileStream_win32 into FileStream
Claus Gittinger <cg@exept.de>
parents:
4907
diff
changeset
|
124 |
# ifdef __DEF_Block |
33b0edbb1920
merged back FileStream_win32 into FileStream
Claus Gittinger <cg@exept.de>
parents:
4907
diff
changeset
|
125 |
# define Block __DEF_Block |
33b0edbb1920
merged back FileStream_win32 into FileStream
Claus Gittinger <cg@exept.de>
parents:
4907
diff
changeset
|
126 |
# endif |
33b0edbb1920
merged back FileStream_win32 into FileStream
Claus Gittinger <cg@exept.de>
parents:
4907
diff
changeset
|
127 |
# ifdef __DEF_Time |
33b0edbb1920
merged back FileStream_win32 into FileStream
Claus Gittinger <cg@exept.de>
parents:
4907
diff
changeset
|
128 |
# define Time __DEF_Time |
33b0edbb1920
merged back FileStream_win32 into FileStream
Claus Gittinger <cg@exept.de>
parents:
4907
diff
changeset
|
129 |
# endif |
33b0edbb1920
merged back FileStream_win32 into FileStream
Claus Gittinger <cg@exept.de>
parents:
4907
diff
changeset
|
130 |
# ifdef __DEF_Date |
33b0edbb1920
merged back FileStream_win32 into FileStream
Claus Gittinger <cg@exept.de>
parents:
4907
diff
changeset
|
131 |
# define Date __DEF_Date |
33b0edbb1920
merged back FileStream_win32 into FileStream
Claus Gittinger <cg@exept.de>
parents:
4907
diff
changeset
|
132 |
# endif |
5120
13143fbcbdf8
introduced __threadErrno (for native threads)
Claus Gittinger <cg@exept.de>
parents:
4910
diff
changeset
|
133 |
# ifdef __DEF_Set |
13143fbcbdf8
introduced __threadErrno (for native threads)
Claus Gittinger <cg@exept.de>
parents:
4910
diff
changeset
|
134 |
# define Set __DEF_Set |
13143fbcbdf8
introduced __threadErrno (for native threads)
Claus Gittinger <cg@exept.de>
parents:
4910
diff
changeset
|
135 |
# endif |
13143fbcbdf8
introduced __threadErrno (for native threads)
Claus Gittinger <cg@exept.de>
parents:
4910
diff
changeset
|
136 |
# ifdef __DEF_Signal |
13143fbcbdf8
introduced __threadErrno (for native threads)
Claus Gittinger <cg@exept.de>
parents:
4910
diff
changeset
|
137 |
# define Signal __DEF_Signal |
13143fbcbdf8
introduced __threadErrno (for native threads)
Claus Gittinger <cg@exept.de>
parents:
4910
diff
changeset
|
138 |
# endif |
13143fbcbdf8
introduced __threadErrno (for native threads)
Claus Gittinger <cg@exept.de>
parents:
4910
diff
changeset
|
139 |
# ifdef __DEF_Delay |
13143fbcbdf8
introduced __threadErrno (for native threads)
Claus Gittinger <cg@exept.de>
parents:
4910
diff
changeset
|
140 |
# define Delay __DEF_Delay |
13143fbcbdf8
introduced __threadErrno (for native threads)
Claus Gittinger <cg@exept.de>
parents:
4910
diff
changeset
|
141 |
# endif |
5910 | 142 |
# ifdef __DEF_Context |
143 |
# define Context __DEF_Context |
|
144 |
# endif |
|
9025 | 145 |
# ifdef __DEF_Message |
146 |
# define Message __DEF_Message |
|
147 |
# endif |
|
148 |
# ifdef __DEF_Process |
|
149 |
# define Process __DEF_Process |
|
150 |
# endif |
|
10342 | 151 |
# ifdef __DEF_Processor |
152 |
# define Processor __DEF_Processor |
|
153 |
# endif |
|
14632 | 154 |
# ifdef __DEF_String |
155 |
# define String __DEF_String |
|
156 |
# endif |
|
157 |
# ifdef __DEF_Character |
|
158 |
# define Character __DEF_Character |
|
159 |
# endif |
|
4910
33b0edbb1920
merged back FileStream_win32 into FileStream
Claus Gittinger <cg@exept.de>
parents:
4907
diff
changeset
|
160 |
|
14644 | 161 |
# define INT STX_INT |
162 |
# define UINT STX_UINT |
|
4910
33b0edbb1920
merged back FileStream_win32 into FileStream
Claus Gittinger <cg@exept.de>
parents:
4907
diff
changeset
|
163 |
|
14632 | 164 |
# if 0 |
6259
164d7efc1eb2
api call stuff (more calls wrapped; care for EINTR)
Claus Gittinger <cg@exept.de>
parents:
6244
diff
changeset
|
165 |
# ifndef DO_WRAP_CALLS |
6267
10016cc16511
pass name of API/C wrap function
Claus Gittinger <cg@exept.de>
parents:
6259
diff
changeset
|
166 |
# define STX_C_CALL0(__nm__, __f__) __f__((__a1__)) |
10016cc16511
pass name of API/C wrap function
Claus Gittinger <cg@exept.de>
parents:
6259
diff
changeset
|
167 |
# define STX_C_CALL1(__nm__, __f__, __a1__) __f__((__a1__)) |
10016cc16511
pass name of API/C wrap function
Claus Gittinger <cg@exept.de>
parents:
6259
diff
changeset
|
168 |
# define STX_C_CALL2(__nm__, __f__, __a1__, __a2__) __f__((__a1__), (__a2__)) |
10016cc16511
pass name of API/C wrap function
Claus Gittinger <cg@exept.de>
parents:
6259
diff
changeset
|
169 |
# define STX_C_CALL3(__nm__, __f__, __a1__, __a2__, __a3__) __f__((__a1__), (__a2__), (__a3__)) |
6259
164d7efc1eb2
api call stuff (more calls wrapped; care for EINTR)
Claus Gittinger <cg@exept.de>
parents:
6244
diff
changeset
|
170 |
# else |
6267
10016cc16511
pass name of API/C wrap function
Claus Gittinger <cg@exept.de>
parents:
6259
diff
changeset
|
171 |
# define STX_C_CALL0(__nm__, __f__) __STX_C_CALL0(__nm__, (void*)__f__) |
10016cc16511
pass name of API/C wrap function
Claus Gittinger <cg@exept.de>
parents:
6259
diff
changeset
|
172 |
# define STX_C_CALL1(__nm__, __f__, __a1__) __STX_C_CALL1(__nm__, (void*)__f__, (void*)(__a1__)) |
10016cc16511
pass name of API/C wrap function
Claus Gittinger <cg@exept.de>
parents:
6259
diff
changeset
|
173 |
# define STX_C_CALL2(__nm__, __f__, __a1__, __a2__) __STX_C_CALL2(__nm__, (void*)__f__, (void*)(__a1__), (void*)(__a2__)) |
10016cc16511
pass name of API/C wrap function
Claus Gittinger <cg@exept.de>
parents:
6259
diff
changeset
|
174 |
# define STX_C_CALL3(__nm__, __f__, __a1__, __a2__, __a3__) __STX_C_CALL3(__nm__, (void*)__f__, (void*)(__a1__), (void*)(__a2__), (void*)(__a3__)) |
6259
164d7efc1eb2
api call stuff (more calls wrapped; care for EINTR)
Claus Gittinger <cg@exept.de>
parents:
6244
diff
changeset
|
175 |
# endif |
14632 | 176 |
# endif |
4910
33b0edbb1920
merged back FileStream_win32 into FileStream
Claus Gittinger <cg@exept.de>
parents:
4907
diff
changeset
|
177 |
|
6259
164d7efc1eb2
api call stuff (more calls wrapped; care for EINTR)
Claus Gittinger <cg@exept.de>
parents:
6244
diff
changeset
|
178 |
/* # define HFILE HANDLE */ |
4910
33b0edbb1920
merged back FileStream_win32 into FileStream
Claus Gittinger <cg@exept.de>
parents:
4907
diff
changeset
|
179 |
# define HFILE FILE * |
6259
164d7efc1eb2
api call stuff (more calls wrapped; care for EINTR)
Claus Gittinger <cg@exept.de>
parents:
6244
diff
changeset
|
180 |
/* # define fileno(f) f */ |
4910
33b0edbb1920
merged back FileStream_win32 into FileStream
Claus Gittinger <cg@exept.de>
parents:
4907
diff
changeset
|
181 |
|
19860
324edacff5cc
unified cpu and os defines;
Claus Gittinger <cg@exept.de>
parents:
19638
diff
changeset
|
182 |
#else /* not __win32__ */ |
7970 | 183 |
# define HFILE FILE * |
19860
324edacff5cc
unified cpu and os defines;
Claus Gittinger <cg@exept.de>
parents:
19638
diff
changeset
|
184 |
#endif /* ! __win32__ */ |
4910
33b0edbb1920
merged back FileStream_win32 into FileStream
Claus Gittinger <cg@exept.de>
parents:
4907
diff
changeset
|
185 |
|
16298 | 186 |
//#define DO_WRAP_CALL_FSEEK |
14853
63ffca691137
fseek does not need a WRAP_CALL (I hope)
Claus Gittinger <cg@exept.de>
parents:
14848
diff
changeset
|
187 |
|
7570 | 188 |
#include "stxOSDefs.h" |
189 |
||
10342 | 190 |
#ifndef SEEK_SET |
191 |
# define SEEK_SET 0 |
|
192 |
#endif |
|
193 |
#ifndef SEEK_CUR |
|
194 |
# define SEEK_CUR 1 |
|
195 |
#endif |
|
196 |
#ifndef SEEK_END |
|
197 |
# define SEEK_END 2 |
|
198 |
#endif |
|
1 | 199 |
%} |
173 | 200 |
! ! |
1 | 201 |
|
613 | 202 |
!FileStream class methodsFor:'documentation'! |
203 |
||
204 |
copyright |
|
205 |
" |
|
206 |
COPYRIGHT (c) 1989 by Claus Gittinger |
|
7872 | 207 |
All Rights Reserved |
613 | 208 |
|
209 |
This software is furnished under a license and may be used |
|
210 |
only in accordance with the terms of that license and with the |
|
211 |
inclusion of the above copyright notice. This software may not |
|
212 |
be provided or otherwise made available to, or used by, any |
|
213 |
other person. No title to or ownership of the software is |
|
214 |
hereby transferred. |
|
215 |
" |
|
216 |
! |
|
217 |
||
218 |
documentation |
|
219 |
" |
|
220 |
This class provides access to the operating systems underlying file |
|
221 |
system (i.e. its an interface to the stdio library). |
|
222 |
||
223 |
Notice, that on some systems, the standard I/O library has performance |
|
8627
5a86b95bebbb
gcc3.3.1 insn-scheduling bug workaround
Claus Gittinger <cg@exept.de>
parents:
8604
diff
changeset
|
224 |
problems when a file is opened for readwrite. |
613 | 225 |
For best results, open files either readonly or writeonly. |
1295 | 226 |
|
2921 | 227 |
Also notice, that some OperatingSystems do not fully support |
228 |
positioning a file stream. |
|
229 |
For example, poor VMS does not allow positioning onto arbitrary |
|
230 |
byte boundaries if the file is a variable-record-RMS file. |
|
15480 | 231 |
(stupid enough, this is the default for textfiles as created by some tools ...) |
2921 | 232 |
Therefore, the instance variable canPosition is set according to |
233 |
this and an error is raised, if a position: is attemted. |
|
234 |
I know, this is ugly, but what else could we do ? |
|
15542 | 235 |
Late note: who cares for VMS these days? |
18651 | 236 |
(and how much useless effort has been put in the past, |
237 |
to support lousy operating systems?) |
|
2921 | 238 |
|
239 |
[instance variables:] |
|
18651 | 240 |
pathName <String> the file's path (if known) |
241 |
canPosition <Boolean> positionable - read above comment |
|
2921 | 242 |
|
1295 | 243 |
[author:] |
18651 | 244 |
Claus Gittinger |
1295 | 245 |
|
246 |
[see also:] |
|
18651 | 247 |
Filename DirectoryStream PipeStream Socket |
613 | 248 |
" |
2921 | 249 |
! |
250 |
||
251 |
examples |
|
252 |
" |
|
253 |
for VMS users only: |
|
254 |
||
255 |
The #openWithMode:attributes: entry allows additional RMS attributes |
|
256 |
to be passed in the second argument, which must be an array of strings |
|
257 |
as described in the 'creat' RTL Library documentation. |
|
258 |
||
259 |
For example, to create a file with fixed records and recordLength of 100, |
|
260 |
use: |
|
261 |
||
7872 | 262 |
|newFile| |
2921 | 263 |
|
7872 | 264 |
newFile := FileStream new pathName:'<nameOfFile>'. |
265 |
newFile setMode:#writeonly. |
|
266 |
newFile openWithMode:'w' attributes:#('rfm=fix' 'fsz=100'). |
|
2921 | 267 |
|
268 |
since all of the above is private protocol, and it is considered bad style to |
|
269 |
access these from user programs, we recommend subclassing FileStream as |
|
270 |
something like VMSFixedRecordFileStream, and redefine the instance creation |
|
8627
5a86b95bebbb
gcc3.3.1 insn-scheduling bug workaround
Claus Gittinger <cg@exept.de>
parents:
8604
diff
changeset
|
271 |
method(s) there as appropriate. |
2921 | 272 |
This will retain VMS specifics in one place and enhance maintanability. |
273 |
" |
|
274 |
! ! |
|
275 |
||
276 |
!FileStream class methodsFor:'initialization'! |
|
277 |
||
278 |
initialize |
|
7379 | 279 |
UserInitiatedFileSaveQuerySignal isNil ifTrue:[ |
7965 | 280 |
"/ |
281 |
"/ this is temporary - for now allow an openError to |
|
282 |
"/ be unhandled and proceed by returning a nil from the |
|
283 |
"/ stream creation method. |
|
284 |
"/ In the future, this will be a hard signal. |
|
285 |
"/ THE FUTURE IS NOW!!!! |
|
286 |
"/ |
|
7379 | 287 |
"/ OpenErrorSignal := QuerySignal new. |
288 |
"/ OpenErrorSignal parent:(super openErrorSignal) mayProceed:true. |
|
289 |
"/ OpenErrorSignal nameClass:self message:#openErrorSignal. |
|
290 |
"/ OpenErrorSignal notifierString:'open error'. |
|
4907 | 291 |
|
7965 | 292 |
UserInitiatedFileSaveQuerySignal := QuerySignal new defaultAnswer:true. |
293 |
UserInitiatedFileSaveQuerySignal nameClass:self message:#userInitiatedFileSaveQuerySignal. |
|
2921 | 294 |
] |
3006
b3530950854a
use new initialization message of signal.
Claus Gittinger <cg@exept.de>
parents:
2998
diff
changeset
|
295 |
|
b3530950854a
use new initialization message of signal.
Claus Gittinger <cg@exept.de>
parents:
2998
diff
changeset
|
296 |
"Modified: 8.10.1997 / 11:56:39 / cg" |
2921 | 297 |
! ! |
298 |
||
1 | 299 |
!FileStream class methodsFor:'instance creation'! |
300 |
||
613 | 301 |
appendingOldFileNamed:filename |
302 |
"return a FileStream for existing file named filename, aString. |
|
303 |
The file is opened for writeonly access." |
|
1 | 304 |
|
7827 | 305 |
|
1 | 306 |
|newStream| |
7827 | 307 |
|
2 | 308 |
newStream := self new pathName:filename. |
8508
68cd95545196
Allow to proceed with any stream from an #openError handler
Stefan Vogel <sv@exept.de>
parents:
8472
diff
changeset
|
309 |
^ newStream openForAppending. |
7827 | 310 |
|
311 |
" |
|
312 |
FileStream appendingOldFileNamed:'adasdasasd' |
|
313 |
" |
|
613 | 314 |
! |
315 |
||
316 |
appendingOldFileNamed:filename in:aDirectory |
|
317 |
"return a FileStream for existing file named filename, aString |
|
318 |
in aDirectory, a FileDirectory. |
|
319 |
The file is opened for writeonly access." |
|
320 |
||
321 |
|newStream| |
|
322 |
newStream := self new pathName:filename in:aDirectory. |
|
8508
68cd95545196
Allow to proceed with any stream from an #openError handler
Stefan Vogel <sv@exept.de>
parents:
8472
diff
changeset
|
323 |
^ newStream openForAppending. |
1 | 324 |
! |
325 |
||
613 | 326 |
fileNamed:filename |
327 |
"return a stream on file filename - if the file does not |
|
328 |
already exist, create it. |
|
92 | 329 |
The file is opened for read/write access." |
1 | 330 |
|
7091
b399f3d19084
Handle openErrorSignal in preparition for change openErrorSignal
Stefan Vogel <sv@exept.de>
parents:
7053
diff
changeset
|
331 |
[ |
25179 | 332 |
^ self oldFileNamed:filename. |
333 |
] on:OpenError do:[:ex| ]. |
|
613 | 334 |
|
7091
b399f3d19084
Handle openErrorSignal in preparition for change openErrorSignal
Stefan Vogel <sv@exept.de>
parents:
7053
diff
changeset
|
335 |
^ self newFileNamed:filename |
613 | 336 |
! |
337 |
||
338 |
fileNamed:filename in:aDirectory |
|
339 |
"return a stream on file filename - if the file does not |
|
340 |
already exist, create it. |
|
341 |
The file is opened for read/write access." |
|
342 |
||
7091
b399f3d19084
Handle openErrorSignal in preparition for change openErrorSignal
Stefan Vogel <sv@exept.de>
parents:
7053
diff
changeset
|
343 |
[ |
25179 | 344 |
^ self oldFileNamed:filename in:aDirectory. |
345 |
] on:OpenError do:[:ex| ]. |
|
613 | 346 |
|
7091
b399f3d19084
Handle openErrorSignal in preparition for change openErrorSignal
Stefan Vogel <sv@exept.de>
parents:
7053
diff
changeset
|
347 |
^ self newFileNamed:filename in:aDirectory |
1 | 348 |
! |
349 |
||
92 | 350 |
newFileForWritingNamed:filename |
351 |
"return a FileStream for new file named filename, aString. |
|
352 |
If the file exists, it is truncated, otherwise created. |
|
353 |
The file is opened for writeonly access." |
|
354 |
||
355 |
|newStream| |
|
356 |
newStream := self new pathName:filename. |
|
8508
68cd95545196
Allow to proceed with any stream from an #openError handler
Stefan Vogel <sv@exept.de>
parents:
8472
diff
changeset
|
357 |
^ newStream createForWriting. |
92 | 358 |
! |
359 |
||
360 |
newFileForWritingNamed:filename in:aDirectory |
|
361 |
"return a FileStream for new file named filename, aString |
|
362 |
in aDirectory, a FileDirectory. |
|
363 |
If the file exists, it is truncated, otherwise created. |
|
364 |
The file is opened for writeonly access." |
|
365 |
||
366 |
|newStream| |
|
367 |
newStream := self new pathName:filename in:aDirectory. |
|
8508
68cd95545196
Allow to proceed with any stream from an #openError handler
Stefan Vogel <sv@exept.de>
parents:
8472
diff
changeset
|
368 |
^ newStream createForWriting. |
92 | 369 |
! |
370 |
||
613 | 371 |
newFileNamed:filename |
372 |
"return a FileStream for new file named filename, aString. |
|
373 |
If the file exists, it is truncated, otherwise created. |
|
374 |
The file is opened for read/write access." |
|
375 |
||
376 |
|newStream| |
|
377 |
newStream := self new pathName:filename. |
|
8508
68cd95545196
Allow to proceed with any stream from an #openError handler
Stefan Vogel <sv@exept.de>
parents:
8472
diff
changeset
|
378 |
^ newStream createForReadWrite. |
3051
29d10c26d6a3
allow for fileNames to be passed to #pathName:
Claus Gittinger <cg@exept.de>
parents:
3044
diff
changeset
|
379 |
|
29d10c26d6a3
allow for fileNames to be passed to #pathName:
Claus Gittinger <cg@exept.de>
parents:
3044
diff
changeset
|
380 |
"Modified: / 28.10.1997 / 14:28:08 / cg" |
613 | 381 |
! |
382 |
||
383 |
newFileNamed:filename in:aDirectory |
|
384 |
"return a FileStream for new file named filename, aString |
|
385 |
in aDirectory, a FileDirectory. |
|
386 |
If the file exists, it is truncated, otherwise created. |
|
387 |
The file is opened for read/write access." |
|
388 |
||
389 |
|newStream| |
|
390 |
newStream := self new pathName:filename in:aDirectory. |
|
8508
68cd95545196
Allow to proceed with any stream from an #openError handler
Stefan Vogel <sv@exept.de>
parents:
8472
diff
changeset
|
391 |
^ newStream createForReadWrite. |
613 | 392 |
! |
393 |
||
15462 | 394 |
newTemporary |
395 |
"create atomically a new file and return the file stream - use this for temporary files. |
|
396 |
The created file has the name '/tmp/stxtmp_xx_nn' where xx is our |
|
397 |
unix process id, and nn is a unique number, incremented with every call to this method. |
|
23045 | 398 |
If any of the environment variables STX_TMPDIR, ST_TMPDIR, TMPDIR is set, |
15462 | 399 |
its value defines the temp directory." |
400 |
||
401 |
^ self newTemporaryIn:Filename tempDirectory |
|
402 |
||
403 |
" |
|
15542 | 404 |
FileStream newTemporary |
405 |
FileStream newTemporary |
|
15462 | 406 |
" |
23045 | 407 |
|
408 |
"Modified (comment): / 31-05-2018 / 09:58:04 / Claus Gittinger" |
|
15462 | 409 |
! |
410 |
||
411 |
newTemporaryIn:aDirectoryOrNil |
|
412 |
"create atomically a new file and return the file stream - use this for temporary files. |
|
413 |
The created file is in aDirectoryPrefix and named 'stxtmp_xx_nn', |
|
15542 | 414 |
where xx is our unix process id, and nn is a unique number, incremented |
15462 | 415 |
with every call to this method." |
416 |
||
22944 | 417 |
^ self newTemporaryIn:aDirectoryOrNil nameTemplate:(Filename tempFileNameTemplate) |
15462 | 418 |
|
419 |
"temp files in '/tmp': |
|
420 |
||
15542 | 421 |
FileStream newTemporary |
15462 | 422 |
" |
423 |
||
15542 | 424 |
"temp files somewhere |
15462 | 425 |
(not recommended - use above since it can be controlled via shell variables): |
426 |
||
15542 | 427 |
FileStream newTemporaryIn:'/tmp' |
428 |
FileStream newTemporaryIn:'/tmp' |
|
429 |
FileStream newTemporaryIn:'/usr/tmp' |
|
430 |
FileStream newTemporaryIn:'/' |
|
15462 | 431 |
" |
432 |
||
433 |
"a local temp file: |
|
434 |
||
15542 | 435 |
FileStream newTemporaryIn:'' |
436 |
FileStream newTemporaryIn:nil |
|
437 |
FileStream newTemporaryIn:'.' |
|
438 |
FileStream newTemporaryIn:('source' asFilename) |
|
15462 | 439 |
" |
440 |
! |
|
441 |
||
442 |
newTemporaryIn:aDirectoryOrNil nameTemplate:template |
|
443 |
"create atomically a new file and return the file stream - use this for temporary files. |
|
444 |
The created file is in aDirectoryOrNil and named after the given template, |
|
445 |
in which %1 and %2 are expanded to the unix process id, and a unique number, incremented |
|
446 |
with every call to this method respectively. |
|
447 |
See also: #newTemporary which looks for a good temp directory." |
|
448 |
||
25377 | 449 |
|nameString random prevRandom prevNameString newTempFilename stream nextSeqNr fn| |
15462 | 450 |
|
451 |
[ |
|
25377 | 452 |
prevRandom := random. |
453 |
prevNameString := nameString. |
|
454 |
||
455 |
"Use random numbers in order to improve the security |
|
456 |
by making the generated names less predictable" |
|
457 |
[ |
|
458 |
random := RandomGenerator nextLettersOrDigits:4. |
|
459 |
] doWhile:[random = prevRandom]. |
|
15462 | 460 |
|
25377 | 461 |
nameString := template bindWith:(OperatingSystem getProcessId) with:random. |
462 |
(prevNameString = nameString) ifTrue:[ |
|
463 |
"/ ouch - the given template seems to not generate unique file names. |
|
464 |
"/ append a sequence number |
|
465 |
nextSeqNr := (nextSeqNr ? 0) + 1. |
|
466 |
fn := nameString asFilename. |
|
467 |
nameString := (fn withoutSuffix name, '_', nextSeqNr asString) asFilename withSuffix:fn suffix. |
|
468 |
]. |
|
15462 | 469 |
|
25377 | 470 |
aDirectoryOrNil isNil ifTrue:[ |
471 |
newTempFilename := nameString. |
|
472 |
] ifFalse:[ |
|
473 |
newTempFilename := aDirectoryOrNil asFilename constructString:nameString. |
|
474 |
]. |
|
15462 | 475 |
|
25377 | 476 |
[ |
477 |
stream := self open:newTempFilename withMode:#(CREATE_NEW GENERIC_READ_WRITE FILE_SHARE_WRITE FILE_SHARE_READ). |
|
478 |
] on:OpenError do:[:ex| |
|
479 |
ex errorCategory ~~ #existingReferentSignal ifTrue:[ |
|
480 |
"some fundamental error, raise exception" |
|
481 |
ex reject. |
|
482 |
]. |
|
483 |
prevNameString = nameString ifTrue:[ |
|
484 |
"no more names - probably a bad template" |
|
485 |
ex reject. |
|
486 |
]. |
|
487 |
"file exists, retry another one" |
|
488 |
]. |
|
15462 | 489 |
] doWhile:[ |
25377 | 490 |
stream isNil and:[prevNameString ~= nameString] "/ if namestring didn't change, the template is bad |
15462 | 491 |
]. |
492 |
^ stream |
|
493 |
||
494 |
"temp files in '/tmp': |
|
495 |
||
25377 | 496 |
FileStream newTemporaryIn:'/tmp' asFilename nameTemplate:'foo%1_%2' |
15462 | 497 |
|
498 |
This must fail on the second try: |
|
25377 | 499 |
FileStream newTemporaryIn:'/tmp' asFilename nameTemplate:'foo' |
500 |
FileStream newTemporaryIn:'c:\temp' asFilename nameTemplate:'foo' |
|
15462 | 501 |
" |
502 |
||
503 |
"temp files somewhere |
|
504 |
(not recommended - use above since it can be controlled via shell variables): |
|
505 |
||
25377 | 506 |
FileStream newTemporaryIn:'/tmp' nameTemplate:'foo' |
15462 | 507 |
FileStream newTemporaryIn:'/tmp' nameTemplate:'foo%1_%2' |
508 |
FileStream newTemporaryIn:'/tmp' nameTemplate:'foo%1_%2' |
|
509 |
FileStream newTemporaryIn:'/usr/tmp' nameTemplate:'foo%1_%2' |
|
510 |
FileStream newTemporaryIn:'/' nameTemplate:'foo%1_%2' |
|
511 |
" |
|
512 |
||
513 |
"a local temp file: |
|
514 |
||
515 |
FileStream newTemporaryIn:'' nameTemplate:'foo%1_%2' |
|
516 |
FileStream newTemporaryIn:nil nameTemplate:'foo%1_%2' |
|
517 |
FileStream newTemporaryIn:'.' nameTemplate:'foo%1_%2' |
|
518 |
FileStream newTemporaryIn:('source' asFilename) nameTemplate:'foo%1_%2' |
|
519 |
" |
|
23429 | 520 |
|
25377 | 521 |
"Modified: / 29-04-2020 / 09:38:15 / Stefan Vogel" |
15462 | 522 |
! |
523 |
||
15542 | 524 |
newTemporaryIn:aDirectoryOrNil withSuffix:aSuffixString |
15469 | 525 |
"create atomically a new file and return the file stream - use this for temporary files. |
526 |
The created file is in aDirectoryPrefix and named 'stxtmp_xx_nn', |
|
527 |
where xx is our unix process id, and nn is a unique number, incremented |
|
528 |
with every call to this method." |
|
15542 | 529 |
|
530 |
^ self |
|
531 |
newTemporaryIn:aDirectoryOrNil |
|
532 |
nameTemplate:(Filename tempFileNameTemplate asFilename |
|
533 |
withSuffix:aSuffixString) asString |
|
15469 | 534 |
|
535 |
" |
|
536 |
FileStream newTemporaryWithSuffix:'txt' |
|
537 |
FileStream newTemporaryIn:'/tmp' withSuffix:'txt' |
|
538 |
" |
|
539 |
! |
|
540 |
||
541 |
newTemporaryWithSuffix:aString |
|
542 |
"create atomically a new file and return the file stream - use this for temporary files. |
|
543 |
The created file has the name '/tmp/stxtmp_xx_nn' where xx is our |
|
544 |
unix process id, and nn is a unique number, incremented with every call to this method. |
|
23045 | 545 |
If any of the environment variables STX_TMPDIR, ST_TMPDIR, TMPDIR is set, |
15469 | 546 |
its value defines the temp directory." |
547 |
||
548 |
^ self newTemporaryIn:Filename tempDirectory withSuffix:aString |
|
549 |
||
550 |
" |
|
551 |
FileStream newTemporaryWithSuffix:'txt' |
|
552 |
" |
|
23045 | 553 |
|
554 |
"Modified (comment): / 31-05-2018 / 09:58:01 / Claus Gittinger" |
|
15469 | 555 |
! |
556 |
||
1 | 557 |
oldFileNamed:filename |
558 |
"return a FileStream for existing file named filename, aString. |
|
7827 | 559 |
The file is opened for read/write access. |
560 |
Raises an error if the file does not exist." |
|
1 | 561 |
|
562 |
|newStream| |
|
563 |
||
7091
b399f3d19084
Handle openErrorSignal in preparition for change openErrorSignal
Stefan Vogel <sv@exept.de>
parents:
7053
diff
changeset
|
564 |
"/ We can do the following, but is is an extra OS-systemcall: |
b399f3d19084
Handle openErrorSignal in preparition for change openErrorSignal
Stefan Vogel <sv@exept.de>
parents:
7053
diff
changeset
|
565 |
"/ (OperatingSystem isReadable:filename) ifFalse:[^ self new openError]. |
b399f3d19084
Handle openErrorSignal in preparition for change openErrorSignal
Stefan Vogel <sv@exept.de>
parents:
7053
diff
changeset
|
566 |
|
2 | 567 |
newStream := self new pathName:filename. |
1 | 568 |
newStream readwrite. |
8508
68cd95545196
Allow to proceed with any stream from an #openError handler
Stefan Vogel <sv@exept.de>
parents:
8472
diff
changeset
|
569 |
^ newStream openForReadWrite. |
7827 | 570 |
|
571 |
" |
|
15489 | 572 |
'/tmp/dAsGiBtEsNiChT' asFilename remove. |
573 |
FileStream oldFileNamed:'/tmp/dAsGiBtEsNiChT' |
|
7827 | 574 |
" |
1 | 575 |
! |
576 |
||
577 |
oldFileNamed:filename in:aDirectory |
|
578 |
"return a FileStream for existing file named filename, aString |
|
579 |
in aDirectory, a FileDirectory. |
|
7827 | 580 |
The file is opened for read/write access. |
581 |
Raises an error if the file does not exist." |
|
1 | 582 |
|
583 |
|newStream| |
|
2 | 584 |
newStream := self new pathName:filename in:aDirectory. |
8508
68cd95545196
Allow to proceed with any stream from an #openError handler
Stefan Vogel <sv@exept.de>
parents:
8472
diff
changeset
|
585 |
^ newStream openForReadWrite. |
7827 | 586 |
|
587 |
" |
|
588 |
FileStream oldFileNamed:'dAsGiBtEsNiChT' in:'/' |
|
589 |
" |
|
1 | 590 |
! |
591 |
||
15462 | 592 |
open:aFilenameString withMode:anArrayOrString |
593 |
"The argument de |
|
594 |
The file is opened for read/write access." |
|
595 |
||
596 |
|stream| |
|
597 |
||
598 |
stream := self new pathName:aFilenameString. |
|
15542 | 599 |
stream |
600 |
readwrite; "/ assume read/write mode, but this depends on the args |
|
601 |
openWithMode:anArrayOrString attributes:nil. |
|
15462 | 602 |
|
603 |
^ stream |
|
604 |
! |
|
605 |
||
1 | 606 |
readonlyFileNamed:filename |
20157 | 607 |
"Return a readonly FileStream for existing file named filename, a String. |
7827 | 608 |
Raises an error if the file does not exist." |
1 | 609 |
|
610 |
|newStream| |
|
611 |
||
20157 | 612 |
"/ We can do the following, but it is an extra system call: |
7091
b399f3d19084
Handle openErrorSignal in preparition for change openErrorSignal
Stefan Vogel <sv@exept.de>
parents:
7053
diff
changeset
|
613 |
"/ (OperatingSystem isReadable:filename) ifFalse:[^ self new openError]. |
1 | 614 |
|
2 | 615 |
newStream := self new pathName:filename. |
8508
68cd95545196
Allow to proceed with any stream from an #openError handler
Stefan Vogel <sv@exept.de>
parents:
8472
diff
changeset
|
616 |
^ newStream openForReading. |
7827 | 617 |
|
618 |
" |
|
8627
5a86b95bebbb
gcc3.3.1 insn-scheduling bug workaround
Claus Gittinger <cg@exept.de>
parents:
8604
diff
changeset
|
619 |
FileStream readonlyFileNamed:'dAsGiBtEsNiChT' |
7827 | 620 |
" |
1 | 621 |
! |
622 |
||
623 |
readonlyFileNamed:filename in:aDirectory |
|
624 |
"return a readonly FileStream for existing file named filename, aString |
|
7827 | 625 |
in aDirectory, a fileName or string instance representing a directory. |
626 |
Raises an error if the file does not exist." |
|
1 | 627 |
|
628 |
|newStream| |
|
2 | 629 |
newStream := self new pathName:filename in:aDirectory. |
8508
68cd95545196
Allow to proceed with any stream from an #openError handler
Stefan Vogel <sv@exept.de>
parents:
8472
diff
changeset
|
630 |
^ newStream openForReading. |
7827 | 631 |
|
632 |
" |
|
633 |
FileStream readonlyFileNamed:'dAsGiBtEsNiChT' in:'/' |
|
634 |
FileStream readonlyFileNamed:'dAsGiBtEsNiChT' in:'/' asFilename |
|
635 |
" |
|
159 | 636 |
! ! |
637 |
||
7298 | 638 |
!FileStream class methodsFor:'Compatibility-ANSI'! |
6586 | 639 |
|
640 |
write:filename |
|
641 |
"return a FileStream for new file named filename, aString. |
|
642 |
If the file exists, it is truncated, otherwise created. |
|
643 |
The file is opened for read/write access. |
|
644 |
Same as newFileNamed: for ANSI compatibilily" |
|
645 |
||
646 |
^ self newFileNamed:filename |
|
647 |
! ! |
|
648 |
||
7298 | 649 |
!FileStream class methodsFor:'Compatibility-Dolphin'! |
6244 | 650 |
|
651 |
read:filename text:text |
|
652 |
"return a readonly FileStream for the existing file named filename, aString. |
|
653 |
If the argument, text is false, the stream is setup to read binary bytes, |
|
654 |
if false, it reads characters." |
|
655 |
||
656 |
|s| |
|
657 |
||
658 |
s := self readOnlyFileNamed:filename. |
|
659 |
text ifTrue:[ |
|
7872 | 660 |
s text |
6244 | 661 |
] ifFalse:[ |
7872 | 662 |
s binary |
6244 | 663 |
]. |
664 |
^ s |
|
6474 | 665 |
! |
666 |
||
667 |
write:filename mode:modeSymbol |
|
668 |
"return a writable FileStream for the file named filename, aString. |
|
669 |
The modeSymbol controls how the file is opened; currently supported are: |
|
7965 | 670 |
#append |
6474 | 671 |
" |
672 |
||
673 |
modeSymbol == #append ifTrue:[ |
|
7965 | 674 |
^ self appendingOldFileNamed:filename |
6474 | 675 |
]. |
7091
b399f3d19084
Handle openErrorSignal in preparition for change openErrorSignal
Stefan Vogel <sv@exept.de>
parents:
7053
diff
changeset
|
676 |
"/ self openErrorSignal is a Notification. This will change.. |
b399f3d19084
Handle openErrorSignal in preparition for change openErrorSignal
Stefan Vogel <sv@exept.de>
parents:
7053
diff
changeset
|
677 |
"/ We want to raise a real error here |
b399f3d19084
Handle openErrorSignal in preparition for change openErrorSignal
Stefan Vogel <sv@exept.de>
parents:
7053
diff
changeset
|
678 |
super openErrorSignal raiseRequestErrorString:' - unsupported mode'. |
6474 | 679 |
^ nil |
680 |
! |
|
681 |
||
682 |
write:filename text:textModeBoolean |
|
683 |
"return a writable FileStream for the file named filename, aString. |
|
684 |
If the argument, text is false, the stream is setup to write binary bytes, |
|
685 |
if false, it writes characters." |
|
686 |
||
687 |
|s| |
|
688 |
||
6475 | 689 |
s := self newFileNamed:filename. |
6474 | 690 |
s notNil ifTrue:[ |
7872 | 691 |
textModeBoolean ifTrue:[ |
692 |
s text |
|
693 |
] ifFalse:[ |
|
694 |
s binary |
|
695 |
] |
|
6474 | 696 |
]. |
697 |
^ s |
|
6244 | 698 |
! ! |
699 |
||
7298 | 700 |
!FileStream class methodsFor:'Compatibility-Squeak'! |
5186 | 701 |
|
19638 | 702 |
fileIn:aPath |
703 |
aPath asFilename fileIn |
|
704 |
! |
|
705 |
||
16109 | 706 |
forceNewFileNamed:filename |
707 |
"return a writing FileStream for new file named filename, aString. |
|
16113 | 708 |
If it already exists, it is overwritten silently." |
16109 | 709 |
|
710 |
^ self newFileNamed:filename |
|
711 |
! |
|
712 |
||
5186 | 713 |
readOnlyFileNamed:filename |
6244 | 714 |
"return a readonly FileStream for the existing file named filename, aString." |
715 |
||
5186 | 716 |
^ self readonlyFileNamed:filename |
23165 | 717 |
! |
718 |
||
719 |
readOnlyFileNamed:filename do:aBlock |
|
720 |
"open a readonly stream on filename and evaluate aBlock with it. |
|
24102 | 721 |
Return the value from aBlock. |
23165 | 722 |
Ensures that the stream is closed afterwards" |
723 |
||
724 |
^ filename asFilename readingFileDo:aBlock |
|
725 |
||
726 |
"Created: / 01-07-2018 / 12:45:19 / Claus Gittinger" |
|
5186 | 727 |
! ! |
728 |
||
7298 | 729 |
!FileStream class methodsFor:'Compatibility-VW'! |
6170
c00688d17b16
+ #badArgumentSignal (VW compat.)
Claus Gittinger <cg@exept.de>
parents:
5910
diff
changeset
|
730 |
|
c00688d17b16
+ #badArgumentSignal (VW compat.)
Claus Gittinger <cg@exept.de>
parents:
5910
diff
changeset
|
731 |
badArgumentsSignal |
c00688d17b16
+ #badArgumentSignal (VW compat.)
Claus Gittinger <cg@exept.de>
parents:
5910
diff
changeset
|
732 |
^ Error |
c00688d17b16
+ #badArgumentSignal (VW compat.)
Claus Gittinger <cg@exept.de>
parents:
5910
diff
changeset
|
733 |
! ! |
c00688d17b16
+ #badArgumentSignal (VW compat.)
Claus Gittinger <cg@exept.de>
parents:
5910
diff
changeset
|
734 |
|
3233
1d29ae45b333
Must redefine #openErrorSignal.
Stefan Vogel <sv@exept.de>
parents:
3231
diff
changeset
|
735 |
!FileStream class methodsFor:'Signal constants'! |
1d29ae45b333
Must redefine #openErrorSignal.
Stefan Vogel <sv@exept.de>
parents:
3231
diff
changeset
|
736 |
|
4907 | 737 |
userInitiatedFileSaveQuerySignal |
738 |
"return the query signal, which is raised before a user-initiated |
|
739 |
file-save / file-saveAs operation is performed. |
|
740 |
The query will be invoked with the fileName which is about to be |
|
741 |
written to. |
|
21751 | 742 |
The default signal here returns true, which will grant the save. |
4907 | 743 |
End-user applications may want to catch this signal, |
744 |
and only return true for certain directories." |
|
745 |
||
746 |
^ UserInitiatedFileSaveQuerySignal |
|
747 |
||
21751 | 748 |
"Modified (comment): / 16-05-2017 / 18:13:28 / mawalch" |
3233
1d29ae45b333
Must redefine #openErrorSignal.
Stefan Vogel <sv@exept.de>
parents:
3231
diff
changeset
|
749 |
! ! |
1d29ae45b333
Must redefine #openErrorSignal.
Stefan Vogel <sv@exept.de>
parents:
3231
diff
changeset
|
750 |
|
7261 | 751 |
!FileStream methodsFor:'Compatibility-Squeak'! |
3044 | 752 |
|
753 |
fullName |
|
754 |
"Squeak compatibility: return the full pathname" |
|
755 |
||
756 |
^ pathName asFilename pathName |
|
757 |
||
758 |
"Created: 17.10.1997 / 17:04:12 / cg" |
|
759 |
"Modified: 20.10.1997 / 19:22:44 / cg" |
|
760 |
! ! |
|
761 |
||
15489 | 762 |
!FileStream methodsFor:'access rights'! |
763 |
||
764 |
accessRights |
|
765 |
"return the access rights of the file as opaque data |
|
766 |
(SmallInteger in unix/linux)" |
|
767 |
||
25318 | 768 |
|fd| |
769 |
||
770 |
"self fileDescriptor sets handle to nil and |
|
771 |
answers nil, if the handle is no longer valid!!" |
|
772 |
fd := self fileDescriptor. |
|
773 |
fd isNil ifTrue:[ |
|
774 |
self errorNotOpen. |
|
775 |
^ nil. |
|
776 |
]. |
|
777 |
^ OperatingSystem accessModeOfFd:fd. |
|
15489 | 778 |
|
779 |
" |
|
780 |
'Make.proto' asFilename readingFileDo:[:s| |
|
25318 | 781 |
s accessRights |
15489 | 782 |
] |
783 |
" |
|
25318 | 784 |
|
785 |
"Modified (comment): / 04-03-2020 / 13:11:30 / Stefan Vogel" |
|
15489 | 786 |
! |
787 |
||
788 |
accessRights:opaqueData |
|
789 |
"set the access rights of the file to opaqueData, |
|
790 |
which is normally retrieved by Filename>>#accessRights |
|
791 |
or FileStreamm>>#accessRights." |
|
792 |
||
25318 | 793 |
|fd| |
794 |
||
795 |
fd := self fileHandle. |
|
796 |
fd isNil ifTrue:[ |
|
797 |
self errorNotOpen. |
|
798 |
^ self. |
|
799 |
]. |
|
800 |
||
801 |
(OperatingSystem changeAccessModeOfFd:fd to:opaqueData) ifFalse:[ |
|
802 |
OperatingSystem accessDeniedErrorSignal |
|
803 |
raiseRequestWith:self |
|
804 |
errorString:(' - cannot change access rights: ' , self pathName). |
|
15489 | 805 |
]. |
806 |
||
807 |
" |
|
808 |
'Make.proto' asFilename readingFileDo:[:s| |
|
25318 | 809 |
s accessRights:s accessRights |
15489 | 810 |
] |
811 |
" |
|
812 |
||
813 |
" |
|
814 |
'/' asFilename readingFileDo:[:s| |
|
25318 | 815 |
s accessRights:s accessRights |
15489 | 816 |
] |
817 |
" |
|
25318 | 818 |
|
819 |
"Modified: / 04-03-2020 / 15:00:22 / Stefan Vogel" |
|
15489 | 820 |
! ! |
821 |
||
1 | 822 |
!FileStream methodsFor:'accessing'! |
823 |
||
17309 | 824 |
asFilename |
825 |
"return the file name" |
|
826 |
||
827 |
^ pathName asFilename |
|
828 |
! |
|
829 |
||
22570 | 830 |
baseName |
831 |
"return my name without leading direcory-path (i.e. the plain fileName) |
|
832 |
as a string" |
|
833 |
||
834 |
^ pathName asFilename baseName |
|
835 |
! |
|
836 |
||
12950
5a6dbcf1a274
added: #contentsOfEntireFile
Claus Gittinger <cg@exept.de>
parents:
12711
diff
changeset
|
837 |
contentsOfEntireFile |
5a6dbcf1a274
added: #contentsOfEntireFile
Claus Gittinger <cg@exept.de>
parents:
12711
diff
changeset
|
838 |
"ST-80 compatibility: return contents as a String (or byteArray, if in binary mode). |
5a6dbcf1a274
added: #contentsOfEntireFile
Claus Gittinger <cg@exept.de>
parents:
12711
diff
changeset
|
839 |
See also #contents, which returns the lines as stringCollection for text files." |
5a6dbcf1a274
added: #contentsOfEntireFile
Claus Gittinger <cg@exept.de>
parents:
12711
diff
changeset
|
840 |
|
5a6dbcf1a274
added: #contentsOfEntireFile
Claus Gittinger <cg@exept.de>
parents:
12711
diff
changeset
|
841 |
position == 0 ifTrue:[ |
13778 | 842 |
^ self next:(self fileSize) |
12950
5a6dbcf1a274
added: #contentsOfEntireFile
Claus Gittinger <cg@exept.de>
parents:
12711
diff
changeset
|
843 |
]. |
5a6dbcf1a274
added: #contentsOfEntireFile
Claus Gittinger <cg@exept.de>
parents:
12711
diff
changeset
|
844 |
|
5a6dbcf1a274
added: #contentsOfEntireFile
Claus Gittinger <cg@exept.de>
parents:
12711
diff
changeset
|
845 |
^ super contentsOfEntireFile |
5a6dbcf1a274
added: #contentsOfEntireFile
Claus Gittinger <cg@exept.de>
parents:
12711
diff
changeset
|
846 |
|
5a6dbcf1a274
added: #contentsOfEntireFile
Claus Gittinger <cg@exept.de>
parents:
12711
diff
changeset
|
847 |
"Created: / 18-07-2010 / 22:17:49 / cg" |
5a6dbcf1a274
added: #contentsOfEntireFile
Claus Gittinger <cg@exept.de>
parents:
12711
diff
changeset
|
848 |
! |
5a6dbcf1a274
added: #contentsOfEntireFile
Claus Gittinger <cg@exept.de>
parents:
12711
diff
changeset
|
849 |
|
1 | 850 |
directoryName |
2903 | 851 |
"return the name of the directory I'm in as a string" |
1 | 852 |
|
2903 | 853 |
^ pathName asFilename directoryName |
1 | 854 |
! |
855 |
||
11437
7869fd22df88
#reset reopens a closed file (compatibility with other smalltalks)
Stefan Vogel <sv@exept.de>
parents:
11290
diff
changeset
|
856 |
fileName |
7869fd22df88
#reset reopens a closed file (compatibility with other smalltalks)
Stefan Vogel <sv@exept.de>
parents:
11290
diff
changeset
|
857 |
"return the file name - same as pathName for compatibility with |
7869fd22df88
#reset reopens a closed file (compatibility with other smalltalks)
Stefan Vogel <sv@exept.de>
parents:
11290
diff
changeset
|
858 |
other smalltalks" |
7869fd22df88
#reset reopens a closed file (compatibility with other smalltalks)
Stefan Vogel <sv@exept.de>
parents:
11290
diff
changeset
|
859 |
|
7869fd22df88
#reset reopens a closed file (compatibility with other smalltalks)
Stefan Vogel <sv@exept.de>
parents:
11290
diff
changeset
|
860 |
^ pathName asFilename |
7869fd22df88
#reset reopens a closed file (compatibility with other smalltalks)
Stefan Vogel <sv@exept.de>
parents:
11290
diff
changeset
|
861 |
! |
7869fd22df88
#reset reopens a closed file (compatibility with other smalltalks)
Stefan Vogel <sv@exept.de>
parents:
11290
diff
changeset
|
862 |
|
1 | 863 |
name |
22570 | 864 |
"return my path name as a string" |
1 | 865 |
|
22570 | 866 |
"/ ^ self baseName |
867 |
^ self pathName |
|
1 | 868 |
! |
869 |
||
870 |
pathName |
|
871 |
"return the pathname" |
|
872 |
||
873 |
^ pathName |
|
613 | 874 |
! |
875 |
||
3754
e281b9479361
added removeOnClose feature.
Claus Gittinger <cg@exept.de>
parents:
3233
diff
changeset
|
876 |
removeOnClose:aBoolean |
e281b9479361
added removeOnClose feature.
Claus Gittinger <cg@exept.de>
parents:
3233
diff
changeset
|
877 |
"set/clear the removeOnClose flag. |
e281b9479361
added removeOnClose feature.
Claus Gittinger <cg@exept.de>
parents:
3233
diff
changeset
|
878 |
If set, the file will be removed when closed. |
e281b9479361
added removeOnClose feature.
Claus Gittinger <cg@exept.de>
parents:
3233
diff
changeset
|
879 |
Provided mostly for OS's which do not allow an |
e281b9479361
added removeOnClose feature.
Claus Gittinger <cg@exept.de>
parents:
3233
diff
changeset
|
880 |
open file to be removed (i.e. non unixes), |
12207
cb911af5184f
comment in: #removeOnClose:
Claus Gittinger <cg@exept.de>
parents:
11813
diff
changeset
|
881 |
when a fileStream for a tempFile is used. |
3754
e281b9479361
added removeOnClose feature.
Claus Gittinger <cg@exept.de>
parents:
3233
diff
changeset
|
882 |
Especially, the CVS-SourceCodeManager returns |
e281b9479361
added removeOnClose feature.
Claus Gittinger <cg@exept.de>
parents:
3233
diff
changeset
|
883 |
this kind of file-handles occasionally. |
e281b9479361
added removeOnClose feature.
Claus Gittinger <cg@exept.de>
parents:
3233
diff
changeset
|
884 |
This is an ST/X special feature which is not portable |
e281b9479361
added removeOnClose feature.
Claus Gittinger <cg@exept.de>
parents:
3233
diff
changeset
|
885 |
to other systems." |
e281b9479361
added removeOnClose feature.
Claus Gittinger <cg@exept.de>
parents:
3233
diff
changeset
|
886 |
|
15475 | 887 |
removeOnClose := aBoolean. |
18781 | 888 |
self reRegisterForFinalization. |
3754
e281b9479361
added removeOnClose feature.
Claus Gittinger <cg@exept.de>
parents:
3233
diff
changeset
|
889 |
|
e281b9479361
added removeOnClose feature.
Claus Gittinger <cg@exept.de>
parents:
3233
diff
changeset
|
890 |
"Modified: / 13.8.1998 / 12:10:07 / cg" |
e281b9479361
added removeOnClose feature.
Claus Gittinger <cg@exept.de>
parents:
3233
diff
changeset
|
891 |
! |
e281b9479361
added removeOnClose feature.
Claus Gittinger <cg@exept.de>
parents:
3233
diff
changeset
|
892 |
|
613 | 893 |
store:something |
894 |
"what really should this do" |
|
895 |
||
896 |
self nextPutAll:something storeString |
|
897 |
! ! |
|
898 |
||
899 |
!FileStream methodsFor:'error handling'! |
|
900 |
||
12687 | 901 |
openError:errorNumber |
902 |
"report an error, that file open failed" |
|
903 |
||
904 |
"This has changed from previous releases: |
|
905 |
in earlier versions of ST/X, failed file open operations |
|
906 |
returned nil - now, an error is raised. |
|
907 |
The old behavior is simulated by providing an exception |
|
908 |
handler, which proceeds with nil: |
|
909 |
||
13778 | 910 |
OpenError ignoreIn:[ |
911 |
'old code expecting nil return values on errors when doing an open'. |
|
12687 | 912 |
|
13778 | 913 |
('/dasGIBtEsbeStimmtNiChT' asFilename readStream) isNil ifTrue:[ |
914 |
self warn:'open failed'. |
|
915 |
]. |
|
916 |
] |
|
12687 | 917 |
" |
918 |
||
919 |
"{ Pragma: +optSpace }" |
|
920 |
||
921 |
LastErrorNumber := errorNumber. |
|
922 |
^ super openError:errorNumber. |
|
923 |
||
924 |
" |
|
925 |
'/dasGIBtEsbeStimmtNiChT' asFilename readStream |
|
926 |
" |
|
613 | 927 |
! ! |
928 |
||
15469 | 929 |
!FileStream methodsFor:'finalization'! |
930 |
||
931 |
executor |
|
932 |
|executor| |
|
933 |
||
934 |
executor := super executor. |
|
18985 | 935 |
"keep the pathname for any FileStream" |
936 |
executor setPathName:pathName removeOnClose:removeOnClose. |
|
15469 | 937 |
^ executor |
938 |
! ! |
|
939 |
||
20367 | 940 |
|
8362 | 941 |
!FileStream methodsFor:'misc functions'! |
942 |
||
17151 | 943 |
syncFileSystem |
944 |
"sync the filesystem containing this FileStream" |
|
945 |
||
946 |
OperatingSystem syncFileSystem:handle. |
|
8362 | 947 |
! ! |
948 |
||
2936
5cce0f2166d9
OpenErrorSignal already defined in super (ExternalStream).
Stefan Vogel <sv@exept.de>
parents:
2921
diff
changeset
|
949 |
!FileStream methodsFor:'positioning'! |
5cce0f2166d9
OpenErrorSignal already defined in super (ExternalStream).
Stefan Vogel <sv@exept.de>
parents:
2921
diff
changeset
|
950 |
|
15620 | 951 |
position |
7053
13e04c48e23c
prep for 0-based stream position
Claus Gittinger <cg@exept.de>
parents:
7051
diff
changeset
|
952 |
"return the read/write position in the file" |
2936
5cce0f2166d9
OpenErrorSignal already defined in super (ExternalStream).
Stefan Vogel <sv@exept.de>
parents:
2921
diff
changeset
|
953 |
|
16298 | 954 |
|error| |
3971 | 955 |
%{ |
4910
33b0edbb1920
merged back FileStream_win32 into FileStream
Claus Gittinger <cg@exept.de>
parents:
4907
diff
changeset
|
956 |
HFILE f; |
7872 | 957 |
off_t currentPosition; |
2936
5cce0f2166d9
OpenErrorSignal already defined in super (ExternalStream).
Stefan Vogel <sv@exept.de>
parents:
2921
diff
changeset
|
958 |
|
12395
1db7ea277c93
filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents:
12207
diff
changeset
|
959 |
if (__INST(handle) != nil) { |
25367 | 960 |
do { |
961 |
f = __FILEVal(__INST(handle)); |
|
19860
324edacff5cc
unified cpu and os defines;
Claus Gittinger <cg@exept.de>
parents:
19638
diff
changeset
|
962 |
#ifdef __win32__ |
25367 | 963 |
__threadErrno = 0; |
964 |
if (__INST(buffered) == true) { |
|
6333 | 965 |
# if 0 |
25367 | 966 |
currentPosition = STX_C_CALL1( "ftell", ftell, f); |
6333 | 967 |
# else |
25367 | 968 |
currentPosition = ftell(f); |
969 |
__threadErrno = errno; |
|
6333 | 970 |
# endif |
25367 | 971 |
} else { |
972 |
OBJ rA = __INST(readAhead); |
|
973 |
off_t offs = 0; |
|
6333 | 974 |
|
25367 | 975 |
if (rA != nil) { |
976 |
__INST(readAhead) = nil; |
|
977 |
offs = -1; |
|
978 |
} |
|
6333 | 979 |
# if 0 |
25367 | 980 |
currentPosition = STX_C_CALL3( "lseek", lseek, fileno(f), offs, SEEK_CUR); |
6333 | 981 |
# else |
25367 | 982 |
currentPosition = lseek(fileno(f), offs, SEEK_CUR); |
983 |
__threadErrno = errno; |
|
6333 | 984 |
# endif |
25367 | 985 |
} |
19860
324edacff5cc
unified cpu and os defines;
Claus Gittinger <cg@exept.de>
parents:
19638
diff
changeset
|
986 |
#else /* !__win32__ */ |
25367 | 987 |
if (__INST(buffered) == true) { |
7883
1039acd0624b
Large file support (off_t with 64 bits) for UNIX
Stefan Vogel <sv@exept.de>
parents:
7874
diff
changeset
|
988 |
#ifdef _LFS_LARGEFILE |
25367 | 989 |
currentPosition = ftello(f); |
7883
1039acd0624b
Large file support (off_t with 64 bits) for UNIX
Stefan Vogel <sv@exept.de>
parents:
7874
diff
changeset
|
990 |
#else |
25367 | 991 |
currentPosition = ftell(f); |
8039
6ef0379296cc
#position0Based did not work on unbuffered streams (with 64 bit off_t).
Stefan Vogel <sv@exept.de>
parents:
8034
diff
changeset
|
992 |
#endif /* ! _LFS_LARGEFILE */ |
25367 | 993 |
} else { |
994 |
currentPosition = lseek(fileno(f), (off_t)0, SEEK_CUR); |
|
995 |
} |
|
8039
6ef0379296cc
#position0Based did not work on unbuffered streams (with 64 bit off_t).
Stefan Vogel <sv@exept.de>
parents:
8034
diff
changeset
|
996 |
#endif /* !WIN32 */ |
25367 | 997 |
} while ((currentPosition < 0) && (__threadErrno == EINTR)); |
6267
10016cc16511
pass name of API/C wrap function
Claus Gittinger <cg@exept.de>
parents:
6259
diff
changeset
|
998 |
|
25367 | 999 |
if (currentPosition >= 0) { |
1000 |
OBJ rslt; |
|
7872 | 1001 |
|
25367 | 1002 |
if (sizeof(currentPosition) == 8) { |
1003 |
rslt = __MKINT64(¤tPosition); |
|
1004 |
} else { |
|
1005 |
rslt = __MKINT(currentPosition); |
|
1006 |
} |
|
1007 |
__INST(position) = rslt; __STORE(self, rslt); |
|
1008 |
RETURN ( rslt ); |
|
1009 |
} |
|
23539 | 1010 |
# ifdef __win32__ |
25367 | 1011 |
__threadErrno = __WIN32_ERR(GetLastError()); |
23539 | 1012 |
# endif |
25367 | 1013 |
error = __mkSmallInteger(__threadErrno); |
2936
5cce0f2166d9
OpenErrorSignal already defined in super (ExternalStream).
Stefan Vogel <sv@exept.de>
parents:
2921
diff
changeset
|
1014 |
} |
5cce0f2166d9
OpenErrorSignal already defined in super (ExternalStream).
Stefan Vogel <sv@exept.de>
parents:
2921
diff
changeset
|
1015 |
%}. |
16298 | 1016 |
error notNil ifTrue:[ |
25367 | 1017 |
lastErrorNumber := error. |
1018 |
^ self ioError:error. |
|
16298 | 1019 |
]. |
12395
1db7ea277c93
filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents:
12207
diff
changeset
|
1020 |
handle isNil ifTrue:[^ self errorNotOpen]. |
2936
5cce0f2166d9
OpenErrorSignal already defined in super (ExternalStream).
Stefan Vogel <sv@exept.de>
parents:
2921
diff
changeset
|
1021 |
^ self primitiveFailed |
23539 | 1022 |
|
25367 | 1023 |
"Modified: / 28-04-2020 / 11:24:40 / Stefan Vogel" |
2936
5cce0f2166d9
OpenErrorSignal already defined in super (ExternalStream).
Stefan Vogel <sv@exept.de>
parents:
2921
diff
changeset
|
1024 |
! |
5cce0f2166d9
OpenErrorSignal already defined in super (ExternalStream).
Stefan Vogel <sv@exept.de>
parents:
2921
diff
changeset
|
1025 |
|
15620 | 1026 |
position:newPos |
2936
5cce0f2166d9
OpenErrorSignal already defined in super (ExternalStream).
Stefan Vogel <sv@exept.de>
parents:
2921
diff
changeset
|
1027 |
"set the read/write position in the file" |
5cce0f2166d9
OpenErrorSignal already defined in super (ExternalStream).
Stefan Vogel <sv@exept.de>
parents:
2921
diff
changeset
|
1028 |
|
16298 | 1029 |
|rslt error| |
3971 | 1030 |
%{ |
4910
33b0edbb1920
merged back FileStream_win32 into FileStream
Claus Gittinger <cg@exept.de>
parents:
4907
diff
changeset
|
1031 |
HFILE f; |
2936
5cce0f2166d9
OpenErrorSignal already defined in super (ExternalStream).
Stefan Vogel <sv@exept.de>
parents:
2921
diff
changeset
|
1032 |
long ret; |
5cce0f2166d9
OpenErrorSignal already defined in super (ExternalStream).
Stefan Vogel <sv@exept.de>
parents:
2921
diff
changeset
|
1033 |
OBJ fp; |
7871
5c357f1ba0c9
preps for 64bit positions; fixed 32bit position
Claus Gittinger <cg@exept.de>
parents:
7870
diff
changeset
|
1034 |
|
8913 | 1035 |
if ((__INST(canPosition) != false) || (newPos == __mkSmallInteger(0))) { |
25367 | 1036 |
if ((fp = __INST(handle)) != nil) { |
7883
1039acd0624b
Large file support (off_t with 64 bits) for UNIX
Stefan Vogel <sv@exept.de>
parents:
7874
diff
changeset
|
1037 |
|
19860
324edacff5cc
unified cpu and os defines;
Claus Gittinger <cg@exept.de>
parents:
19638
diff
changeset
|
1038 |
#if defined(_LFS_LARGE_FILE) && !defined(__win32__) |
7883
1039acd0624b
Large file support (off_t with 64 bits) for UNIX
Stefan Vogel <sv@exept.de>
parents:
7874
diff
changeset
|
1039 |
# define FSEEK fseeko |
25367 | 1040 |
off_t nP; |
7883
1039acd0624b
Large file support (off_t with 64 bits) for UNIX
Stefan Vogel <sv@exept.de>
parents:
7874
diff
changeset
|
1041 |
#else |
1039acd0624b
Large file support (off_t with 64 bits) for UNIX
Stefan Vogel <sv@exept.de>
parents:
7874
diff
changeset
|
1042 |
#define FSEEK fseek |
25367 | 1043 |
long nP; |
7883
1039acd0624b
Large file support (off_t with 64 bits) for UNIX
Stefan Vogel <sv@exept.de>
parents:
7874
diff
changeset
|
1044 |
#endif |
2936
5cce0f2166d9
OpenErrorSignal already defined in super (ExternalStream).
Stefan Vogel <sv@exept.de>
parents:
2921
diff
changeset
|
1045 |
|
25367 | 1046 |
if (__isSmallInteger(newPos)) { |
1047 |
nP = __intVal(newPos); |
|
1048 |
if (nP < 0) { |
|
1049 |
__INST(lastErrorNumber) = __mkSmallInteger(EINVAL); |
|
1050 |
goto getOutOfHere; |
|
1051 |
} |
|
1052 |
} else { |
|
1053 |
nP = __signedLongIntVal(newPos); |
|
1054 |
if (nP < 0) { |
|
1055 |
__INST(lastErrorNumber) = __mkSmallInteger(EINVAL); |
|
1056 |
goto getOutOfHere; |
|
1057 |
} |
|
1058 |
if (nP == 0) { |
|
1059 |
if (sizeof(nP) == 8) { |
|
1060 |
if (__signedLong64IntVal(newPos, &nP) == 0 || nP < 0) { |
|
1061 |
__INST(lastErrorNumber) = __mkSmallInteger(EINVAL); |
|
1062 |
goto getOutOfHere; |
|
1063 |
} |
|
1064 |
} else { |
|
1065 |
__INST(lastErrorNumber) = __mkSmallInteger(EINVAL); |
|
1066 |
goto getOutOfHere; |
|
1067 |
} |
|
1068 |
} |
|
1069 |
} |
|
7871
5c357f1ba0c9
preps for 64bit positions; fixed 32bit position
Claus Gittinger <cg@exept.de>
parents:
7870
diff
changeset
|
1070 |
|
25367 | 1071 |
f = __FILEVal(fp); |
4910
33b0edbb1920
merged back FileStream_win32 into FileStream
Claus Gittinger <cg@exept.de>
parents:
4907
diff
changeset
|
1072 |
|
25367 | 1073 |
do { |
14853
63ffca691137
fseek does not need a WRAP_CALL (I hope)
Claus Gittinger <cg@exept.de>
parents:
14848
diff
changeset
|
1074 |
#if defined(DO_WRAP_CALL_FSEEK) |
25367 | 1075 |
__threadErrno = 0; |
1076 |
if (__INST(buffered) == true) { |
|
1077 |
ret = STX_C_CALL3( "fseek", fseek, f, nP, SEEK_SET); |
|
1078 |
} else { |
|
1079 |
__INST(readAhead) = nil; |
|
1080 |
ret = STX_C_CALL3( "lseek", lseek, fileno(f), nP, SEEK_SET); |
|
1081 |
} |
|
4910
33b0edbb1920
merged back FileStream_win32 into FileStream
Claus Gittinger <cg@exept.de>
parents:
4907
diff
changeset
|
1082 |
#else |
25367 | 1083 |
errno = 0; |
1084 |
if (__INST(buffered) == true) { |
|
1085 |
ret = FSEEK(f, nP, SEEK_SET); |
|
1086 |
} else { |
|
1087 |
ret = lseek(fileno(f), nP, SEEK_SET); |
|
1088 |
} |
|
1089 |
__threadErrno = errno; |
|
4910
33b0edbb1920
merged back FileStream_win32 into FileStream
Claus Gittinger <cg@exept.de>
parents:
4907
diff
changeset
|
1090 |
#endif |
25367 | 1091 |
} while ((ret < 0) && (__threadErrno == EINTR)); |
1092 |
if (ret >= 0) { |
|
1093 |
__INST(position) = newPos; __STORE(self, newPos); |
|
1094 |
/* |
|
1095 |
* just to make certain ... |
|
1096 |
*/ |
|
1097 |
__INST(hitEOF) = false; |
|
1098 |
RETURN ( self ); |
|
1099 |
} |
|
23539 | 1100 |
# ifdef __win32__ |
25367 | 1101 |
__threadErrno = __WIN32_ERR(GetLastError()); |
23539 | 1102 |
# endif |
25367 | 1103 |
error = __mkSmallInteger(__threadErrno); |
1104 |
} |
|
2936
5cce0f2166d9
OpenErrorSignal already defined in super (ExternalStream).
Stefan Vogel <sv@exept.de>
parents:
2921
diff
changeset
|
1105 |
} |
7871
5c357f1ba0c9
preps for 64bit positions; fixed 32bit position
Claus Gittinger <cg@exept.de>
parents:
7870
diff
changeset
|
1106 |
getOutOfHere: ; |
7883
1039acd0624b
Large file support (off_t with 64 bits) for UNIX
Stefan Vogel <sv@exept.de>
parents:
7874
diff
changeset
|
1107 |
#undef FSEEK |
2936
5cce0f2166d9
OpenErrorSignal already defined in super (ExternalStream).
Stefan Vogel <sv@exept.de>
parents:
2921
diff
changeset
|
1108 |
%}. |
5cce0f2166d9
OpenErrorSignal already defined in super (ExternalStream).
Stefan Vogel <sv@exept.de>
parents:
2921
diff
changeset
|
1109 |
canPosition == false ifTrue:[ |
25367 | 1110 |
"/ VMS only: position by rewinding & re-reading everything up-to |
1111 |
"/ that point. |
|
1112 |
^ self slowPosition:newPos |
|
2936
5cce0f2166d9
OpenErrorSignal already defined in super (ExternalStream).
Stefan Vogel <sv@exept.de>
parents:
2921
diff
changeset
|
1113 |
]. |
16298 | 1114 |
error notNil ifTrue:[ |
25367 | 1115 |
lastErrorNumber := error. |
1116 |
(OperatingSystem errorSymbolForNumber:error) == #EINVAL ifTrue:[ |
|
1117 |
"/ invalid position |
|
1118 |
^ self positionError. |
|
1119 |
]. |
|
1120 |
"/ assume I/O error |
|
1121 |
^ self ioError:error. |
|
2963 | 1122 |
]. |
12395
1db7ea277c93
filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents:
12207
diff
changeset
|
1123 |
handle isNil ifTrue:[^ self errorNotOpen]. |
7871
5c357f1ba0c9
preps for 64bit positions; fixed 32bit position
Claus Gittinger <cg@exept.de>
parents:
7870
diff
changeset
|
1124 |
|
12395
1db7ea277c93
filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents:
12207
diff
changeset
|
1125 |
rslt := self positionFile:handle position:newPos. |
5493 | 1126 |
rslt >= 0 ifTrue:[ |
25367 | 1127 |
position := newPos. |
5493 | 1128 |
] ifFalse:[ |
25367 | 1129 |
hitEOF := true. |
5493 | 1130 |
] |
23539 | 1131 |
|
25367 | 1132 |
"Modified: / 28-04-2020 / 11:17:37 / Stefan Vogel" |
2936
5cce0f2166d9
OpenErrorSignal already defined in super (ExternalStream).
Stefan Vogel <sv@exept.de>
parents:
2921
diff
changeset
|
1133 |
! |
5cce0f2166d9
OpenErrorSignal already defined in super (ExternalStream).
Stefan Vogel <sv@exept.de>
parents:
2921
diff
changeset
|
1134 |
|
11437
7869fd22df88
#reset reopens a closed file (compatibility with other smalltalks)
Stefan Vogel <sv@exept.de>
parents:
11290
diff
changeset
|
1135 |
reset |
7869fd22df88
#reset reopens a closed file (compatibility with other smalltalks)
Stefan Vogel <sv@exept.de>
parents:
11290
diff
changeset
|
1136 |
"additionaly to setting the position to the beginning of the file, |
7869fd22df88
#reset reopens a closed file (compatibility with other smalltalks)
Stefan Vogel <sv@exept.de>
parents:
11290
diff
changeset
|
1137 |
re-open a previously closed file. This behavior is compatible |
7869fd22df88
#reset reopens a closed file (compatibility with other smalltalks)
Stefan Vogel <sv@exept.de>
parents:
11290
diff
changeset
|
1138 |
with other Smalltalk dialects" |
7869fd22df88
#reset reopens a closed file (compatibility with other smalltalks)
Stefan Vogel <sv@exept.de>
parents:
11290
diff
changeset
|
1139 |
|
12395
1db7ea277c93
filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents:
12207
diff
changeset
|
1140 |
handle isNil ifTrue:[ |
1db7ea277c93
filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents:
12207
diff
changeset
|
1141 |
"reopen the file" |
1db7ea277c93
filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents:
12207
diff
changeset
|
1142 |
mode == #readonly ifTrue: [ |
1db7ea277c93
filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents:
12207
diff
changeset
|
1143 |
self openForReading |
1db7ea277c93
filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents:
12207
diff
changeset
|
1144 |
] ifFalse:[mode == #writeonly ifTrue: [ |
1db7ea277c93
filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents:
12207
diff
changeset
|
1145 |
self openForWriting. |
1db7ea277c93
filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents:
12207
diff
changeset
|
1146 |
] ifFalse:[ |
1db7ea277c93
filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents:
12207
diff
changeset
|
1147 |
self openForReadWrite. |
1db7ea277c93
filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents:
12207
diff
changeset
|
1148 |
]]. |
11437
7869fd22df88
#reset reopens a closed file (compatibility with other smalltalks)
Stefan Vogel <sv@exept.de>
parents:
11290
diff
changeset
|
1149 |
] ifFalse:[ |
12395
1db7ea277c93
filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents:
12207
diff
changeset
|
1150 |
super reset. |
11437
7869fd22df88
#reset reopens a closed file (compatibility with other smalltalks)
Stefan Vogel <sv@exept.de>
parents:
11290
diff
changeset
|
1151 |
]. |
7869fd22df88
#reset reopens a closed file (compatibility with other smalltalks)
Stefan Vogel <sv@exept.de>
parents:
11290
diff
changeset
|
1152 |
! |
7869fd22df88
#reset reopens a closed file (compatibility with other smalltalks)
Stefan Vogel <sv@exept.de>
parents:
11290
diff
changeset
|
1153 |
|
2936
5cce0f2166d9
OpenErrorSignal already defined in super (ExternalStream).
Stefan Vogel <sv@exept.de>
parents:
2921
diff
changeset
|
1154 |
setToEnd |
5cce0f2166d9
OpenErrorSignal already defined in super (ExternalStream).
Stefan Vogel <sv@exept.de>
parents:
2921
diff
changeset
|
1155 |
"set the read/write position in the file to be at the end of the file" |
5cce0f2166d9
OpenErrorSignal already defined in super (ExternalStream).
Stefan Vogel <sv@exept.de>
parents:
2921
diff
changeset
|
1156 |
|
16298 | 1157 |
|error| |
2936
5cce0f2166d9
OpenErrorSignal already defined in super (ExternalStream).
Stefan Vogel <sv@exept.de>
parents:
2921
diff
changeset
|
1158 |
%{ |
4910
33b0edbb1920
merged back FileStream_win32 into FileStream
Claus Gittinger <cg@exept.de>
parents:
4907
diff
changeset
|
1159 |
HFILE f; |
7883
1039acd0624b
Large file support (off_t with 64 bits) for UNIX
Stefan Vogel <sv@exept.de>
parents:
7874
diff
changeset
|
1160 |
off_t ret; |
2936
5cce0f2166d9
OpenErrorSignal already defined in super (ExternalStream).
Stefan Vogel <sv@exept.de>
parents:
2921
diff
changeset
|
1161 |
|
12395
1db7ea277c93
filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents:
12207
diff
changeset
|
1162 |
if (__INST(handle) != nil) { |
24102 | 1163 |
f = __FILEVal(__INST(handle)); |
1164 |
__INST(position) = nil; /* i.e. unknown */ |
|
1165 |
do { |
|
19860
324edacff5cc
unified cpu and os defines;
Claus Gittinger <cg@exept.de>
parents:
19638
diff
changeset
|
1166 |
#ifdef __win32__ |
24102 | 1167 |
__threadErrno = 0; |
1168 |
if (__INST(buffered) == true) { |
|
1169 |
ret = STX_C_CALL3( "fseek", fseek, f, 0L, SEEK_END); |
|
1170 |
} else { |
|
1171 |
__INST(readAhead) = nil; |
|
1172 |
ret = STX_C_CALL3( "lseek", lseek, fileno(f), 0L, SEEK_END); |
|
1173 |
} |
|
4910
33b0edbb1920
merged back FileStream_win32 into FileStream
Claus Gittinger <cg@exept.de>
parents:
4907
diff
changeset
|
1174 |
#else |
24102 | 1175 |
if (__INST(buffered) == true) { |
19860
324edacff5cc
unified cpu and os defines;
Claus Gittinger <cg@exept.de>
parents:
19638
diff
changeset
|
1176 |
# ifdef _LFS_LARGE_FILE |
24102 | 1177 |
ret = fseeko(f, (off_t)0, SEEK_END); |
19860
324edacff5cc
unified cpu and os defines;
Claus Gittinger <cg@exept.de>
parents:
19638
diff
changeset
|
1178 |
# else |
24102 | 1179 |
ret = fseek(f, 0L, SEEK_END); |
19860
324edacff5cc
unified cpu and os defines;
Claus Gittinger <cg@exept.de>
parents:
19638
diff
changeset
|
1180 |
# endif |
24102 | 1181 |
} else { |
1182 |
ret = lseek(fileno(f), (off_t)0, SEEK_END); |
|
1183 |
} |
|
7883
1039acd0624b
Large file support (off_t with 64 bits) for UNIX
Stefan Vogel <sv@exept.de>
parents:
7874
diff
changeset
|
1184 |
#endif |
24102 | 1185 |
} while ((ret < 0) && (__threadErrno == EINTR)); |
1186 |
if (ret >= 0) { |
|
1187 |
RETURN ( self ); |
|
1188 |
} |
|
23539 | 1189 |
# ifdef __win32__ |
24102 | 1190 |
__threadErrno = __WIN32_ERR(GetLastError()); |
23539 | 1191 |
# endif |
24102 | 1192 |
error = __mkSmallInteger(__threadErrno); |
2936
5cce0f2166d9
OpenErrorSignal already defined in super (ExternalStream).
Stefan Vogel <sv@exept.de>
parents:
2921
diff
changeset
|
1193 |
} |
5cce0f2166d9
OpenErrorSignal already defined in super (ExternalStream).
Stefan Vogel <sv@exept.de>
parents:
2921
diff
changeset
|
1194 |
%}. |
16298 | 1195 |
error notNil ifTrue:[ |
24102 | 1196 |
lastErrorNumber := error. |
1197 |
self ioError:error. |
|
1198 |
^ self. |
|
16298 | 1199 |
]. |
12395
1db7ea277c93
filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents:
12207
diff
changeset
|
1200 |
handle isNil ifTrue:[^ self errorNotOpen]. |
2936
5cce0f2166d9
OpenErrorSignal already defined in super (ExternalStream).
Stefan Vogel <sv@exept.de>
parents:
2921
diff
changeset
|
1201 |
^ self primitiveFailed |
23539 | 1202 |
|
1203 |
"Modified: / 22-11-2018 / 14:54:29 / Stefan Vogel" |
|
2936
5cce0f2166d9
OpenErrorSignal already defined in super (ExternalStream).
Stefan Vogel <sv@exept.de>
parents:
2921
diff
changeset
|
1204 |
! |
5cce0f2166d9
OpenErrorSignal already defined in super (ExternalStream).
Stefan Vogel <sv@exept.de>
parents:
2921
diff
changeset
|
1205 |
|
5cce0f2166d9
OpenErrorSignal already defined in super (ExternalStream).
Stefan Vogel <sv@exept.de>
parents:
2921
diff
changeset
|
1206 |
slowPosition:newPos |
5cce0f2166d9
OpenErrorSignal already defined in super (ExternalStream).
Stefan Vogel <sv@exept.de>
parents:
2921
diff
changeset
|
1207 |
"position the file by re-reading everything up-to newPos. |
5cce0f2166d9
OpenErrorSignal already defined in super (ExternalStream).
Stefan Vogel <sv@exept.de>
parents:
2921
diff
changeset
|
1208 |
The effect is the same as that of #position:, but its much slower. |
5cce0f2166d9
OpenErrorSignal already defined in super (ExternalStream).
Stefan Vogel <sv@exept.de>
parents:
2921
diff
changeset
|
1209 |
This is required to reposition nonPositionable streams, such |
5cce0f2166d9
OpenErrorSignal already defined in super (ExternalStream).
Stefan Vogel <sv@exept.de>
parents:
2921
diff
changeset
|
1210 |
as tape-streams or variable-record-RMS files under VMS. |
5cce0f2166d9
OpenErrorSignal already defined in super (ExternalStream).
Stefan Vogel <sv@exept.de>
parents:
2921
diff
changeset
|
1211 |
Caveat: |
18651 | 1212 |
This should really be done transparently by the stdio library." |
15620 | 1213 |
|
1214 |
|buffer amount pos0Based| |
|
1215 |
||
1216 |
self isReadable ifFalse:[ |
|
18651 | 1217 |
"/ sorry |
1218 |
^ self positionError |
|
15620 | 1219 |
]. |
1220 |
||
1221 |
buffer := ByteArray new:8*1024. |
|
2936
5cce0f2166d9
OpenErrorSignal already defined in super (ExternalStream).
Stefan Vogel <sv@exept.de>
parents:
2921
diff
changeset
|
1222 |
|
15620 | 1223 |
(position isNil "/ i.e. unknown |
1224 |
or:[newPos < (pos0Based := self position)]) ifTrue:[ |
|
18651 | 1225 |
self reset. |
1226 |
pos0Based := self position. |
|
15620 | 1227 |
]. |
1228 |
[pos0Based < newPos] whileTrue:[ |
|
18651 | 1229 |
amount := (buffer size) min:(newPos-pos0Based). |
1230 |
(self nextBytes:amount into:buffer startingAt:1) ~~ amount ifTrue:[ |
|
1231 |
^ self positionError |
|
1232 |
]. |
|
1233 |
pos0Based := self position. |
|
15620 | 1234 |
]. |
1235 |
"/ ('FileStream [info]: slow position - please convert ''' , pathName printString , ''' to streamLF format') infoPrintCR. |
|
25371 | 1236 |
! |
1237 |
||
1238 |
truncateTo:newSize |
|
1239 |
"truncate the underlying OS file to newSize. |
|
1240 |
Warning: this may not be implemented on all platforms." |
|
1241 |
||
1242 |
%{ |
|
1243 |
#ifdef HAS_FTRUNCATE |
|
1244 |
OBJ fp = __INST(handle); |
|
1245 |
FILE *f; |
|
1246 |
off_t truncateSize; |
|
1247 |
||
1248 |
if ((fp != nil) && (__INST(mode) != @symbol(readonly))) { |
|
1249 |
if (__isSmallInteger(newSize)) { |
|
1250 |
truncateSize = __intVal(newSize); |
|
1251 |
if (truncateSize < 0) { |
|
1252 |
goto getOutOfHere; |
|
1253 |
} |
|
1254 |
} else { |
|
1255 |
truncateSize = __signedLongIntVal(newSize); |
|
1256 |
if (truncateSize < 0) { |
|
1257 |
goto getOutOfHere; |
|
1258 |
} |
|
1259 |
if (truncateSize == 0) { |
|
1260 |
if (sizeof(truncateSize) == 8) { |
|
1261 |
if (__signedLong64IntVal(newSize, &truncateSize) == 0 || truncateSize < 0) { |
|
1262 |
goto getOutOfHere; |
|
1263 |
} |
|
1264 |
} else { |
|
1265 |
goto getOutOfHere; |
|
1266 |
} |
|
1267 |
} |
|
1268 |
} |
|
1269 |
||
1270 |
f = __FILEVal(fp); |
|
1271 |
||
1272 |
if (__INST(buffered) == true) { |
|
1273 |
fflush(f); |
|
1274 |
fseek(f, 0L, SEEK_END); /* needed in stdio */ |
|
1275 |
} |
|
1276 |
ftruncate(fileno(f), truncateSize); |
|
1277 |
||
1278 |
// oops - must also position |
|
1279 |
if (__INST(buffered) == true) { |
|
1280 |
fseek(f, truncateSize, SEEK_SET); |
|
1281 |
} else { |
|
1282 |
lseek(fileno(f), truncateSize, SEEK_SET); |
|
1283 |
} |
|
1284 |
__INST(position) = newSize; __STORE(self, newSize); |
|
1285 |
||
1286 |
RETURN (self); |
|
1287 |
} |
|
1288 |
getOutOfHere: ; |
|
1289 |
#endif |
|
1290 |
%}. |
|
1291 |
handle isNil ifTrue:[self errorNotOpen. ^ self]. |
|
1292 |
(mode == #readonly) ifTrue:[self errorReadOnly. ^ self]. |
|
1293 |
newSize < 0 ifTrue:[ |
|
1294 |
self error:'wrong arg'. |
|
1295 |
]. |
|
1296 |
self errorUnsupportedOperation |
|
1297 |
||
1298 |
" |
|
1299 |
|f s| |
|
1300 |
||
1301 |
f := 'testTTTT' asFilename. |
|
1302 |
s := f writeStream. |
|
1303 |
s next:1000 put:$a. |
|
1304 |
s truncateTo:100. |
|
1305 |
s close. |
|
1306 |
||
1307 |
Transcript showCR:(f fileSize). |
|
1308 |
f remove. |
|
1309 |
" |
|
1310 |
||
1311 |
"Modified: / 02-08-2017 / 09:44:32 / cg" |
|
2936
5cce0f2166d9
OpenErrorSignal already defined in super (ExternalStream).
Stefan Vogel <sv@exept.de>
parents:
2921
diff
changeset
|
1312 |
! ! |
5cce0f2166d9
OpenErrorSignal already defined in super (ExternalStream).
Stefan Vogel <sv@exept.de>
parents:
2921
diff
changeset
|
1313 |
|
613 | 1314 |
!FileStream methodsFor:'printing & storing'! |
1315 |
||
1316 |
printOn:aStream |
|
4392 | 1317 |
"append a user printed representation of the receiver to aStream. |
1318 |
The format is suitable for a human - not meant to be read back." |
|
2903 | 1319 |
|
10354 | 1320 |
aStream nextPutAll:self className,'(for: '. |
1321 |
pathName printOn:aStream. |
|
1322 |
aStream nextPut:$). |
|
2811
21136662e8f1
dont crash into debugger when printing with nil pathName
Claus Gittinger <cg@exept.de>
parents:
2808
diff
changeset
|
1323 |
|
10354 | 1324 |
" |
10450
bb40b56ed6b6
Remove unused stat() redefinition
Stefan Vogel <sv@exept.de>
parents:
10407
diff
changeset
|
1325 |
'/' asFilename readStream printString |
10354 | 1326 |
" |
613 | 1327 |
! |
1328 |
||
1329 |
storeOn:aStream |
|
2903 | 1330 |
"append a representation of the receiver to aStream, |
1331 |
from which a copy can be reconstructed later." |
|
1332 |
||
613 | 1333 |
aStream nextPutAll:'(FileStream oldFileNamed:'. |
2811
21136662e8f1
dont crash into debugger when printing with nil pathName
Claus Gittinger <cg@exept.de>
parents:
2808
diff
changeset
|
1334 |
aStream nextPutAll:pathName storeString. |
15360 | 1335 |
(self position ~~ 0) ifTrue:[ |
15542 | 1336 |
aStream nextPutAll:'; position:'. |
1337 |
self position storeOn:aStream |
|
613 | 1338 |
]. |
1339 |
aStream nextPut:$) |
|
2811
21136662e8f1
dont crash into debugger when printing with nil pathName
Claus Gittinger <cg@exept.de>
parents:
2808
diff
changeset
|
1340 |
|
21136662e8f1
dont crash into debugger when printing with nil pathName
Claus Gittinger <cg@exept.de>
parents:
2808
diff
changeset
|
1341 |
"Modified: 30.7.1997 / 16:43:50 / cg" |
1 | 1342 |
! ! |
1343 |
||
18784 | 1344 |
!FileStream protectedMethodsFor:'private'! |
1 | 1345 |
|
8471
e061e503bf03
Documentation: users shold never send #closeFile
Stefan Vogel <sv@exept.de>
parents:
8362
diff
changeset
|
1346 |
closeFile |
e061e503bf03
Documentation: users shold never send #closeFile
Stefan Vogel <sv@exept.de>
parents:
8362
diff
changeset
|
1347 |
"low level close - may be redefined in subclasses. |
e061e503bf03
Documentation: users shold never send #closeFile
Stefan Vogel <sv@exept.de>
parents:
8362
diff
changeset
|
1348 |
Don't send this message, send #close instead" |
e061e503bf03
Documentation: users shold never send #closeFile
Stefan Vogel <sv@exept.de>
parents:
8362
diff
changeset
|
1349 |
|
e061e503bf03
Documentation: users shold never send #closeFile
Stefan Vogel <sv@exept.de>
parents:
8362
diff
changeset
|
1350 |
super closeFile. |
e061e503bf03
Documentation: users shold never send #closeFile
Stefan Vogel <sv@exept.de>
parents:
8362
diff
changeset
|
1351 |
removeOnClose == true ifTrue:[ |
8627
5a86b95bebbb
gcc3.3.1 insn-scheduling bug workaround
Claus Gittinger <cg@exept.de>
parents:
8604
diff
changeset
|
1352 |
pathName asFilename remove. |
8471
e061e503bf03
Documentation: users shold never send #closeFile
Stefan Vogel <sv@exept.de>
parents:
8362
diff
changeset
|
1353 |
] |
e061e503bf03
Documentation: users shold never send #closeFile
Stefan Vogel <sv@exept.de>
parents:
8362
diff
changeset
|
1354 |
|
e061e503bf03
Documentation: users shold never send #closeFile
Stefan Vogel <sv@exept.de>
parents:
8362
diff
changeset
|
1355 |
"Created: / 13.8.1998 / 12:11:22 / cg" |
18784 | 1356 |
! ! |
1357 |
||
1358 |
!FileStream methodsFor:'private'! |
|
8471
e061e503bf03
Documentation: users shold never send #closeFile
Stefan Vogel <sv@exept.de>
parents:
8362
diff
changeset
|
1359 |
|
613 | 1360 |
createForReadWrite |
1361 |
"create/truncate the file for read/write. |
|
20921 | 1362 |
If the file existed, it's truncated; otherwise it's created." |
1 | 1363 |
|
613 | 1364 |
mode := #readwrite. |
2921 | 1365 |
^ self openWithMode:CreateReadWriteMode |
1 | 1366 |
! |
1367 |
||
613 | 1368 |
createForWriting |
1369 |
"create/truncate the file for writeonly. |
|
20921 | 1370 |
If the file existed, it's truncated; otherwise it's created." |
1 | 1371 |
|
613 | 1372 |
mode := #writeonly. |
1373 |
didWrite := true. |
|
8604 | 1374 |
^ self openWithMode:WriteMode |
1 | 1375 |
! |
1376 |
||
5439 | 1377 |
openFile:pathName withMode:openmode attributes:attributeSpec |
8627
5a86b95bebbb
gcc3.3.1 insn-scheduling bug workaround
Claus Gittinger <cg@exept.de>
parents:
8604
diff
changeset
|
1378 |
"open the file; |
5439 | 1379 |
openmode is the string defining the way to open as defined by the stdio library |
1380 |
(i.e. the 2nd fopen argument). |
|
1381 |
||
1382 |
attributeSpec is an additional argument, only used with VMS - it allows a file to |
|
1383 |
be created as fixedRecord, variableRecord, streamLF, streamCR, ... |
|
1384 |
In VMS, if nonNil, it must consist of an array of strings (max:10), giving additional |
|
1385 |
attributes (see fopen description). |
|
1386 |
Passing a nil specifies the default format (streamLF) - ST/X always invokes this with nil. |
|
1387 |
This argument is ignored in UNIX & MSDOS systems. |
|
1388 |
||
1389 |
This is a private entry, but maybe useful to open/create a file in a special mode, |
|
1390 |
which is proprietrary to the operatingSystem." |
|
1391 |
||
16298 | 1392 |
|wasBlocked encodedPathName error| |
14578 | 1393 |
|
1394 |
encodedPathName := OperatingSystem encodePath:pathName. |
|
5439 | 1395 |
|
1396 |
%{ |
|
15462 | 1397 |
HFILE f = NULL; |
5439 | 1398 |
int pass = 0; |
1399 |
||
15462 | 1400 |
if (!__isNonNilObject(encodedPathName) |
24102 | 1401 |
|| !(__isStringLike(openmode) || __isArrayLike(openmode))) |
1402 |
goto badArgument; |
|
15462 | 1403 |
|
5439 | 1404 |
retry: |
1405 |
#ifdef __VMS__ |
|
15462 | 1406 |
if (__isStringLike(encodedPathName)) { |
24102 | 1407 |
do { |
1408 |
/* |
|
1409 |
* allow passing additional RMS arguments. |
|
1410 |
* stupid: DEC does not seem to offer an interface for passing a char **. |
|
1411 |
*/ |
|
1412 |
__threadErrno = 0; |
|
5439 | 1413 |
|
24102 | 1414 |
{ |
1415 |
if (__isArrayLike(attributeSpec)) { |
|
1416 |
OBJ *ap = __ArrayInstPtr(attributeSpec)->a_element; |
|
1417 |
int numAttrib = 0; |
|
1418 |
int i; |
|
5439 | 1419 |
|
24102 | 1420 |
numAttrib = __arraySize(attributeSpec); |
1421 |
for (i=0; i<numAttrib;i++) { |
|
1422 |
if (! __isStringLike(ap[i])) { |
|
1423 |
__threadErrno = EINVAL; /* invalid argument */ |
|
1424 |
goto getOutOfHere; |
|
1425 |
} |
|
1426 |
} |
|
1427 |
switch (numAttrib) { |
|
1428 |
case 0: |
|
1429 |
__BEGIN_INTERRUPTABLE__ |
|
1430 |
f = fopen((char *)__stringVal(pathName), (char *)__stringVal(openmode)); |
|
1431 |
__END_INTERRUPTABLE__ |
|
1432 |
break; |
|
1433 |
case 1: |
|
1434 |
__BEGIN_INTERRUPTABLE__ |
|
1435 |
f = fopen((char *)__stringVal(pathName), (char *)__stringVal(openmode), |
|
1436 |
__stringVal(ap[0])); |
|
1437 |
__END_INTERRUPTABLE__ |
|
1438 |
break; |
|
1439 |
case 2: |
|
1440 |
__BEGIN_INTERRUPTABLE__ |
|
1441 |
f = fopen((char *)__stringVal(pathName), (char *)__stringVal(openmode), |
|
1442 |
__stringVal(ap[0]), __stringVal(ap[1])); |
|
1443 |
__END_INTERRUPTABLE__ |
|
1444 |
break; |
|
1445 |
case 3: |
|
1446 |
__BEGIN_INTERRUPTABLE__ |
|
1447 |
f = fopen((char *)__stringVal(pathName), (char *)__stringVal(openmode), |
|
1448 |
__stringVal(ap[0]), __stringVal(ap[1]), __stringVal(ap[2])); |
|
1449 |
__END_INTERRUPTABLE__ |
|
1450 |
break; |
|
1451 |
case 4: |
|
1452 |
__BEGIN_INTERRUPTABLE__ |
|
1453 |
f = fopen((char *)__stringVal(pathName), (char *)__stringVal(openmode), |
|
1454 |
__stringVal(ap[0]), __stringVal(ap[1]), __stringVal(ap[2]), |
|
1455 |
__stringVal(ap[3])); |
|
1456 |
__END_INTERRUPTABLE__ |
|
1457 |
break; |
|
1458 |
case 5: |
|
1459 |
__BEGIN_INTERRUPTABLE__ |
|
1460 |
f = fopen((char *)__stringVal(pathName), (char *)__stringVal(openmode), |
|
1461 |
__stringVal(ap[0]), __stringVal(ap[1]), __stringVal(ap[2]), |
|
1462 |
__stringVal(ap[3]), __stringVal(ap[4])); |
|
1463 |
__END_INTERRUPTABLE__ |
|
1464 |
break; |
|
1465 |
case 6: |
|
1466 |
__BEGIN_INTERRUPTABLE__ |
|
1467 |
f = fopen((char *)__stringVal(pathName), (char *)__stringVal(openmode), |
|
1468 |
__stringVal(ap[0]), __stringVal(ap[1]), __stringVal(ap[2]), |
|
1469 |
__stringVal(ap[3]), __stringVal(ap[4]), __stringVal(ap[5])); |
|
1470 |
__END_INTERRUPTABLE__ |
|
1471 |
break; |
|
1472 |
case 7: |
|
1473 |
__BEGIN_INTERRUPTABLE__ |
|
1474 |
f = fopen((char *)__stringVal(pathName), (char *)__stringVal(openmode), |
|
1475 |
__stringVal(ap[0]), __stringVal(ap[1]), __stringVal(ap[2]), |
|
1476 |
__stringVal(ap[3]), __stringVal(ap[4]), __stringVal(ap[5]), |
|
1477 |
__stringVal(ap[6])); |
|
1478 |
__END_INTERRUPTABLE__ |
|
1479 |
break; |
|
1480 |
case 8: |
|
1481 |
__BEGIN_INTERRUPTABLE__ |
|
1482 |
f = fopen((char *)__stringVal(pathName), (char *)__stringVal(openmode), |
|
1483 |
__stringVal(ap[0]), __stringVal(ap[1]), __stringVal(ap[2]), |
|
1484 |
__stringVal(ap[3]), __stringVal(ap[4]), __stringVal(ap[5]), |
|
1485 |
__stringVal(ap[6]), __stringVal(ap[7])); |
|
1486 |
__END_INTERRUPTABLE__ |
|
1487 |
break; |
|
1488 |
case 9: |
|
1489 |
__BEGIN_INTERRUPTABLE__ |
|
1490 |
f = fopen((char *)__stringVal(pathName), (char *)__stringVal(openmode), |
|
1491 |
__stringVal(ap[0]), __stringVal(ap[1]), __stringVal(ap[2]), |
|
1492 |
__stringVal(ap[3]), __stringVal(ap[4]), __stringVal(ap[5]), |
|
1493 |
__stringVal(ap[6]), __stringVal(ap[7]), __stringVal(ap[8])); |
|
1494 |
__END_INTERRUPTABLE__ |
|
1495 |
break; |
|
1496 |
case 10: |
|
1497 |
__BEGIN_INTERRUPTABLE__ |
|
1498 |
f = fopen((char *)__stringVal(pathName), (char *)__stringVal(openmode), |
|
1499 |
__stringVal(ap[0]), __stringVal(ap[1]), __stringVal(ap[2]), |
|
1500 |
__stringVal(ap[3]), __stringVal(ap[4]), __stringVal(ap[5]), |
|
1501 |
__stringVal(ap[6]), __stringVal(ap[7]), __stringVal(ap[8]), |
|
1502 |
__stringVal(ap[9])); |
|
1503 |
__END_INTERRUPTABLE__ |
|
1504 |
break; |
|
1505 |
default: |
|
1506 |
__threadErrno = E2BIG; /* too many args */ |
|
1507 |
goto getOutOfHere; |
|
1508 |
} |
|
1509 |
} else if (attributeSpec != nil) { |
|
1510 |
__threadErrno = EINVAL; /* invalid argument */ |
|
1511 |
goto getOutOfHere; |
|
1512 |
} else { |
|
1513 |
/* |
|
1514 |
* create file as sequential streamLF by default. |
|
1515 |
*/ |
|
1516 |
__BEGIN_INTERRUPTABLE__ |
|
1517 |
f = fopen((char *)__stringVal(pathName), (char *)__stringVal(openmode), "rfm=stmlf"); |
|
1518 |
__END_INTERRUPTABLE__ |
|
1519 |
} |
|
1520 |
} |
|
1521 |
} while ((f == NULL) && (__threadErrno == EINTR)); |
|
11290 | 1522 |
} |
5439 | 1523 |
#else /* not VMS */ |
1524 |
||
19860
324edacff5cc
unified cpu and os defines;
Claus Gittinger <cg@exept.de>
parents:
19638
diff
changeset
|
1525 |
# ifdef __win32__ |
11812
fae9f7c22dbd
Windows: user CreateFile() instead of fopen() to avoid wrong data to be read
Stefan Vogel <sv@exept.de>
parents:
11454
diff
changeset
|
1526 |
{ |
24102 | 1527 |
DWORD share = 0, access = 0, create = 0, attr = 0; |
1528 |
char * __openmode; |
|
1529 |
HANDLE handle; |
|
1530 |
SECURITY_ATTRIBUTES sa; |
|
11290 | 1531 |
|
24102 | 1532 |
if (__isStringLike(openmode)) { |
1533 |
share = FILE_SHARE_READ|FILE_SHARE_WRITE; |
|
1534 |
__openmode = __stringVal(openmode); |
|
1535 |
if (strcmp(__openmode, "rb") == 0) { |
|
1536 |
access = GENERIC_READ; |
|
1537 |
create = OPEN_EXISTING; |
|
1538 |
} else if (strcmp(__openmode, "rb+") == 0) { |
|
1539 |
access = GENERIC_READ | GENERIC_WRITE; |
|
1540 |
create = OPEN_EXISTING; |
|
1541 |
} else if (strcmp(__openmode, "wb") == 0) { |
|
1542 |
access = GENERIC_WRITE; |
|
1543 |
create = CREATE_ALWAYS; |
|
1544 |
} else if (strcmp(__openmode, "wb+") == 0) { |
|
1545 |
access = GENERIC_READ | GENERIC_WRITE; |
|
1546 |
create = CREATE_ALWAYS; |
|
1547 |
} else if (strcmp(__openmode, "ab") == 0) { |
|
1548 |
access = FILE_APPEND_DATA|FILE_WRITE_ATTRIBUTES|FILE_WRITE_EA| |
|
1549 |
STANDARD_RIGHTS_WRITE|SYNCHRONIZE; |
|
1550 |
create = OPEN_ALWAYS; |
|
1551 |
} else if (strcmp(__openmode, "ab+") == 0) { |
|
1552 |
access = GENERIC_READ |FILE_APPEND_DATA|FILE_WRITE_ATTRIBUTES|FILE_WRITE_EA| |
|
1553 |
STANDARD_RIGHTS_WRITE|SYNCHRONIZE; |
|
1554 |
create = OPEN_ALWAYS; |
|
1555 |
} else { |
|
1556 |
console_fprintf(stderr, "Win32OS [warning]: unsupported open mode\n"); |
|
1557 |
} |
|
1558 |
} else if (__isArrayLike(openmode)) { |
|
1559 |
OBJ *ap = __arrayVal(openmode); |
|
1560 |
int numAttrib = __arraySize(openmode); |
|
1561 |
int i; |
|
15462 | 1562 |
|
24102 | 1563 |
__openmode = "rb+"; |
11812
fae9f7c22dbd
Windows: user CreateFile() instead of fopen() to avoid wrong data to be read
Stefan Vogel <sv@exept.de>
parents:
11454
diff
changeset
|
1564 |
|
24102 | 1565 |
for (i=0; i<numAttrib; i++) { |
1566 |
OBJ attrSym = ap[i]; |
|
11812
fae9f7c22dbd
Windows: user CreateFile() instead of fopen() to avoid wrong data to be read
Stefan Vogel <sv@exept.de>
parents:
11454
diff
changeset
|
1567 |
|
24102 | 1568 |
if (attrSym == @symbol(FILE_SHARE_READ)) { |
1569 |
share |= FILE_SHARE_READ; |
|
1570 |
} else if (attrSym == @symbol(FILE_SHARE_WRITE)) { |
|
1571 |
share |= FILE_SHARE_WRITE; |
|
11812
fae9f7c22dbd
Windows: user CreateFile() instead of fopen() to avoid wrong data to be read
Stefan Vogel <sv@exept.de>
parents:
11454
diff
changeset
|
1572 |
|
24102 | 1573 |
} else if (attrSym == @symbol(GENERIC_READ)) { |
1574 |
access |= GENERIC_READ; |
|
1575 |
} else if (attrSym == @symbol(GENERIC_WRITE)) { |
|
1576 |
access |= GENERIC_WRITE; |
|
1577 |
} else if (attrSym == @symbol(GENERIC_READ_WRITE)) { |
|
1578 |
access |= GENERIC_READ|GENERIC_WRITE; |
|
11812
fae9f7c22dbd
Windows: user CreateFile() instead of fopen() to avoid wrong data to be read
Stefan Vogel <sv@exept.de>
parents:
11454
diff
changeset
|
1579 |
|
24102 | 1580 |
} else if (attrSym == @symbol(CREATE_NEW)) { |
1581 |
create |= CREATE_NEW; |
|
1582 |
} else if (attrSym == @symbol(CREATE_ALWAYS)) { |
|
1583 |
create |= CREATE_ALWAYS; |
|
1584 |
} else if (attrSym == @symbol(OPEN_EXISTING)) { |
|
1585 |
create |= OPEN_EXISTING; |
|
1586 |
} else if (attrSym == @symbol(OPEN_ALWAYS)) { |
|
1587 |
create |= OPEN_ALWAYS; |
|
1588 |
} else if (attrSym == @symbol(TRUNCATE_EXISTING)) { |
|
1589 |
create |= TRUNCATE_EXISTING; |
|
5439 | 1590 |
|
24102 | 1591 |
} else if (attrSym == @symbol(FILE_ATTRIBUTE_HIDDEN)) { |
1592 |
attr |= FILE_ATTRIBUTE_HIDDEN; |
|
1593 |
} else if (attrSym == @symbol(FILE_ATTRIBUTE_READONLY)) { |
|
1594 |
attr |= FILE_ATTRIBUTE_READONLY; |
|
1595 |
} else if (attrSym == @symbol(FILE_FLAG_WRITE_THROUGH)) { |
|
1596 |
attr |= FILE_FLAG_WRITE_THROUGH; |
|
1597 |
} else if (attrSym == @symbol(FILE_FLAG_SEQUENTIAL_SCAN)) { |
|
1598 |
attr |= FILE_FLAG_SEQUENTIAL_SCAN; |
|
1599 |
} else if (attrSym == @symbol(FILE_FLAG_DELETE_ON_CLOSE)) { |
|
1600 |
attr |= FILE_FLAG_DELETE_ON_CLOSE; |
|
1601 |
} else if (!__isSymbol(attrSym) && __isStringLike(attrSym)) { |
|
1602 |
__openmode = __stringVal(attrSym); |
|
1603 |
} else { |
|
1604 |
console_fprintf(stderr, "Win32OS [warning]: unsupported open mode\n"); |
|
1605 |
} |
|
1606 |
} |
|
1607 |
} |
|
1608 |
if (create == 0) { |
|
11812
fae9f7c22dbd
Windows: user CreateFile() instead of fopen() to avoid wrong data to be read
Stefan Vogel <sv@exept.de>
parents:
11454
diff
changeset
|
1609 |
// argumentError = @symbol(missingCreateMode); |
24102 | 1610 |
goto badArgument; |
1611 |
} |
|
1612 |
if (attr == 0) { |
|
1613 |
attr = FILE_ATTRIBUTE_NORMAL; |
|
1614 |
} |
|
11812
fae9f7c22dbd
Windows: user CreateFile() instead of fopen() to avoid wrong data to be read
Stefan Vogel <sv@exept.de>
parents:
11454
diff
changeset
|
1615 |
|
24102 | 1616 |
/* |
1617 |
* create security attributes - make handle inheritable by subprocesses |
|
1618 |
*/ |
|
1619 |
memset(&sa, 0, sizeof (sa)); |
|
1620 |
sa.nLength = sizeof( sa ); |
|
1621 |
// sa.bInheritHandle = TRUE; |
|
1622 |
sa.bInheritHandle = FALSE; |
|
12630
c6151f8f6854
changed: #openFile:withMode:attributes:
Stefan Vogel <sv@exept.de>
parents:
12467
diff
changeset
|
1623 |
|
24102 | 1624 |
if (__isStringLike(pathName)) { |
1625 |
char _aPathName[MAXPATHLEN]; |
|
11290 | 1626 |
|
24102 | 1627 |
strncpy(_aPathName, __stringVal(pathName), MAXPATHLEN-1); _aPathName[MAXPATHLEN-1] = '\0'; |
1628 |
do { |
|
1629 |
__threadErrno = 0; |
|
1630 |
handle = STX_API_NOINT_CALL7( "CreateFileA", CreateFileA, _aPathName, access, share, &sa, create, attr, 0 /* hTempl */); |
|
1631 |
if (__threadErrno == EINTR) { |
|
1632 |
handle = INVALID_HANDLE_VALUE; |
|
1633 |
} |
|
1634 |
} while ((handle == INVALID_HANDLE_VALUE) && (__threadErrno == EINTR)); |
|
1635 |
} else if (__isUnicode16String(pathName)) { |
|
1636 |
wchar_t _aPathName[MAXPATHLEN+1]; |
|
1637 |
int i, l; |
|
11812
fae9f7c22dbd
Windows: user CreateFile() instead of fopen() to avoid wrong data to be read
Stefan Vogel <sv@exept.de>
parents:
11454
diff
changeset
|
1638 |
|
24102 | 1639 |
l = __unicode16StringSize(pathName); |
1640 |
if (l > MAXPATHLEN) l = MAXPATHLEN; |
|
1641 |
for (i=0; i<l; i++) { |
|
1642 |
_aPathName[i] = __unicode16StringVal(pathName)[i]; |
|
1643 |
} |
|
1644 |
_aPathName[i] = 0; |
|
11812
fae9f7c22dbd
Windows: user CreateFile() instead of fopen() to avoid wrong data to be read
Stefan Vogel <sv@exept.de>
parents:
11454
diff
changeset
|
1645 |
|
24102 | 1646 |
do { |
1647 |
__threadErrno = 0; |
|
1648 |
handle = STX_API_NOINT_CALL7( "CreateFileW", CreateFileW, _aPathName, access, share, &sa, create, attr, 0 /* hTempl */); |
|
1649 |
if (__threadErrno == EINTR) { |
|
1650 |
handle = INVALID_HANDLE_VALUE; |
|
1651 |
} |
|
1652 |
} while ((handle == INVALID_HANDLE_VALUE) && (__threadErrno == EINTR)); |
|
1653 |
} |
|
1654 |
if (handle != INVALID_HANDLE_VALUE) { |
|
1655 |
int fd; |
|
1656 |
extern void __stxWrapApiEnterCritical(), __stxWrapApiLeaveCritical(); |
|
12692
6150a8d7c87e
changed: #openFile:withMode:attributes:
Stefan Vogel <sv@exept.de>
parents:
12687
diff
changeset
|
1657 |
|
24102 | 1658 |
__stxWrapApiEnterCritical(); |
1659 |
fd = _open_osfhandle((long)handle, O_BINARY); |
|
1660 |
if (fd < 0) { |
|
1661 |
if (__threadErrno == 0) { |
|
1662 |
// no more file descriptors |
|
1663 |
__threadErrno = EMFILE; |
|
1664 |
} |
|
1665 |
CloseHandle(handle); |
|
1666 |
} else { |
|
1667 |
f = fdopen(fd, __openmode); |
|
1668 |
} |
|
1669 |
__stxWrapApiLeaveCritical(); |
|
1670 |
} else { |
|
1671 |
__threadErrno = __WIN32_ERR(GetLastError()); |
|
1672 |
} |
|
11290 | 1673 |
} |
5439 | 1674 |
# else /* not WIN32 */ |
1675 |
||
14578 | 1676 |
if (__isStringLike(encodedPathName)) { |
24102 | 1677 |
int accessMode = 0666; // default access mode of fopen(), relies on umask() |
1678 |
int flags = 0; |
|
1679 |
int fd; |
|
1680 |
char * __openmode; |
|
15462 | 1681 |
|
24102 | 1682 |
if (__isStringLike(openmode)) { |
1683 |
__openmode = __stringVal(openmode); |
|
1684 |
if (strcmp(__openmode, "r") == 0) { |
|
1685 |
flags = O_RDONLY; |
|
1686 |
} else if (strcmp(__openmode, "r+") == 0) { |
|
1687 |
flags = O_RDWR; |
|
1688 |
} else if (strcmp(__openmode, "w") == 0) { |
|
1689 |
flags = O_WRONLY | O_CREAT | O_TRUNC; |
|
1690 |
} else if (strcmp(__openmode, "w+") == 0) { |
|
1691 |
flags = O_RDWR | O_CREAT | O_TRUNC; |
|
1692 |
} else if (strcmp(__openmode, "a") == 0) { |
|
1693 |
flags = O_WRONLY | O_CREAT | O_APPEND; |
|
1694 |
} else if (strcmp(__openmode, "a+") == 0) { |
|
1695 |
flags = O_RDWR | O_CREAT| O_APPEND; |
|
1696 |
} else { |
|
1697 |
console_fprintf(stderr, "UNIXOS [warning]: unsupported open mode\n"); |
|
1698 |
} |
|
1699 |
} else if (__isArrayLike(openmode)) { |
|
1700 |
OBJ *ap = __arrayVal(openmode); |
|
1701 |
int numAttrib = __arraySize(openmode); |
|
1702 |
int i; |
|
15462 | 1703 |
|
24102 | 1704 |
__openmode = "r+"; |
15462 | 1705 |
|
24102 | 1706 |
for (i=0; i<numAttrib; i++) { |
1707 |
OBJ attrSym = ap[i]; |
|
15462 | 1708 |
|
24102 | 1709 |
if (attrSym == @symbol(FILE_SHARE_READ)) { |
1710 |
// ignore |
|
1711 |
} else if (attrSym == @symbol(FILE_SHARE_WRITE)) { |
|
1712 |
// ignore |
|
1713 |
} else if (attrSym == @symbol(GENERIC_READ)) { |
|
1714 |
flags |= O_RDONLY; |
|
1715 |
__openmode = "r"; |
|
1716 |
} else if (attrSym == @symbol(GENERIC_WRITE)) { |
|
1717 |
flags |= O_WRONLY; |
|
1718 |
__openmode = "w"; |
|
1719 |
} else if (attrSym == @symbol(GENERIC_READ_WRITE)) { |
|
1720 |
flags |= O_RDWR; |
|
1721 |
__openmode = "r+"; |
|
1722 |
} else if (attrSym == @symbol(CREATE_NEW)) { |
|
1723 |
flags |= O_CREAT|O_EXCL; |
|
1724 |
accessMode = 0600; // simulate mkstemp() |
|
1725 |
} else if (attrSym == @symbol(CREATE_ALWAYS)) { |
|
1726 |
flags |= O_CREAT|O_TRUNC; |
|
1727 |
} else if (attrSym == @symbol(OPEN_EXISTING)) { |
|
1728 |
// nothing to be set |
|
1729 |
} else if (attrSym == @symbol(OPEN_ALWAYS)) { |
|
1730 |
flags |= O_CREAT; |
|
1731 |
} else if (attrSym == @symbol(TRUNCATE_EXISTING)) { |
|
1732 |
flags |= O_TRUNC; |
|
15462 | 1733 |
|
24102 | 1734 |
} else if (attrSym == @symbol(FILE_ATTRIBUTE_HIDDEN)) { |
1735 |
// ignore |
|
1736 |
} else if (attrSym == @symbol(FILE_ATTRIBUTE_READONLY)) { |
|
1737 |
accessMode &= 0444; |
|
1738 |
} else if (attrSym == @symbol(FILE_FLAG_WRITE_THROUGH)) { |
|
15462 | 1739 |
#ifdef O_DIRECT |
24102 | 1740 |
flags |= O_DIRECT; |
15462 | 1741 |
#endif |
24102 | 1742 |
} else if (attrSym == @symbol(FILE_FLAG_SEQUENTIAL_SCAN)) { |
1743 |
// ignore |
|
1744 |
} else if (attrSym == @symbol(FILE_FLAG_DELETE_ON_CLOSE)) { |
|
1745 |
// ignore; |
|
1746 |
} else if (!__isSymbol(attrSym) && __isStringLike(attrSym)) { |
|
1747 |
__openmode = __stringVal(attrSym); |
|
1748 |
} else { |
|
1749 |
console_fprintf(stderr, "UNIXOS [warning]: unsupported open mode\n"); |
|
1750 |
} |
|
1751 |
} |
|
1752 |
} |
|
1753 |
do { |
|
1754 |
__BEGIN_INTERRUPTABLE__ |
|
1755 |
fd = open((char *) __stringVal(encodedPathName), flags, accessMode); |
|
1756 |
__END_INTERRUPTABLE__ |
|
1757 |
} while ((fd < 0) && (__threadErrno == EINTR)); |
|
15462 | 1758 |
|
24102 | 1759 |
if (fd >= 0) { |
1760 |
__threadErrno = 0; |
|
1761 |
f = fdopen(fd, __openmode); |
|
1762 |
if (f == NULL) { |
|
1763 |
close(fd); // fdopen failed, close before retry. |
|
1764 |
} |
|
1765 |
} |
|
11290 | 1766 |
} |
15462 | 1767 |
|
5439 | 1768 |
# endif /* not WIN32 */ |
1769 |
#endif /* not VMS */ |
|
1770 |
||
15462 | 1771 |
|
11290 | 1772 |
if (f == NULL) { |
24102 | 1773 |
/* |
1774 |
* If no filedescriptors available, try to finalize |
|
1775 |
* possibly collected fd's and try again. |
|
1776 |
*/ |
|
1777 |
if ((__threadErrno == ENFILE || __threadErrno == EMFILE) && pass == 0) { |
|
1778 |
pass = 1; |
|
1779 |
__SSEND0(@global(ObjectMemory), @symbol(scavenge), 0); |
|
1780 |
__SSEND0(@global(ObjectMemory), @symbol(finalize), 0); |
|
1781 |
goto retry; |
|
1782 |
} |
|
11812
fae9f7c22dbd
Windows: user CreateFile() instead of fopen() to avoid wrong data to be read
Stefan Vogel <sv@exept.de>
parents:
11454
diff
changeset
|
1783 |
badArgument: |
fae9f7c22dbd
Windows: user CreateFile() instead of fopen() to avoid wrong data to be read
Stefan Vogel <sv@exept.de>
parents:
11454
diff
changeset
|
1784 |
getOutOfHere: |
23539 | 1785 |
# ifdef __win32__ |
24102 | 1786 |
__threadErrno = __WIN32_ERR(GetLastError()); |
23539 | 1787 |
# endif |
24102 | 1788 |
error = __mkSmallInteger(__threadErrno); |
1789 |
__INST(position) = nil; |
|
11290 | 1790 |
} else { |
5439 | 1791 |
#ifdef __VMS__ |
24102 | 1792 |
/* |
1793 |
* check to see if this is positionable ... |
|
1794 |
*/ |
|
1795 |
__INST(canPosition) = false; |
|
5439 | 1796 |
# ifndef _POSIX_C_SOURCE |
24102 | 1797 |
{ |
1798 |
struct stat statBuffer; |
|
5439 | 1799 |
|
24102 | 1800 |
if (fstat(fileno(f), &statBuffer) >= 0) { |
1801 |
switch (statBuffer.st_fab_rfm) { |
|
1802 |
case FAB$C_UDF: /* undefined (also stream binary) */ |
|
1803 |
case FAB$C_VAR: /* variable length records */ |
|
1804 |
case FAB$C_VFC: /* variable fixed control */ |
|
1805 |
case FAB$C_STM: /* RMS-11 stream (valid only for sequen) */ |
|
1806 |
default: |
|
1807 |
__INST(canPosition) = false; |
|
1808 |
break; |
|
5439 | 1809 |
|
24102 | 1810 |
case FAB$C_FIX: /* fixed length records */ |
1811 |
case FAB$C_STMLF: /* LF stream (valid only for sequential) */ |
|
1812 |
case FAB$C_STMCR: /* CR stream (valid only for sequential) */ |
|
1813 |
__INST(canPosition) = true; |
|
1814 |
break; |
|
1815 |
} |
|
1816 |
} |
|
1817 |
} |
|
8627
5a86b95bebbb
gcc3.3.1 insn-scheduling bug workaround
Claus Gittinger <cg@exept.de>
parents:
8604
diff
changeset
|
1818 |
# endif |
5439 | 1819 |
#else /* not VMS */ |
24102 | 1820 |
__INST(canPosition) = true; |
11812
fae9f7c22dbd
Windows: user CreateFile() instead of fopen() to avoid wrong data to be read
Stefan Vogel <sv@exept.de>
parents:
11454
diff
changeset
|
1821 |
#endif /* not VMS */ |
5439 | 1822 |
|
24102 | 1823 |
if (@global(FileOpenTrace) == true) { |
1824 |
console_fprintf(stderr, "fopen %s [FileStream] -> %"_lx_"\n", __stringVal(pathName), (INT)f); |
|
1825 |
} |
|
1826 |
if (f != NULL) { |
|
1827 |
OBJ fp; |
|
8627
5a86b95bebbb
gcc3.3.1 insn-scheduling bug workaround
Claus Gittinger <cg@exept.de>
parents:
8604
diff
changeset
|
1828 |
|
24102 | 1829 |
wasBlocked = __BLOCKINTERRUPTS(); |
8627
5a86b95bebbb
gcc3.3.1 insn-scheduling bug workaround
Claus Gittinger <cg@exept.de>
parents:
8604
diff
changeset
|
1830 |
#if 0 |
24102 | 1831 |
// The original code was: |
1832 |
__INST(handle) = fp = __MKEXTERNALADDRESS(f); __STORE(self, fp); |
|
1833 |
// but for that, gcc generates wrong code, which loads self (volatile) into |
|
1834 |
// a register (bp), then calls __MKEXTERNALADDRESS, then stores indirect bp. |
|
1835 |
// That is wrong if a scavenge occurs in __MKEXTERNALADDRESS, as bp is now still pointing to the old |
|
1836 |
// object. |
|
8627
5a86b95bebbb
gcc3.3.1 insn-scheduling bug workaround
Claus Gittinger <cg@exept.de>
parents:
8604
diff
changeset
|
1837 |
#else |
24102 | 1838 |
fp = __MKEXTERNALADDRESS(f); |
1839 |
__INST(handle) = fp; |
|
1840 |
__STORE(self, fp); |
|
8627
5a86b95bebbb
gcc3.3.1 insn-scheduling bug workaround
Claus Gittinger <cg@exept.de>
parents:
8604
diff
changeset
|
1841 |
#endif |
24102 | 1842 |
} |
5439 | 1843 |
} |
1844 |
%}. |
|
16298 | 1845 |
error notNil ifTrue:[ |
24102 | 1846 |
lastErrorNumber := error. |
1847 |
^ self openError:error. |
|
8034
716b32d832bf
raise condition interrupt: interrupt between creation and lobby
ca
parents:
7970
diff
changeset
|
1848 |
]. |
16298 | 1849 |
handle isNil ifTrue:[ |
24102 | 1850 |
^ self openError:0. |
16298 | 1851 |
]. |
1852 |
||
20096 | 1853 |
buffered := true. "default is buffered" |
16298 | 1854 |
position := 0. |
1855 |
handleType := #filePointer. |
|
18781 | 1856 |
self registerForFinalization. |
16298 | 1857 |
wasBlocked == false ifTrue:[OperatingSystem unblockInterrupts]. |
12395
1db7ea277c93
filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents:
12207
diff
changeset
|
1858 |
^ handle |
23539 | 1859 |
|
1860 |
"Modified: / 22-11-2018 / 14:53:51 / Stefan Vogel" |
|
5439 | 1861 |
! |
1862 |
||
613 | 1863 |
openForAppending |
1864 |
"open the file for writeonly appending to the end. |
|
20921 | 1865 |
If the file does not exist, raise OpenError; |
613 | 1866 |
otherwise return the receiver." |
1867 |
||
1868 |
mode := #writeonly. |
|
1869 |
didWrite := true. |
|
2921 | 1870 |
^ self openWithMode:AppendMode |
613 | 1871 |
! |
1872 |
||
1873 |
openForReadWrite |
|
1874 |
"open the file for read/write. |
|
20921 | 1875 |
If the file does not exist, raise OpenError; |
613 | 1876 |
otherwise return the receiver." |
1877 |
||
1878 |
mode := #readwrite. |
|
2921 | 1879 |
^ self openWithMode:ReadWriteMode |
613 | 1880 |
! |
1881 |
||
1882 |
openForReading |
|
1883 |
"open the file for readonly. |
|
20921 | 1884 |
If the file does not exist, raise OpenError; |
613 | 1885 |
otherwise return the receiver." |
1886 |
||
1887 |
mode := #readonly. |
|
1888 |
didWrite := false. |
|
2921 | 1889 |
^ self openWithMode:ReadMode |
613 | 1890 |
! |
1891 |
||
1892 |
openForWriting |
|
8604 | 1893 |
"open the file writeonly. The contents of the file is preserved. |
20921 | 1894 |
If the file does not exist, raise OpenError; |
613 | 1895 |
otherwise return the receiver." |
1896 |
||
1897 |
mode := #writeonly. |
|
1898 |
didWrite := true. |
|
8604 | 1899 |
"we must not truncate the file!! So do not use WriteMode" |
1900 |
^ self openWithMode:ReadWriteMode |
|
613 | 1901 |
! |
1902 |
||
1 | 1903 |
openWithMode:openmode |
2921 | 1904 |
"open the file; |
1905 |
openmode is the string defining the way to open as defined by the stdio library |
|
1906 |
(i.e. the 2nd fopen argument). |
|
1907 |
||
1908 |
This is a private entry, but maybe useful to open a file in a special mode, |
|
1909 |
which is proprietrary to the operatingSystem." |
|
8627
5a86b95bebbb
gcc3.3.1 insn-scheduling bug workaround
Claus Gittinger <cg@exept.de>
parents:
8604
diff
changeset
|
1910 |
|
2921 | 1911 |
^ self openWithMode:openmode attributes:nil |
1912 |
! |
|
1913 |
||
1914 |
openWithMode:openmode attributes:attributeSpec |
|
8627
5a86b95bebbb
gcc3.3.1 insn-scheduling bug workaround
Claus Gittinger <cg@exept.de>
parents:
8604
diff
changeset
|
1915 |
"open the file; |
2921 | 1916 |
openmode is the string defining the way to open as defined by the stdio library |
1917 |
(i.e. the 2nd fopen argument). |
|
1918 |
||
1919 |
attributeSpec is an additional argument, only used with VMS - it allows a file to |
|
1920 |
be created as fixedRecord, variableRecord, streamLF, streamCR, ... |
|
1921 |
In VMS, if nonNil, it must consist of an array of strings (max:10), giving additional |
|
1922 |
attributes (see fopen description). |
|
1923 |
Passing a nil specifies the default format (streamLF) - ST/X always invokes this with nil. |
|
1924 |
This argument is ignored in UNIX & MSDOS systems. |
|
1925 |
||
1926 |
This is a private entry, but maybe useful to open/create a file in a special mode, |
|
1927 |
which is proprietrary to the operatingSystem." |
|
1 | 1928 |
|
12395
1db7ea277c93
filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents:
12207
diff
changeset
|
1929 |
handle notNil ifTrue:[^ self errorAlreadyOpen]. |
2921 | 1930 |
|
12395
1db7ea277c93
filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents:
12207
diff
changeset
|
1931 |
handle := self openFile:pathName withMode:openmode attributes:attributeSpec. |
17144 | 1932 |
handle isNil ifTrue:[ |
21623 | 1933 |
"this happens, if proceeding from an OpenError raised in #openFile..." |
1934 |
^ nil. |
|
8604 | 1935 |
]. |
1 | 1936 |
! |
1937 |
||
613 | 1938 |
pathName:filename |
1939 |
"set the pathname" |
|
1 | 1940 |
|
3051
29d10c26d6a3
allow for fileNames to be passed to #pathName:
Claus Gittinger <cg@exept.de>
parents:
3044
diff
changeset
|
1941 |
pathName := filename asString |
29d10c26d6a3
allow for fileNames to be passed to #pathName:
Claus Gittinger <cg@exept.de>
parents:
3044
diff
changeset
|
1942 |
|
29d10c26d6a3
allow for fileNames to be passed to #pathName:
Claus Gittinger <cg@exept.de>
parents:
3044
diff
changeset
|
1943 |
"Modified: / 28.10.1997 / 14:29:01 / cg" |
1 | 1944 |
! |
1945 |
||
613 | 1946 |
pathName:filename in:aDirectory |
1947 |
"set the pathname starting at aDirectory, a FileDirectory" |
|
77 | 1948 |
|
3051
29d10c26d6a3
allow for fileNames to be passed to #pathName:
Claus Gittinger <cg@exept.de>
parents:
3044
diff
changeset
|
1949 |
pathName := aDirectory asFilename constructString:filename |
29d10c26d6a3
allow for fileNames to be passed to #pathName:
Claus Gittinger <cg@exept.de>
parents:
3044
diff
changeset
|
1950 |
|
29d10c26d6a3
allow for fileNames to be passed to #pathName:
Claus Gittinger <cg@exept.de>
parents:
3044
diff
changeset
|
1951 |
"Modified: / 28.10.1997 / 14:28:54 / cg" |
1 | 1952 |
! |
1953 |
||
1954 |
reOpen |
|
8604 | 1955 |
"USERS WILL NEVER INVOKE THIS METHOD |
1956 |
sent after snapin to reopen streams." |
|
1 | 1957 |
|
12395
1db7ea277c93
filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents:
12207
diff
changeset
|
1958 |
handle notNil ifTrue:[ |
24380 | 1959 |
"it was open, when snapped-out" |
1960 |
handle := nil. |
|
1961 |
self unregisterForFinalization. |
|
1962 |
pathName isNil ifTrue:[ |
|
1963 |
^ self. |
|
1964 |
]. |
|
1965 |
[ |
|
1966 |
|oldPos| |
|
173 | 1967 |
|
24380 | 1968 |
"should take care of appending files and open them for |
1969 |
append / position them to the end" |
|
8604 | 1970 |
|
24380 | 1971 |
oldPos := position. |
8604 | 1972 |
|
24380 | 1973 |
mode == #readonly ifTrue: [ |
1974 |
self openForReading |
|
1975 |
] ifFalse:[mode == #writeonly ifTrue: [ |
|
1976 |
self openForWriting. |
|
1977 |
] ifFalse:[ |
|
1978 |
self openForReadWrite. |
|
1979 |
]]. |
|
8604 | 1980 |
|
24380 | 1981 |
oldPos notNil ifTrue:[ |
1982 |
self position:oldPos. |
|
1983 |
] |
|
1984 |
] on:OpenError do:[:ex| |
|
1985 |
"this happens, if after a restart, |
|
1986 |
the file is no longer present or accessible ..." |
|
13 | 1987 |
|
24380 | 1988 |
(self className , ' [warning]: could not reopen file: ', pathName) errorPrintCR. |
1989 |
]. |
|
1 | 1990 |
] |
1088 | 1991 |
|
24380 | 1992 |
"Modified: / 10-01-1997 / 17:50:51 / cg" |
1993 |
"Modified: / 28-06-2019 / 08:43:38 / Claus Gittinger" |
|
2921 | 1994 |
! |
1995 |
||
1996 |
setMode:aModeSymbol |
|
1997 |
mode := aModeSymbol |
|
15479 | 1998 |
! |
1999 |
||
2000 |
setPathName:pathNameString removeOnClose:aBoolean |
|
2001 |
pathName := pathNameString. |
|
2002 |
removeOnClose := aBoolean. |
|
31 | 2003 |
! ! |
2004 |
||
10395
62fd9eba5c4b
Move fileIn methods refering to filenames from PeekableStream
Stefan Vogel <sv@exept.de>
parents:
10354
diff
changeset
|
2005 |
!FileStream methodsFor:'private fileIn'! |
62fd9eba5c4b
Move fileIn methods refering to filenames from PeekableStream
Stefan Vogel <sv@exept.de>
parents:
10354
diff
changeset
|
2006 |
|
62fd9eba5c4b
Move fileIn methods refering to filenames from PeekableStream
Stefan Vogel <sv@exept.de>
parents:
10354
diff
changeset
|
2007 |
fileInNotifying:notifiedLoader passChunk:passChunk |
62fd9eba5c4b
Move fileIn methods refering to filenames from PeekableStream
Stefan Vogel <sv@exept.de>
parents:
10354
diff
changeset
|
2008 |
"central method to file in from the receiver, i.e. read chunks and evaluate them - |
62fd9eba5c4b
Move fileIn methods refering to filenames from PeekableStream
Stefan Vogel <sv@exept.de>
parents:
10354
diff
changeset
|
2009 |
return the value of the last chunk. |
62fd9eba5c4b
Move fileIn methods refering to filenames from PeekableStream
Stefan Vogel <sv@exept.de>
parents:
10354
diff
changeset
|
2010 |
Someone (which is usually some codeView) is notified of errors." |
62fd9eba5c4b
Move fileIn methods refering to filenames from PeekableStream
Stefan Vogel <sv@exept.de>
parents:
10354
diff
changeset
|
2011 |
|
21459 | 2012 |
^ self fileInNotifying:notifiedLoader passChunk:passChunk inDirectory:(self fileName directory). |
2013 |
||
2014 |
"Modified: / 16-02-2017 / 11:28:21 / stefan" |
|
10395
62fd9eba5c4b
Move fileIn methods refering to filenames from PeekableStream
Stefan Vogel <sv@exept.de>
parents:
10354
diff
changeset
|
2015 |
! ! |
62fd9eba5c4b
Move fileIn methods refering to filenames from PeekableStream
Stefan Vogel <sv@exept.de>
parents:
10354
diff
changeset
|
2016 |
|
2921 | 2017 |
!FileStream methodsFor:'queries'! |
613 | 2018 |
|
17603 | 2019 |
collectionSize |
2020 |
"common stream protocol: return the size of the stream; |
|
20412 | 2021 |
that's the number of bytes of the file." |
17603 | 2022 |
|
2023 |
^ self fileSize. |
|
2024 |
! |
|
2025 |
||
2903 | 2026 |
fileSize |
613 | 2027 |
"return the size in bytes of the file" |
2028 |
||
16298 | 2029 |
|error| |
3971 | 2030 |
%{ |
4910
33b0edbb1920
merged back FileStream_win32 into FileStream
Claus Gittinger <cg@exept.de>
parents:
4907
diff
changeset
|
2031 |
HFILE f; |
33b0edbb1920
merged back FileStream_win32 into FileStream
Claus Gittinger <cg@exept.de>
parents:
4907
diff
changeset
|
2032 |
|
613 | 2033 |
#ifdef transputer |
7874
5e0319ca5e60
stats filesize can be up-to 32bit now.
Claus Gittinger <cg@exept.de>
parents:
7872
diff
changeset
|
2034 |
unsigned int size; |
613 | 2035 |
|
12395
1db7ea277c93
filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents:
12207
diff
changeset
|
2036 |
if (__INST(handle) != nil) { |
24102 | 2037 |
f = __FILEVal(__INST(handle)); |
2038 |
if ((size = filesize(fileno(f))) >= 0) { |
|
2039 |
RETURN ( __MKUINT(size) ); |
|
2040 |
} |
|
613 | 2041 |
} |
2042 |
#else |
|
2043 |
struct stat buf; |
|
2044 |
int ret; |
|
2045 |
int fd; |
|
2046 |
||
12395
1db7ea277c93
filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents:
12207
diff
changeset
|
2047 |
if (__INST(handle) != nil) { |
24102 | 2048 |
f = __FILEVal(__INST(handle)); |
2049 |
fd = fileno(f); |
|
2050 |
do { |
|
2051 |
ret = fstat(fd, &buf); |
|
2052 |
} while ((ret < 0) && (__threadErrno == EINTR)); |
|
2053 |
if (ret >= 0) { |
|
2054 |
OBJ rslt; |
|
7883
1039acd0624b
Large file support (off_t with 64 bits) for UNIX
Stefan Vogel <sv@exept.de>
parents:
7874
diff
changeset
|
2055 |
|
24102 | 2056 |
if (sizeof(buf.st_size) == 8) { |
2057 |
rslt = __MKINT64(&buf.st_size); |
|
2058 |
} else { |
|
2059 |
rslt = __MKINT(buf.st_size); |
|
2060 |
} |
|
2061 |
RETURN(rslt); |
|
2062 |
} |
|
23539 | 2063 |
# ifdef __win32__ |
24102 | 2064 |
__threadErrno = __WIN32_ERR(GetLastError()); |
23539 | 2065 |
# endif |
24102 | 2066 |
error = __mkSmallInteger(__threadErrno); |
613 | 2067 |
} |
2068 |
#endif |
|
2069 |
%}. |
|
2070 |
||
2071 |
"could add a fall-back here: |
|
2072 |
||
24102 | 2073 |
oldPosition := self position. |
2074 |
self setToEnd. |
|
2075 |
sz := self position. |
|
2076 |
self position:oldPosition. |
|
2077 |
^ sz |
|
613 | 2078 |
" |
16298 | 2079 |
error notNil ifTrue:[ |
24102 | 2080 |
lastErrorNumber := error. |
2081 |
self ioError:error. |
|
2082 |
^ self. |
|
16298 | 2083 |
]. |
12395
1db7ea277c93
filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents:
12207
diff
changeset
|
2084 |
handle isNil ifTrue:[^ self errorNotOpen]. |
613 | 2085 |
^ self primitiveFailed |
23539 | 2086 |
|
2087 |
"Modified: / 22-11-2018 / 14:53:12 / Stefan Vogel" |
|
2936
5cce0f2166d9
OpenErrorSignal already defined in super (ExternalStream).
Stefan Vogel <sv@exept.de>
parents:
2921
diff
changeset
|
2088 |
! |
5cce0f2166d9
OpenErrorSignal already defined in super (ExternalStream).
Stefan Vogel <sv@exept.de>
parents:
2921
diff
changeset
|
2089 |
|
17603 | 2090 |
remainingSize |
24839 | 2091 |
"return the number of remaining elements to read in the streamed collection." |
17603 | 2092 |
|
2093 |
^ self fileSize - self position. |
|
24839 | 2094 |
|
2095 |
"Modified (comment): / 15-10-2019 / 18:30:40 / Stefan Vogel" |
|
17603 | 2096 |
! |
2097 |
||
2936
5cce0f2166d9
OpenErrorSignal already defined in super (ExternalStream).
Stefan Vogel <sv@exept.de>
parents:
2921
diff
changeset
|
2098 |
size |
11454 | 2099 |
"common stream protocol: return the size of the stream; |
20412 | 2100 |
that's the number of bytes of the file." |
2998
930360fb3f12
#size is not really obsolete
Claus Gittinger <cg@exept.de>
parents:
2992
diff
changeset
|
2101 |
|
2936
5cce0f2166d9
OpenErrorSignal already defined in super (ExternalStream).
Stefan Vogel <sv@exept.de>
parents:
2921
diff
changeset
|
2102 |
^ self fileSize. |
2998
930360fb3f12
#size is not really obsolete
Claus Gittinger <cg@exept.de>
parents:
2992
diff
changeset
|
2103 |
|
930360fb3f12
#size is not really obsolete
Claus Gittinger <cg@exept.de>
parents:
2992
diff
changeset
|
2104 |
"Modified: 4.10.1997 / 18:01:09 / cg" |
1 | 2105 |
! ! |
2106 |
||
5493 | 2107 |
!FileStream methodsFor:'rel5 protocol'! |
2108 |
||
12395
1db7ea277c93
filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents:
12207
diff
changeset
|
2109 |
positionFile:handle position:newPos |
5493 | 2110 |
"for migration to rel5 only" |
2111 |
||
2112 |
self primitiveFailed |
|
2113 |
! ! |
|
2114 |
||
25371 | 2115 |
!FileStream methodsFor:'stream-to-stream copy'! |
2116 |
||
2117 |
copy:numberOfBytesOrNil into:outStream bufferSize:bufferSize |
|
2118 |
"read from the receiver, and write numberOfBytes data to another aWriteStream. |
|
2119 |
Return the number of bytes which have been transferred. |
|
2120 |
If nuberOfBytesOrNil is nil, copy until the end of myself. |
|
2121 |
Redefined to use operating system features to do a fast copy." |
|
2122 |
||
2123 |
|nToWrite nWritten fromFd toFd| |
|
2124 |
||
2125 |
nToWrite := numberOfBytesOrNil. |
|
2126 |
outStream isExternalStream ifTrue:[ |
|
2127 |
"/ try optimized system call |
|
2128 |
fromFd := self fileHandle. |
|
2129 |
fromFd isNil ifTrue:[ |
|
2130 |
self errorNotOpen. |
|
2131 |
^ 0. |
|
2132 |
]. |
|
2133 |
toFd := outStream fileHandle. |
|
2134 |
toFd isNil ifTrue:[ |
|
2135 |
outStream errorNotOpen. |
|
2136 |
^ 0. |
|
2137 |
]. |
|
2138 |
||
2139 |
nToWrite isNil ifTrue:[ |
|
2140 |
"/ write to end |
|
2141 |
nToWrite := self fileSize - self position. |
|
2142 |
]. |
|
2143 |
||
2144 |
"sendfile() in Linux does not support any combination of file/pipe/socket fds. |
|
2145 |
#copyFromFd:toFd:startIndex:count: is currently not available in windows. |
|
2146 |
Both will return 0 and fall through to super." |
|
2147 |
||
2148 |
nWritten := OperatingSystem |
|
2149 |
copyFromFd:fromFd |
|
2150 |
toFd:toFd |
|
2151 |
startIndex:nil "/ nil -> start at the current position of fromFd |
|
2152 |
count:nToWrite. |
|
2153 |
nWritten > 0 ifTrue:[ |
|
2154 |
"/ sendfile() changes the position in fromFd and toFd. |
|
2155 |
"/ sending #position fixes the position instVar as a side effect. |
|
2156 |
self position. |
|
2157 |
outStream isPositionable ifTrue:[ |
|
2158 |
outStream position. |
|
2159 |
]. |
|
2160 |
||
2161 |
nWritten == nToWrite ifTrue:[ |
|
2162 |
"we are finished, all has been written" |
|
2163 |
^ nToWrite |
|
2164 |
]. |
|
2165 |
"some data has not been written. |
|
2166 |
Maybe an error occured. We will get the error whe we #copy the rest below" |
|
2167 |
nToWrite := nToWrite - nWritten. |
|
2168 |
^ nWritten + (super copy:nToWrite into:outStream bufferSize:bufferSize). |
|
2169 |
]. |
|
2170 |
]. |
|
2171 |
||
2172 |
"fall back..." |
|
2173 |
^ super copy:nToWrite into:outStream bufferSize:bufferSize. |
|
2174 |
||
2175 |
" |
|
2176 |
|in out| |
|
2177 |
||
2178 |
in := 'Make.proto' asFilename readStream. |
|
2179 |
in copyToEndInto:Stdout. |
|
2180 |
in close. |
|
2181 |
" |
|
2182 |
||
2183 |
" |
|
2184 |
|in out| |
|
2185 |
||
2186 |
in := 'Make.proto' asFilename readStream. |
|
2187 |
out := '/tmp/test' asFilename newReadWriteStream. |
|
2188 |
in copyToEndInto:out. |
|
2189 |
self assert:(in fileSize = out fileSize). |
|
2190 |
self assert:(in position = in fileSize). |
|
2191 |
self assert:(in atEnd). |
|
2192 |
self assert:(in position = out position). |
|
2193 |
in reset. out reset. |
|
2194 |
self assert:(in contents = out contents). |
|
2195 |
in close. out close. |
|
2196 |
||
2197 |
'/tmp/test' asFilename contents. |
|
2198 |
" |
|
2199 |
||
2200 |
"Created: / 13-03-2019 / 16:27:04 / Stefan Vogel" |
|
2201 |
"Modified (format): / 25-05-2019 / 16:47:53 / Claus Gittinger" |
|
2202 |
"Modified (comment): / 28-04-2020 / 11:59:00 / Stefan Vogel" |
|
2203 |
! ! |
|
2204 |
||
2 | 2205 |
!FileStream methodsFor:'testing'! |
2206 |
||
25371 | 2207 |
isDirectory |
2208 |
^ OperatingSystem isDirectory:pathName |
|
2209 |
! |
|
2210 |
||
2211 |
isEmpty |
|
2212 |
"common stream protocol: are there no bytes in the file?" |
|
2213 |
||
2214 |
^ self fileSize == 0. |
|
2215 |
! |
|
2216 |
||
2 | 2217 |
isFileStream |
2218 |
"return true, if the receiver is some kind of fileStream. |
|
2219 |
redefined from Object" |
|
2220 |
||
2221 |
^ true |
|
2222 |
! ! |
|
2223 |
||
1088 | 2224 |
!FileStream class methodsFor:'documentation'! |
2225 |
||
2226 |
version |
|
18651 | 2227 |
^ '$Header$' |
12207
cb911af5184f
comment in: #removeOnClose:
Claus Gittinger <cg@exept.de>
parents:
11813
diff
changeset
|
2228 |
! |
cb911af5184f
comment in: #removeOnClose:
Claus Gittinger <cg@exept.de>
parents:
11813
diff
changeset
|
2229 |
|
cb911af5184f
comment in: #removeOnClose:
Claus Gittinger <cg@exept.de>
parents:
11813
diff
changeset
|
2230 |
version_CVS |
18651 | 2231 |
^ '$Header$' |
1088 | 2232 |
! ! |
6798 | 2233 |
|
15360 | 2234 |
|
2936
5cce0f2166d9
OpenErrorSignal already defined in super (ExternalStream).
Stefan Vogel <sv@exept.de>
parents:
2921
diff
changeset
|
2235 |
FileStream initialize! |