This is the mail archive of the cygwin-apps mailing list for the Cygwin project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[patch/rebase] Open file only once


Hi Chuck,


here's another simplification of peflags.  The file gets opened and
closed in do_mark now.  get_xxx and set_xxx get the pe_flags pointer
rather than the filename as parameter.

Ok to apply?


Corinna


	* peflags.c (pe_file): Move definition to start of file.
	(pe_open): Declare.
	(pe_close): Declare.
	(do_mark): Call pe_open/pe_close here.  Call get/set functions
	weith pe_file pointer instead of filename.
	(get_characteristics): Change first parameter to pe_file pointer.
	Drop calls to pe_open/pe_close.
	(set_coff_characteristics): Ditto.
	(set_pe_characteristics): Ditto.


Index: peflags.c
===================================================================
RCS file: /sourceware/projects/cygwin-apps-home/cvsfiles/rebase/peflags.c,v
retrieving revision 1.6
diff -u -p -r1.6 peflags.c
--- peflags.c	6 Aug 2011 08:30:00 -0000	1.6
+++ peflags.c	6 Aug 2011 19:23:06 -0000
@@ -53,6 +53,17 @@ static WORD pe_characteristics_set;
 static WORD pe_characteristics_clr;
 static WORD pe_characteristics_show;
 
+typedef struct
+{
+  PIMAGE_DOS_HEADER dosheader;
+  union
+    {
+      PIMAGE_NT_HEADERS32 ntheader32;
+      PIMAGE_NT_HEADERS64 ntheader64;
+    };
+  BOOL is_64bit;
+} pe_file;
+
 typedef struct {
   long          flag;
   const char *  name;
@@ -126,12 +137,14 @@ static void help (FILE *f);
 static void version (FILE *f);
 
 int do_mark (const char *pathname);
-int get_characteristics(const char *pathname,
+pe_file *pe_open (const char *path, BOOL writing);
+void pe_close (pe_file *pep);
+int get_characteristics(const pe_file *pep,
                         WORD* coff_characteristics,
                         WORD* pe_characteristics);
-int set_coff_characteristics(const char *pathname,
+int set_coff_characteristics(const pe_file *pep,
                              WORD coff_characteristics);
-int set_pe_characteristics(const char *pathname,
+int set_pe_characteristics(const pe_file *pep,
                            WORD pe_characteristics);
 
 static void display_flags (const char *field_name, const symbolic_flags_t *syms,
@@ -249,7 +262,16 @@ do_mark (const char *pathname)
         }
     }
 
-  if (get_characteristics (pathname,
+  pe_file *pep = pe_open (pathname, mark_any != 0);
+  if (!pep)
+    {
+      fprintf (stderr,
+               "%s: skipped because could not open\n",
+               pathname);
+      return 0;
+    }
+
+  if (get_characteristics (pep,
                            &old_coff_characteristics,
                            &old_pe_characteristics) != 0)
     {
@@ -323,7 +345,7 @@ do_mark (const char *pathname)
 
       /* setting */
       if (new_coff_characteristics != old_coff_characteristics)
-        if (set_coff_characteristics (pathname,new_coff_characteristics) != 0)
+        if (set_coff_characteristics (pep, new_coff_characteristics) != 0)
           {
             fprintf (stderr,
                      "Error: could not update coff characteristics (%s): %s\n",
@@ -331,7 +353,7 @@ do_mark (const char *pathname)
             return 1;
           }
       if (new_pe_characteristics != old_pe_characteristics)
-        if (set_pe_characteristics (pathname,new_pe_characteristics) != 0)
+        if (set_pe_characteristics (pep, new_pe_characteristics) != 0)
           {
             fprintf (stderr,
                      "Error: could not update pe characteristics (%s): %s\n",
@@ -360,6 +382,8 @@ do_mark (const char *pathname)
       puts ("");
     }
 
+  pe_close (pep);
+
   return 0;
 }
 
@@ -713,17 +737,6 @@ munmap (void *addr, size_t len)
 }
 #endif
 
-typedef struct
-{
-  PIMAGE_DOS_HEADER dosheader;
-  union
-    {
-      PIMAGE_NT_HEADERS32 ntheader32;
-      PIMAGE_NT_HEADERS64 ntheader64;
-    };
-  BOOL is_64bit;
-} pe_file;
-
 pe_file *
 pe_open (const char *path, BOOL writing)
 {
@@ -767,13 +780,10 @@ pe_close (pe_file *pep)
 }
 
 int
-get_characteristics(const char *pathname,
+get_characteristics(const pe_file *pep,
                     WORD* coff_characteristics,
                     WORD* pe_characteristics)
 {
-  pe_file *pep = pe_open (pathname, FALSE);
-  if (!pep)
-    return 1;
   if (pep->is_64bit)
     {
       *coff_characteristics = pep->ntheader64->FileHeader.Characteristics;
@@ -784,37 +794,28 @@ get_characteristics(const char *pathname
       *coff_characteristics = pep->ntheader32->FileHeader.Characteristics;
       *pe_characteristics = pep->ntheader32->OptionalHeader.DllCharacteristics;
     }
-  pe_close (pep);
   return 0;
 }
 
 int
-set_coff_characteristics(const char *pathname,
+set_coff_characteristics(const pe_file *pep,
                          WORD coff_characteristics)
 {
-  pe_file *pep = pe_open (pathname, TRUE);
-  if (!pep)
-    return 1;
   if (pep->is_64bit)
     pep->ntheader64->FileHeader.Characteristics = coff_characteristics;
   else
     pep->ntheader32->FileHeader.Characteristics = coff_characteristics;
-  pe_close (pep);
   return 0;
 }
 
 int
-set_pe_characteristics(const char *pathname,
+set_pe_characteristics(const pe_file *pep,
                        WORD pe_characteristics)
 {
-  pe_file *pep = pe_open (pathname, TRUE);
-  if (!pep)
-    return 1;
   if (pep->is_64bit)
     pep->ntheader64->OptionalHeader.DllCharacteristics = pe_characteristics;
   else
     pep->ntheader32->OptionalHeader.DllCharacteristics = pe_characteristics;
-  pe_close (pep);
   return 0;
 }
 

-- 
Corinna Vinschen                  Please, send mails regarding Cygwin to
Cygwin Project Co-Leader          cygwin AT cygwin DOT com
Red Hat


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]