comparison src/file-coding.c @ 5795:d2c0ff38ad5c

Report lstream errors when encoding/decoding. See <CAHCOHQ=FAieD-2nP303fMvwkii8HK2z+X7gRZ2+4PH1CA5_-NA@mail.gmail.com> in xemacs-patches.
author Jerry James <james@xemacs.org>
date Wed, 14 May 2014 14:16:24 -0600
parents 0cb4f494a548
children a216b3c2b09e
comparison
equal deleted inserted replaced
5794:2d20d57d4e7b 5795:d2c0ff38ad5c
2292 Charbpos newpos, even_newer_pos; 2292 Charbpos newpos, even_newer_pos;
2293 Charbpos oldpos = lisp_buffer_stream_startpos (istr); 2293 Charbpos oldpos = lisp_buffer_stream_startpos (istr);
2294 Bytecount size_in_bytes = 2294 Bytecount size_in_bytes =
2295 Lstream_read (istr, tempbuf, sizeof (tempbuf)); 2295 Lstream_read (istr, tempbuf, sizeof (tempbuf));
2296 2296
2297 if (size_in_bytes < 0)
2298 {
2299 int err = Lstream_errno (istr);
2300 if (err)
2301 signal_error_2 (Qtext_conversion_error,
2302 direction == CODING_DECODE
2303 ? "Internal error while decoding"
2304 : "Internal error while encoding",
2305 XCODING_SYSTEM_NAME (coding_system),
2306 lisp_strerror (err));
2307 else
2308 signal_error (Qtext_conversion_error,
2309 direction == CODING_DECODE
2310 ? "Internal error while decoding"
2311 : "Internal error while encoding",
2312 XCODING_SYSTEM_NAME (coding_system));
2313 }
2297 if (!size_in_bytes) 2314 if (!size_in_bytes)
2298 break; 2315 break;
2299 newpos = lisp_buffer_stream_startpos (istr); 2316 newpos = lisp_buffer_stream_startpos (istr);
2300 Lstream_write (ostr, tempbuf, size_in_bytes); 2317 Lstream_write (ostr, tempbuf, size_in_bytes);
2301 even_newer_pos = lisp_buffer_stream_startpos (istr); 2318 even_newer_pos = lisp_buffer_stream_startpos (istr);
3561 1 == stop 3578 1 == stop
3562 */ 3579 */
3563 3580
3564 static int 3581 static int
3565 detect_coding_type (struct detection_state *st, const UExtbyte *src, 3582 detect_coding_type (struct detection_state *st, const UExtbyte *src,
3566 Bytecount n) 3583 Bytecount n, int err)
3567 { 3584 {
3568 Bytecount n2 = n; 3585 Bytecount n2 = n;
3569 const UExtbyte *src2 = src; 3586 const UExtbyte *src2 = src;
3570 int i; 3587 int i;
3588
3589 if (n < 0)
3590 signal_error (Qtext_conversion_error,
3591 "Error reading file to determine coding system",
3592 err ? lisp_strerror (err) : Qnil);
3571 3593
3572 #ifdef DEBUG_XEMACS 3594 #ifdef DEBUG_XEMACS
3573 if (!NILP (Vdebug_coding_detection)) 3595 if (!NILP (Vdebug_coding_detection))
3574 { 3596 {
3575 int bytes = min (16, n); 3597 int bytes = min (16, n);
3782 return Qnil; 3804 return Qnil;
3783 } 3805 }
3784 3806
3785 static Lisp_Object 3807 static Lisp_Object
3786 look_for_coding_system_magic_cookie (const UExtbyte *data, Bytecount len, 3808 look_for_coding_system_magic_cookie (const UExtbyte *data, Bytecount len,
3787 Boolint find_coding_system_p) 3809 Boolint find_coding_system_p, int err)
3788 { 3810 {
3789 const UExtbyte *p; 3811 const UExtbyte *p;
3790 const UExtbyte *scan_end; 3812 const UExtbyte *scan_end;
3791 Bytecount cookie_len; 3813 Bytecount cookie_len;
3814
3815 if (len < 0)
3816 {
3817 signal_error (Qtext_conversion_error,
3818 "Internal error while looking for coding cookie",
3819 err ? lisp_strerror (err) : Qnil);
3820 }
3792 3821
3793 /* Look for initial "-*-"; mode line prefix */ 3822 /* Look for initial "-*-"; mode line prefix */
3794 for (p = data, 3823 for (p = data,
3795 scan_end = data + len - LENGTH ("-*-coding:?-*-"); 3824 scan_end = data + len - LENGTH ("-*-coding:?-*-");
3796 p <= scan_end 3825 p <= scan_end
3862 int depth = record_unwind_protect (unwind_free_detection_state, 3891 int depth = record_unwind_protect (unwind_free_detection_state,
3863 make_opaque_ptr (st)); 3892 make_opaque_ptr (st));
3864 UExtbyte buf[4096]; 3893 UExtbyte buf[4096];
3865 Bytecount nread = Lstream_read (stream, buf, sizeof (buf)); 3894 Bytecount nread = Lstream_read (stream, buf, sizeof (buf));
3866 Lisp_Object coding_system 3895 Lisp_Object coding_system
3867 = look_for_coding_system_magic_cookie (buf, nread, 1); 3896 = look_for_coding_system_magic_cookie (buf, nread, 1,
3897 Lstream_errno (stream));
3868 3898
3869 if (NILP (coding_system)) 3899 if (NILP (coding_system))
3870 { 3900 {
3871 while (1) 3901 while (1)
3872 { 3902 {
3873 if (detect_coding_type (st, buf, nread)) 3903 if (detect_coding_type (st, buf, nread, Lstream_errno (stream)))
3874 break; 3904 break;
3875 nread = Lstream_read (stream, buf, sizeof (buf)); 3905 nread = Lstream_read (stream, buf, sizeof (buf));
3876 if (nread == 0) 3906 if (nread == 0)
3877 break; 3907 break;
3878 } 3908 }
3966 */ 3996 */
3967 struct undecided_coding_system *csdata = 3997 struct undecided_coding_system *csdata =
3968 XCODING_SYSTEM_TYPE_DATA (str->codesys, undecided); 3998 XCODING_SYSTEM_TYPE_DATA (str->codesys, undecided);
3969 struct undecided_coding_stream *data = 3999 struct undecided_coding_stream *data =
3970 CODING_STREAM_TYPE_DATA (str, undecided); 4000 CODING_STREAM_TYPE_DATA (str, undecided);
4001 int err = 0;
3971 4002
3972 if (str->eof) 4003 if (str->eof)
3973 { 4004 {
3974 /* Each will close the next. We need to close now because more 4005 /* Each will close the next. We need to close now because more
3975 data may be generated. */ 4006 data may be generated. */
4009 Lstream_set_buffering (XLSTREAM (data->c.lstreams[0]), 4040 Lstream_set_buffering (XLSTREAM (data->c.lstreams[0]),
4010 LSTREAM_UNBUFFERED, 0); 4041 LSTREAM_UNBUFFERED, 0);
4011 4042
4012 first_time = 1; 4043 first_time = 1;
4013 data->c.initted = 1; 4044 data->c.initted = 1;
4045 err = Lstream_errno (str->other_end);
4014 } 4046 }
4015 4047
4016 /* If necessary, do encoding-detection now. We do this when we're a 4048 /* If necessary, do encoding-detection now. We do this when we're a
4017 writing stream or a non-seekable reading stream, meaning that we 4049 writing stream or a non-seekable reading stream, meaning that we
4018 can't just process the whole input, rewind, and start over. */ 4050 can't just process the whole input, rewind, and start over. */
4026 data->st = allocate_detection_state (); 4058 data->st = allocate_detection_state ();
4027 if (first_time) 4059 if (first_time)
4028 /* #### This is cheesy. What we really ought to do is buffer 4060 /* #### This is cheesy. What we really ought to do is buffer
4029 up a certain minimum amount of data to get a better result. 4061 up a certain minimum amount of data to get a better result.
4030 */ 4062 */
4031 data->actual = look_for_coding_system_magic_cookie (src, n, 1); 4063 data->actual =
4064 look_for_coding_system_magic_cookie (src, n, 1, err);
4032 if (NILP (data->actual)) 4065 if (NILP (data->actual))
4033 { 4066 {
4034 /* #### This is cheesy. What we really ought to do is buffer 4067 /* #### This is cheesy. What we really ought to do is buffer
4035 up a certain minimum amount of data so as to get a less 4068 up a certain minimum amount of data so as to get a less
4036 random result when doing subprocess detection. */ 4069 random result when doing subprocess detection. */
4037 detect_coding_type (data->st, src, n); 4070 detect_coding_type (data->st, src, n, err);
4038 data->actual = detected_coding_system (data->st); 4071 data->actual = detected_coding_system (data->st);
4039 /* kludge to prevent infinite recursion */ 4072 /* kludge to prevent infinite recursion */
4040 if (XCODING_SYSTEM(data->actual)->methods->enumtype == undecided_coding_system) 4073 if (XCODING_SYSTEM(data->actual)->methods->enumtype == undecided_coding_system)
4041 data->actual = Fget_coding_system (Qbinary); 4074 data->actual = Fget_coding_system (Qbinary);
4042 } 4075 }
4289 (filename)) 4322 (filename))
4290 { 4323 {
4291 Lisp_Object lstream; 4324 Lisp_Object lstream;
4292 UExtbyte buf[4096]; 4325 UExtbyte buf[4096];
4293 Bytecount nread; 4326 Bytecount nread;
4294 int fd = -1; 4327 int fd = -1, err;
4295 struct stat st; 4328 struct stat st;
4296 4329
4297 filename = Fexpand_file_name (filename, Qnil); 4330 filename = Fexpand_file_name (filename, Qnil);
4298 4331
4299 if (qxe_stat (XSTRING_DATA (filename), &st) < 0) 4332 if (qxe_stat (XSTRING_DATA (filename), &st) < 0)
4310 } 4343 }
4311 4344
4312 lstream = make_filedesc_input_stream (fd, 0, -1, 0); 4345 lstream = make_filedesc_input_stream (fd, 0, -1, 0);
4313 Lstream_set_buffering (XLSTREAM (lstream), LSTREAM_UNBUFFERED, 0); 4346 Lstream_set_buffering (XLSTREAM (lstream), LSTREAM_UNBUFFERED, 0);
4314 nread = Lstream_read (XLSTREAM (lstream), buf, sizeof (buf)); 4347 nread = Lstream_read (XLSTREAM (lstream), buf, sizeof (buf));
4348 err = Lstream_errno (XLSTREAM (lstream));
4315 Lstream_delete (XLSTREAM (lstream)); 4349 Lstream_delete (XLSTREAM (lstream));
4316 retry_close (fd); 4350 retry_close (fd);
4317 4351
4318 return look_for_coding_system_magic_cookie (buf, nread, 0); 4352 return look_for_coding_system_magic_cookie (buf, nread, 0, err);
4319 } 4353 }
4320 4354
4321 4355
4322 #ifdef DEBUG_XEMACS 4356 #ifdef DEBUG_XEMACS
4323 4357