ali@9: #include ali@9: #include ali@9: #include ali@9: #include ali@9: #include ali@9: #include ali@9: #ifdef WIN32 ali@9: #include ali@9: #endif ali@9: #include ali@9: #include ali@9: #include ali@9: #include "testcase.h" ali@9: #include "testcaseinput.h" ali@9: ali@9: #ifndef O_BINARY ali@9: #define O_BINARY 0 ali@9: #endif ali@9: ali@9: /* ali@9: * As write(), but with error handling. ali@9: */ ali@9: static size_t write_file(int fd,const char *buf,size_t count,GError **error) ali@9: { ali@9: if (write(fd,buf,count)). ali@9: * The file is written in the encoding specified for communicating with ali@9: * bookloupe. The name_used field of is filled in with the name ali@9: * of the created file (which may be different than the name specified ali@9: * if that contained "XXXXXX" to be replaced by a unique string). ali@9: */ ali@9: gboolean testcase_input_create(Testcase *testcase,TestcaseInput *input, ali@9: GError **error) ali@9: { ali@9: int fd; ali@9: size_t n; ali@9: char *filename,*s,*t; ali@9: GError *tmp_err=NULL; ali@9: if (input->contents) ali@9: { ali@9: if (testcase->encoding) ali@9: { ali@9: t=unix2dos(input->contents); ali@9: s=g_convert(t,-1,testcase->encoding,"UTF-8",NULL,&n,&tmp_err); ali@9: g_free(t); ali@9: if (!s) ali@9: { ali@9: g_propagate_prefixed_error(error,tmp_err, ali@9: "Conversion to %s failed: ",testcase->encoding); ali@9: return FALSE; ali@9: } ali@9: } ali@9: else ali@9: { ali@9: s=unix2dos(input->contents); ali@9: n=strlen(s); ali@9: } ali@9: } ali@9: else ali@9: { ali@9: n=0; ali@9: s=NULL; ali@9: } ali@9: g_free(input->name_used); ali@9: input->name_used=NULL; ali@9: if (testcase->tmpdir) ali@9: filename=g_build_filename(testcase->tmpdir,input->name,NULL); ali@9: else ali@9: filename=g_strdup(input->name); ali@9: if (strstr(input->name,"XXXXXX")) ali@9: fd=g_mkstemp(filename); ali@9: else ali@9: fd=g_open(filename,O_WRONLY|O_CREAT|O_EXCL|O_BINARY,0600); ali@9: if (fd<0) ali@9: { ali@9: g_set_error(error,G_FILE_ERROR,g_file_error_from_errno(errno), ali@9: "%s: %s",filename,g_strerror(errno)); ali@9: g_free(s); ali@9: return FALSE; ali@9: } ali@9: input->name_used=g_strdup(filename+strlen(filename)-strlen(input->name)); ali@9: if (n && write_file(fd,s,n,error)!=n) ali@9: { ali@9: g_free(s); ali@9: close(fd); ali@9: (void)g_unlink(filename); ali@9: g_free(filename); ali@9: g_free(input->name_used); ali@9: input->name_used=NULL; ali@9: return FALSE; ali@9: } ali@9: g_free(s); ali@9: if (close(fd)<0) ali@9: { ali@9: g_set_error(error,G_FILE_ERROR,g_file_error_from_errno(errno), ali@9: "%s: %s",filename,g_strerror(errno)); ali@9: (void)g_unlink(filename); ali@9: g_free(filename); ali@9: g_free(input->name_used); ali@9: input->name_used=NULL; ali@9: return FALSE; ali@9: } ali@9: g_free(filename); ali@9: return TRUE; ali@9: } ali@9: ali@9: /* ali@9: * Remove an input file created with testcase_input_create() ali@9: */ ali@9: gboolean testcase_input_remove(Testcase *testcase,TestcaseInput *input, ali@9: GError **error) ali@9: { ali@9: char *filename; ali@9: if (input->name_used) ali@9: { ali@9: if (testcase->tmpdir) ali@9: filename=g_build_filename(testcase->tmpdir,input->name_used,NULL); ali@9: else ali@9: filename=g_strdup(input->name_used); ali@9: if (g_unlink(filename)<0) ali@9: { ali@9: g_set_error(error,G_FILE_ERROR,g_file_error_from_errno(errno), ali@9: "%s: %s",filename,g_strerror(errno)); ali@9: return FALSE; ali@9: } ali@9: g_free(filename); ali@9: g_free(input->name_used); ali@9: input->name_used=NULL; ali@9: } ali@9: return TRUE; ali@9: } ali@9: ali@9: /* Create a new description of an input file needed for a testcase */ ali@9: TestcaseInput *testcase_input_new(const char *name,const char *contents) ali@9: { ali@9: TestcaseInput *input; ali@9: input=g_new0(TestcaseInput,1); ali@9: input->name=g_strdup(name); ali@9: input->contents=g_strdup(contents); ali@9: return input; ali@9: } ali@9: ali@9: /* Free the description of a testcase input file */ ali@9: void testcase_input_free(TestcaseInput *input) ali@9: { ali@9: g_free(input->name); ali@9: g_free(input->name_used); ali@9: g_free(input->contents); ali@9: g_free(input); ali@9: }