]> git.proxmox.com Git - mirror_edk2.git/blob - BaseTools/Source/C/PyUtility/PyUtility.c
ff3ad9443b7aebfe7f1de40bf8e8ffe212c54da9
[mirror_edk2.git] / BaseTools / Source / C / PyUtility / PyUtility.c
1 /** @file
2
3 Copyright (c) 2009 - 2010, Intel Corporation. All rights reserved.<BR>
4 This program and the accompanying materials are licensed and made available
5 under the terms and conditions of the BSD License which accompanies this
6 distribution. The full text of the license may be found at
7 http://opensource.org/licenses/bsd-license.php
8
9 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
10 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
11
12 **/
13
14 #include <Python.h>
15 #include <Windows.h>
16 #include <Common/UefiBaseTypes.h>
17
18 /*
19 SaveFileToDisk(FilePath, Content)
20 */
21 STATIC
22 PyObject*
23 SaveFileToDisk (
24 PyObject *Self,
25 PyObject *Args
26 )
27 {
28 CHAR8 *File;
29 UINT8 *Data;
30 UINTN DataLength;
31 UINTN WriteBytes;
32 UINTN Status;
33 HANDLE FileHandle;
34 PyObject *ReturnValue = Py_False;
35
36 Status = PyArg_ParseTuple(
37 Args,
38 "ss#",
39 &File,
40 &Data,
41 &DataLength
42 );
43 if (Status == 0) {
44 return NULL;
45 }
46
47 FileHandle = CreateFile(
48 File,
49 GENERIC_WRITE,
50 FILE_SHARE_WRITE|FILE_SHARE_READ|FILE_SHARE_DELETE,
51 NULL,
52 CREATE_ALWAYS,
53 FILE_ATTRIBUTE_NORMAL,
54 NULL
55 );
56 if (FileHandle == INVALID_HANDLE_VALUE) {
57 PyErr_SetString(PyExc_Exception, "File creation failure");
58 return NULL;
59 }
60
61 while (WriteFile(FileHandle, Data, DataLength, &WriteBytes, NULL)) {
62 if (DataLength <= WriteBytes) {
63 DataLength = 0;
64 break;
65 }
66
67 Data += WriteBytes;
68 DataLength -= WriteBytes;
69 }
70
71 if (DataLength != 0) {
72 // file saved unsuccessfully
73 PyErr_SetString(PyExc_Exception, "File write failure");
74 goto Done;
75 }
76
77 //
78 // Flush buffer may slow down the whole build performance (average 10s slower)
79 //
80 //if (!FlushFileBuffers(FileHandle)) {
81 // PyErr_SetString(PyExc_Exception, "File flush failure");
82 // goto Done;
83 //}
84
85 // success!
86 ReturnValue = Py_True;
87
88 Done:
89 CloseHandle(FileHandle);
90 return ReturnValue;
91 }
92
93 STATIC INT8 SaveFileToDiskDocs[] = "SaveFileToDisk(): Make sure the file is saved to disk\n";
94
95 STATIC PyMethodDef PyUtility_Funcs[] = {
96 {"SaveFileToDisk", (PyCFunction)SaveFileToDisk, METH_VARARGS, SaveFileToDiskDocs},
97 {NULL, NULL, 0, NULL}
98 };
99
100 PyMODINIT_FUNC
101 initPyUtility(VOID) {
102 Py_InitModule3("PyUtility", PyUtility_Funcs, "Utilties Module Implemented C Language");
103 }
104
105