MagickCore 7.0.10
file.c
Go to the documentation of this file.
1/*
2%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3% %
4% %
5% FFFFF IIIII L EEEEE %
6% F I L E %
7% FFF I L EEE %
8% F I L E %
9% F IIIII LLLLL EEEEE %
10% %
11% %
12% Wizard's Toolkit File Methods %
13% %
14% Software Design %
15% Cristy %
16% March 2003 %
17% %
18% %
19% Copyright @ 1999 ImageMagick Studio LLC, a non-profit organization %
20% dedicated to making software imaging solutions freely available. %
21% %
22% You may not use this file except in compliance with the License. You may %
23% obtain a copy of the License at %
24% %
25% https://imagemagick.org/script/license.php %
26% %
27% Unless required by applicable law or agreed to in writing, software %
28% distributed under the License is distributed on an "AS IS" BASIS, %
29% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. %
30% See the License for the specific language governing permissions and %
31% limitations under the License. %
32% %
33%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
34%
35%
36%
37*/
38
39/*
40 Include declarations.
41*/
42#include "wizard/studio.h"
43#include "wizard/blob.h"
44#include "wizard/exception.h"
46#include "wizard/file.h"
47#include "wizard/memory_.h"
48#include "wizard/nt-base.h"
49#include "wizard/semaphore.h"
50#include "wizard/string_.h"
51#include "wizard/utility.h"
53#if defined(WIZARDSTOOLKIT_THREAD_SUPPORT)
54#include <pthread.h>
55#endif
56#if defined(WIZARDSTOOLKIT_WINDOWS_SUPPORT)
57#include <windows.h>
58#endif
59
60/*
61 Forward declarations.
62*/
65
66/*
67 Typedef declarations.
68*/
70{
71 char
73
74 int
76
77 struct stat
79
82
83 time_t
85
86 size_t
88};
89
90/*
91%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
92% %
93% %
94% %
95% A c q u i r e F i l e L o c k %
96% %
97% %
98% %
99%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
100%
101% AcquireFileLock() acquires a lock for a file.
102%
103% The format of the AcquireFileLock method is:
104%
105% WizardBooleanType AcquireFileLock(FileInfo *file_info,
106% ExceptionInfo *exception)
107%
108% A description of each parameter follows:
109%
110% o file_info: The file info.
111%
112% o exception: Return any errors or warnings in this structure.
113%
114*/
115
116static size_t GetWizardThreadId(void)
117{
118#if defined(WIZARDSTOOLKIT_THREAD_SUPPORT)
119 return((size_t) pthread_self());
120#elif defined(WIZARDSTOOLKIT_WINDOWS_SUPPORT)
121 return((size_t) GetCurrentThreadId());
122#else
123 return((size_t) getpid());
124#endif
125}
126
128 ExceptionInfo *exception)
129{
130 char
131 *path;
132
133 ssize_t
134 pid;
135
136 ssize_t
137 i;
138
139 size_t
140 tid;
141
143 status;
144
145 /*
146 Engage primitive atomic lock.
147 */
148 assert(file_info != (FileInfo *) NULL);
149 assert(file_info->signature == WizardSignature);
150 (void) LogWizardEvent(TraceEvent,GetWizardModule(),"%s",file_info->path);
151 LockSemaphoreInfo(file_info->semaphore);
152 path=AcquireString(file_info->path);
153 AppendFileExtension("lck",path);
154 for (i=0; i < 10; i++)
155 {
156 file_info->file=open_utf8(path,O_WRONLY | O_CREAT | O_EXCL,S_MODE);
157 if (file_info->file == -1)
158 {
159 if (errno != EEXIST)
160 break;
161 file_info->file=open_utf8(path,O_RDONLY,0);
162 if (file_info->file == -1)
163 break;
164 pid=(-1);
165 tid=(~0UL);
166 status=ReadFileChunk(file_info,&pid,sizeof(pid));
167 status&=ReadFileChunk(file_info,&tid,sizeof(tid));
168 if (close(file_info->file) == -1)
170 "unable to close file `%s': %s",path,strerror(errno));
171 file_info->file=(-1);
172 if (status != WizardFalse)
173 {
175 active_process;
176
177 if ((pid == (ssize_t) getpid()) && (tid == GetWizardThreadId()))
178 {
179 path=DestroyString(path);
180 UnlockSemaphoreInfo(file_info->semaphore);
181 return(WizardTrue);
182 }
183#if defined(WIZARDSTOOLKIT_WINDOWS_SUPPORT)
184 {
185 HANDLE
186 handle;
187
188 handle=OpenProcess(PROCESS_ALL_ACCESS,FALSE,(DWORD) pid);
189 active_process=handle == (HANDLE) NULL ? WizardFalse : WizardTrue;
190 if (handle != (HANDLE) NULL)
191 CloseHandle(handle);
192 }
193#else
194 active_process=kill((pid_t) pid,0) == -1 ? WizardFalse : WizardTrue;
195#endif
196 if (active_process == WizardFalse)
197 {
198 if (errno != ESRCH)
199 break;
200 i--;
201 if (remove_utf8(path) == -1)
202 (void) ThrowWizardException(exception,GetWizardModule(),
203 FileError,"unable to remove file `%s': %s",path,
204 strerror(errno));
205 break;
206 }
207 }
208 WizardDelay(1000);
209 continue;
210 }
211 pid=(ssize_t) getpid();
212 status=WriteFileChunk(file_info,&pid,sizeof(pid));
213 tid=GetWizardThreadId();
214 status=WriteFileChunk(file_info,&tid,sizeof(tid));
215 if (close(file_info->file) == -1)
217 "unable to close file `%s': %s",path,strerror(errno));
218 file_info->file=(-1);
219 if (status == WizardFalse)
220 break;
221 i--;
222 }
223 path=DestroyString(path);
224 UnlockSemaphoreInfo(file_info->semaphore);
225 return(WizardFalse);
226}
227
228/*
229%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % %
230% %
231% %
232% A c q u i r e F i l e I n f o %
233% %
234% %
235% %
236%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
237%
238% AcquireFileInfo() opens the file for reading or writing and returns a
239% FileInfo structure.
240%
241% The format of the AcquireFileInfo method is:
242%
243% FileInfo *AcquireFileInfo(const char *path,const char *relative_path,
244% const FileMode mode,ExceptionInfo *exception)
245%
246% A description of each parameter follows:
247%
248% o path: The file path.
249%
250% o relative_path: The path relative to the default path.
251%
252% o mode: The file I/O mode.
253%
254% o exception: Return any errors or warnings in this structure.
255%
256*/
258 const char *relative_path,const FileMode mode,ExceptionInfo *exception)
259{
260 char
261 *home;
262
264 *file_info;
265
266 /*
267 Acquire file info.
268 */
269 assert(relative_path != (char *) NULL);
270 (void) LogWizardEvent(TraceEvent,GetWizardModule(),"%s",relative_path);
271 assert(exception != (ExceptionInfo *) NULL);
272 file_info=(FileInfo *) AcquireWizardMemory(sizeof(*file_info));
273 if (file_info == (FileInfo *) NULL)
274 {
276 "memory allocation failed `%s'",strerror(errno));
277 return((FileInfo *) NULL);
278 }
279 (void) memset(file_info,0,sizeof(*file_info));
280 file_info->path=AcquireString((char *) NULL);
281 file_info->file=(-1);
282 file_info->semaphore=AcquireSemaphoreInfo();
283 file_info->timestamp=time((time_t *) NULL);
284 file_info->signature=WizardSignature;
285 if (path != (char *) NULL)
286 (void) CopyWizardString(file_info->path,path,WizardPathExtent);
287 else
288 {
289 /*
290 Default location for key ring, random state, etc.
291 */
292 *file_info->path='\0';
293 home=GetEnvironmentValue("WIZARD_HOME");
294 if (home != (char *) NULL)
295 {
296 (void) CopyWizardString(file_info->path,home,WizardPathExtent);
299 home=(char *) RelinquishWizardMemory(home);
300 }
301 else
302 {
303 home=GetEnvironmentValue("HOME");
304 if (home == (char *) NULL)
305 home=GetEnvironmentValue("USERPROFILE");
306 if (home != (char *) NULL)
307 {
308 (void) CopyWizardString(file_info->path,home,WizardPathExtent);
309 (void) ConcatenateWizardString(file_info->path,
311 (void) ConcatenateWizardString(file_info->path,".wizard",
313#if defined(WIZARDSTOOLKIT_WINDOWS_SUPPORT)
314 (void) mkdir(file_info->path);
315#else
316 (void) mkdir(file_info->path,0700);
317#endif
318 (void) ConcatenateWizardString(file_info->path,
320 home=(char *) RelinquishWizardMemory(home);
321 }
322 }
323 (void) ConcatenateWizardString(file_info->path,relative_path,
325 }
326 if (AcquireFileLock(file_info,exception) == WizardFalse)
327 {
328 file_info=DestroyFileInfo(file_info,exception);
329 return((FileInfo *) NULL);
330 }
331 /*
332 Open file.
333 */
334 switch (mode)
335 {
336 case ReadFileMode:
337 {
338 file_info->file=open_utf8(file_info->path,O_RDONLY | O_BINARY,0);
339 break;
340 }
341 case WriteFileMode:
342 {
343 file_info->file=open_utf8(file_info->path,O_RDWR | O_CREAT | O_BINARY,
344 S_MODE);
345 break;
346 }
347 default:
348 {
350 "Invalid file mode `%s'",path);
351 file_info=DestroyFileInfo(file_info,exception);
352 return((FileInfo *) NULL);
353 }
354 }
355 if (file_info->file < 0)
356 {
357 file_info=DestroyFileInfo(file_info,exception);
358 return((FileInfo *) NULL);
359 }
360 (void) fstat(file_info->file,&file_info->properties);
361 return(file_info);
362}
363
364/*
365%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
366% %
367% %
368% %
369% D e s t r o y F i l e %
370% %
371% %
372% %
373%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
374%
375% DestroyFile() destroys the file on disk.
376%
377% The format of the DestroyFile method is:
378%
379% WizardBooleanType DestroyFile(FileInfo *file_info,
380% Exceptioninfo *exception)
381%
382% A description of each parameter follows:
383%
384% o file_info: The file info.
385%
386% o exception: Return any errors or warnings in this structure.
387%
388*/
390 ExceptionInfo *exception)
391{
392 if (file_info->file >= 0)
393 if (close(file_info->file) == -1)
394 {
396 "unable to close file `%s': %s",file_info->path,strerror(errno));
397 return(WizardFalse);
398 }
399 file_info->file=(-1);
400 if (remove_utf8(file_info->path) == -1)
401 {
403 "unable to close file `%s': %s",file_info->path,strerror(errno));
404 return(WizardFalse);
405 }
406 return(WizardTrue);
407}
408
409/*
410%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
411% %
412% %
413% %
414% D e s t r o y F i l e I n f o %
415% %
416% %
417% %
418%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
419%
420% DestroyFileInfo() destroys the file info structure.
421%
422% The format of the DestroyFileInfo method is:
423%
424% FileInfo *DestroyFileInfo(FileInfo *file_info)
425%
426% A description of each parameter follows:
427%
428% o path: The file name.
429%
430*/
432 ExceptionInfo *exception)
433{
434 assert(file_info != (FileInfo *) NULL);
435 assert(file_info->signature == WizardSignature);
436 (void) LogWizardEvent(TraceEvent,GetWizardModule(),"%s",file_info->path);
437 LockSemaphoreInfo(file_info->semaphore);
438 if (file_info->file >= 0)
439 if (close(file_info->file) == -1)
441 "unable to close file `%s': %s",file_info->path,strerror(errno));
442 file_info->file=(-1);
443 (void) RelinquishFileLock(file_info,exception);
444 if (file_info->path != (char *) NULL)
445 file_info->path=DestroyString(file_info->path);
446 file_info->signature=(~WizardSignature);
447 UnlockSemaphoreInfo(file_info->semaphore);
449 file_info=(FileInfo *) RelinquishWizardMemory(file_info);
450 return(file_info);
451}
452
453/*
454%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
455% %
456% %
457% %
458% G e t F i l e D e s c r i p t o r %
459% %
460% %
461% %
462%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
463%
464% GetFileDescriptor() returns the file descriptor.
465%
466% The format of the GetFileDescriptor method is:
467%
468% int GetFileDescriptor(const FileInfo *file_info)
469%
470% A description of each parameter follows:
471%
472% o file_info: The file info.
473%
474*/
476{
478 assert(file_info != (FileInfo *) NULL);
479 assert(file_info->signature == WizardSignature);
480 return(file_info->file);
481}
482
483/*
484%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
485% %
486% %
487% %
488% G e t F i l e P a t h %
489% %
490% %
491% %
492%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
493%
494% GetFilePath() returns the file path.
495%
496% The format of the GetFilePath method is:
497%
498% const const char *GetFilePath(const FileInfo *file_info)
499%
500% A description of each parameter follows:
501%
502% o file_info: The file info.
503%
504*/
505WizardExport const char *GetFilePath(const FileInfo *file_info)
506{
508 assert(file_info != (FileInfo *) NULL);
509 assert(file_info->signature == WizardSignature);
510 return(file_info->path);
511}
512
513/*
514%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
515% %
516% %
517% %
518% G e t F i l e P r o p e r t i e s %
519% %
520% %
521% %
522%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
523%
524% GetFileproperties() returns the file properties.
525%
526% The format of the GetFileProperties method is:
527%
528% const struct stat *GetFileProperties(const FileInfo *file_info)
529%
530% A description of each parameter follows:
531%
532% o file_info: The file info.
533%
534*/
535WizardExport const struct stat *GetFileProperties(const FileInfo *file_info)
536{
538 assert(file_info != (FileInfo *) NULL);
539 assert(file_info->signature == WizardSignature);
540 return(&file_info->properties);
541}
542
543/*
544%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
545% %
546% %
547% %
548% R e a d F i l e B y t e %
549% %
550% %
551% %
552%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
553%
554% ReadFileByte() returns the character read as an unsigned char cast to an
555% integer or EOF on end of file or error.
556%
557% The format of the ReadFileByte method is:
558%
559% int ReadFileByte(FileInfo *file_info)
560%
561% A description of each parameter follows.
562%
563% o file_info: the blob info.
564%
565*/
567{
568 unsigned char
569 buffer[1];
570
571 assert(file_info != (FileInfo *) NULL);
572 assert(file_info->signature == WizardSignature);
573 if (ReadFileChunk(file_info,buffer,1) == WizardFalse)
574 return(EOF);
575 return((int) (*buffer));
576}
577
578/*
579%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
580% %
581% %
582% %
583% R e a d F i l e C h u n k %
584% %
585% %
586% %
587%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
588%
589% ReadFileChunk() reliably reads a chunk of data from a file. It returns
590% WizardTrue if all the data requested is read otherwise WizardFalse.
591%
592% The format of the ReadFileChunk method is:
593%
594% WizardBooleanType ReadFileChunk(FileInfo *file_info,const void *data,
595% const size_t length)
596%
597% A description of each parameter follows:
598%
599% o file_info: The file info.
600%
601% o data: The data.
602%
603% o length: The data length in bytes.
604%
605*/
607 const size_t length)
608{
609 ssize_t
610 i;
611
612 ssize_t
613 count;
614
615 assert(file_info != (FileInfo *) NULL);
616 assert(file_info->signature == WizardSignature);
617 (void) LogWizardEvent(TraceEvent,GetWizardModule(),"%s",file_info->path);
618 for (i=0; i < (ssize_t) length; i+=count)
619 {
620 count=read(file_info->file,(unsigned char *) data+i,Min(length-i,(size_t)
622 if (count <= 0)
623 {
624 count=0;
625 if (errno != EINTR)
626 break;
627 }
628 }
629 if (i < (ssize_t) length)
630 return(WizardFalse);
631 return(WizardTrue);
632}
633
634/*
635%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
636% %
637% %
638% %
639% R e a d F i l e 1 6 B i t s %
640% %
641% %
642% %
643%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
644%
645% ReadFile16Bits() reads a 16-bit quantity in least-significant byte first
646% order and returns it.
647%
648% The format of the ReadFile16Bits method is:
649%
650% WizardBooleanType ReadFile16Bits(FileInfo *file_info,
651% unsigned short *value)
652%
653% A description of each parameter follows.
654%
655% o file_info: The image.
656%
657% o value: The value.
658%
659*/
661 unsigned short *value)
662{
664 status;
665
666 unsigned char
667 buffer[2];
668
669 assert(file_info != (FileInfo *) NULL);
670 assert(file_info->signature == WizardSignature);
671 *buffer=(unsigned char) '\0';
672 status=ReadFileChunk(file_info,buffer,sizeof(buffer));
673 if (status == WizardFalse)
674 return(status);
675 *value=(unsigned short) (buffer[1] << 8);
676 *value|=(unsigned short) buffer[0];
677 return(status);
678}
679
680/*
681%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
682% %
683% %
684% %
685% R e a d F i l e 3 2 B i t s %
686% %
687% %
688% %
689%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
690%
691% ReadFile32Bits() reads a 32-bit quantity in least-significant byte first
692% order and returns it.
693%
694% The format of the ReadFile32Bits method is:
695%
696% WizardBooleanType ReadFile32Bits(FileInfo *file_info,
697% size_t *value)
698%
699% A description of each parameter follows.
700%
701% o file_info: The image.
702%
703% o value: The value.
704%
705*/
707 size_t *value)
708{
710 status;
711
712 unsigned char
713 buffer[4];
714
715 assert(file_info != (FileInfo *) NULL);
716 assert(file_info->signature == WizardSignature);
717 *buffer=(unsigned char) '\0';
718 status=ReadFileChunk(file_info,buffer,sizeof(buffer));
719 if (status == WizardFalse)
720 return(status);
721 *value=((size_t) buffer[3]) << 24;
722 *value|=((size_t) buffer[2]) << 16;
723 *value|=((size_t) buffer[1]) << 8;
724 *value|=((size_t) buffer[0]);
725 return(status);
726}
727
728/*
729%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
730% %
731% %
732% %
733% R e a d F i l e 6 4 B i t s %
734% %
735% %
736% %
737%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
738%
739% ReadFile64Bits() reads a 64-bit quantity in least-significant byte first
740% order and returns it.
741%
742% The format of the ReadFile64Bits method is:
743%
744% WizardBooleanType ReadFile64Bits(FileInfo *file_info,
745% WizardSizeType *value)
746%
747% A description of each parameter follows.
748%
749% o file_info: The image.
750%
751% o value: The value.
752%
753*/
755 WizardSizeType *value)
756{
758 status;
759
760 unsigned char
761 buffer[8];
762
763 assert(file_info != (FileInfo *) NULL);
764 assert(file_info->signature == WizardSignature);
765 *buffer=(unsigned char) '\0';
766 status=ReadFileChunk(file_info,buffer,sizeof(buffer));
767 if (status == WizardFalse)
768 return(status);
769 *value=((WizardSizeType) buffer[7]) << 56;
770 *value|=((WizardSizeType) buffer[6]) << 48;
771 *value|=((WizardSizeType) buffer[5]) << 40;
772 *value|=((WizardSizeType) buffer[4]) << 32;
773 *value|=((WizardSizeType) buffer[3]) << 24;
774 *value|=((WizardSizeType) buffer[2]) << 16;
775 *value|=((WizardSizeType) buffer[1]) << 8;
776 *value|=(WizardSizeType) buffer[0];
777 return(status);
778}
779
780/*
781%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
782% %
783% %
784% %
785% R e l i n q u i s h F i l e L o c k %
786% %
787% %
788% %
789%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
790%
791% RelinquishFileLock() relinquishes a file lock.
792%
793% The format of the RelinquishFileLock method is:
794%
795% WizardBooleanType RelinquishFileLock(FileInfo *file_info,
796% ExceptionInfo *exception)
797%
798% A description of each parameter follows:
799%
800% o file_info: The file info.
801%
802% o exception: Return any errors or warnings in this structure.
803%
804*/
806 ExceptionInfo *exception)
807{
808 char
809 *path;
810
811 path=AcquireString(file_info->path);
812 AppendFileExtension("lck",path);
813 if (remove_utf8(path) == -1)
814 {
816 "unable to remove file `%s': %s",path,strerror(errno));
817 path=DestroyString(path);
818 return(WizardFalse);
819 }
820 path=DestroyString(path);
821 return(WizardTrue);
822}
823
824/*
825%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
826% %
827% %
828% %
829% W r i t e F i l e C h u n k %
830% %
831% %
832% %
833%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
834%
835% WriteFileChunk() reliably writes data to a file.
836%
837% The format of the WriteFileChunk method is:
838%
839% WizardBooleanType WriteFileChunk(FileInfo *file_info,const void *data,
840% const size_t length)
841%
842% A description of each parameter follows:
843%
844% o file_info: The file info.
845%
846% o data: The data.
847%
848% o lenth: The data length in bytes.
849%
850*/
852 const void *data,const size_t length)
853{
854 ssize_t
855 i;
856
857 ssize_t
858 count;
859
860 assert(file_info != (FileInfo *) NULL);
861 assert(file_info->signature == WizardSignature);
862 (void) LogWizardEvent(TraceEvent,GetWizardModule(),"%s",file_info->path);
863 for (i=0; i < (ssize_t) length; i+=count)
864 {
865 count=write(file_info->file,(unsigned char *) data+i,length-i);
866 if (count <= 0)
867 {
868 count=0;
869 if (errno != EINTR)
870 break;
871 }
872 }
873 if (i < (ssize_t) length)
874 return(WizardFalse);
875 return(WizardTrue);
876}
877
878/*
879%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
880% %
881% %
882% %
883% W r i t e F i l e 1 6 B i t s %
884% %
885% %
886% %
887%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
888%
889% WriteFile16Bits() writes a ssize_t value as a 16-bit quantity in
890% least-significant byte first order.
891%
892% The format of the WriteFile16Bits method is:
893%
894% WizardBooleanType WriteFile16Bits(FileInfo *file_info,
895% const unsigned short value)
896%
897% A description of each parameter follows.
898%
899% o value: Specifies the value to write.
900%
901% o file_info: The image.
902%
903*/
905 const unsigned short value)
906{
907 unsigned char
908 buffer[2];
909
910 assert(file_info != (FileInfo *) NULL);
911 assert(file_info->signature == WizardSignature);
912 buffer[0]=(unsigned char) value;
913 buffer[1]=(unsigned char) (value >> 8);
914 return(WriteFileChunk(file_info,buffer,sizeof(buffer)));
915}
916
917/*
918%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
919% %
920% %
921% %
922% W r i t e F i l e 3 2 B i t s %
923% %
924% %
925% %
926%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
927%
928% WriteFile32Bits() writes a ssize_t value as a 32-bit quantity in
929% least-significant byte first order.
930%
931% The format of the WriteFile32Bits method is:
932%
933% WizardBooleanType WriteFile32Bits(FileInfo *file_info,
934% const size_t value)
935%
936% A description of each parameter follows.
937%
938% o value: Specifies the value to write.
939%
940% o file_info: The image.
941%
942*/
944 const size_t value)
945{
946 unsigned char
947 buffer[4];
948
950 assert(file_info != (FileInfo *) NULL);
951 buffer[0]=(unsigned char) value;
952 buffer[1]=(unsigned char) (value >> 8);
953 buffer[2]=(unsigned char) (value >> 16);
954 buffer[3]=(unsigned char) (value >> 24);
955 return(WriteFileChunk(file_info,buffer,sizeof(buffer)));
956}
957
958/*
959%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
960% %
961% %
962% %
963% W r i t e F i l e 6 4 B i t s %
964% %
965% %
966% %
967%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
968%
969% WriteFile64Bits() writes a ssize_t value as a 64-bit quantity in
970% least-significant byte first order.
971%
972% The format of the WriteFile64Bits method is:
973%
974% WizardBooleanType WriteFile64Bits(FileInfo *file_info,
975% const WizardSizeType value)
976%
977% A description of each parameter follows.
978%
979% o value: Specifies the value to write.
980%
981% o file_info: The image.
982%
983*/
985 const WizardSizeType value)
986{
987 unsigned char
988 buffer[8];
989
991 assert(file_info != (FileInfo *) NULL);
992 buffer[0]=(unsigned char) value;
993 buffer[1]=(unsigned char) (value >> 8);
994 buffer[2]=(unsigned char) (value >> 16);
995 buffer[3]=(unsigned char) (value >> 24);
996 buffer[4]=(unsigned char) (value >> 32);
997 buffer[5]=(unsigned char) (value >> 40);
998 buffer[6]=(unsigned char) (value >> 48);
999 buffer[7]=(unsigned char) (value >> 56);
1000 return(WriteFileChunk(file_info,buffer,sizeof(buffer)));
1001}
#define WizardMaxBufferExtent
Definition blob.h:28
WizardExport WizardBooleanType ThrowWizardException(ExceptionInfo *exception, const char *module, const char *function, const size_t line, const ExceptionType severity, const char *format,...)
Definition exception.c:1029
@ FileError
Definition exception.h:99
WizardExport WizardBooleanType ReadFile64Bits(FileInfo *file_info, WizardSizeType *value)
Definition file.c:754
WizardExport const char * GetFilePath(const FileInfo *file_info)
Definition file.c:505
static WizardBooleanType AcquireFileLock(FileInfo *file_info, ExceptionInfo *exception)
Definition file.c:127
static size_t GetWizardThreadId(void)
Definition file.c:116
WizardExport FileInfo * AcquireFileInfo(const char *path, const char *relative_path, const FileMode mode, ExceptionInfo *exception)
Definition file.c:257
WizardExport WizardBooleanType WriteFile32Bits(FileInfo *file_info, const size_t value)
Definition file.c:943
WizardExport WizardBooleanType WriteFile64Bits(FileInfo *file_info, const WizardSizeType value)
Definition file.c:984
static WizardBooleanType RelinquishFileLock(FileInfo *, ExceptionInfo *)
Definition file.c:805
WizardExport WizardBooleanType DestroyFile(FileInfo *file_info, ExceptionInfo *exception)
Definition file.c:389
WizardExport int ReadFileByte(FileInfo *file_info)
Definition file.c:566
WizardExport WizardBooleanType WriteFile16Bits(FileInfo *file_info, const unsigned short value)
Definition file.c:904
WizardExport const struct stat * GetFileProperties(const FileInfo *file_info)
Definition file.c:535
WizardExport WizardBooleanType ReadFileChunk(FileInfo *file_info, void *data, const size_t length)
Definition file.c:606
WizardExport int GetFileDescriptor(const FileInfo *file_info)
Definition file.c:475
WizardExport WizardBooleanType WriteFileChunk(FileInfo *file_info, const void *data, const size_t length)
Definition file.c:851
WizardExport WizardBooleanType ReadFile32Bits(FileInfo *file_info, size_t *value)
Definition file.c:706
WizardExport FileInfo * DestroyFileInfo(FileInfo *file_info, ExceptionInfo *exception)
Definition file.c:431
WizardExport WizardBooleanType ReadFile16Bits(FileInfo *file_info, unsigned short *value)
Definition file.c:660
FileMode
Definition file.h:28
@ WriteFileMode
Definition file.h:30
@ ReadFileMode
Definition file.h:29
WizardBooleanType LogWizardEvent(const LogEventType type, const char *module, const char *function, const size_t line, const char *format,...)
Definition log.c:1390
@ TraceEvent
Definition log.h:39
#define GetWizardModule()
Definition log.h:30
WizardExport void * AcquireWizardMemory(const size_t size)
Definition memory.c:586
WizardExport void * RelinquishWizardMemory(void *memory)
Definition memory.c:1039
#define WizardExport
#define WizardPathExtent
#define WizardSignature
WizardExport void RelinquishSemaphoreInfo(SemaphoreInfo **semaphore_info)
Definition semaphore.c:338
WizardExport SemaphoreInfo * AcquireSemaphoreInfo(void)
Definition semaphore.c:156
WizardExport void LockSemaphoreInfo(SemaphoreInfo *semaphore_info)
Definition semaphore.c:282
WizardExport void UnlockSemaphoreInfo(SemaphoreInfo *semaphore_info)
Definition semaphore.c:437
WizardExport char * DestroyString(char *string)
Definition string.c:830
WizardExport size_t ConcatenateWizardString(char *destination, const char *source, const size_t length)
Definition string.c:478
WizardExport char * AcquireString(const char *source)
Definition string.c:133
WizardExport char * GetEnvironmentValue(const char *name)
Definition string.c:1177
WizardExport size_t CopyWizardString(char *destination, const char *source, const size_t length)
Definition string.c:762
SemaphoreInfo * semaphore
Definition file.c:81
int file
Definition file.c:75
time_t timestamp
Definition file.c:84
char * path
Definition file.c:72
struct stat properties
Definition file.c:77
size_t signature
Definition file.c:87
#define Min(x, y)
Definition studio.h:307
#define O_BINARY
Definition studio.h:326
#define DirectorySeparator
Definition studio.h:239
#define S_MODE
Definition studio.h:334
static int open_utf8(const char *path, int flags, mode_t mode)
static int remove_utf8(const char *path)
WizardExport void WizardDelay(const WizardSizeType)
Definition utility.c:1042
WizardExport void AppendFileExtension(const char *extension, char *filename)
Definition utility.c:94
size_t WizardSizeType
Definition wizard-type.h:51
WizardBooleanType
Definition wizard-type.h:26
@ WizardTrue
Definition wizard-type.h:28
@ WizardFalse
Definition wizard-type.h:27
unsigned int WizardStatusType
Definition wizard-type.h:42