Ticket #204: patch-204b.txt

File patch-204b.txt, 16.8 KB (added by Yuri Dario, 10 years ago)
Line 
1Index: src/emx/include/InnoTekLIBC/backend.h
2===================================================================
3--- src/emx/include/InnoTekLIBC/backend.h       (revision 3803)
4+++ src/emx/include/InnoTekLIBC/backend.h       (working copy)
5@@ -326,6 +326,16 @@
6 int __libc_Back_fsSymlinkModeSet(const char *pszPath, mode_t Mode);
7 
8 /**
9+ * Sets the file group/owner of a file.
10+ *
11+ * @returns 0 on success.
12+ * @returns Negative error code (errno.h) on failure.
13+ * @param   fh      Handle to file.
14+ * @param   Mode    The filemode.
15+ */
16+int __libc_Back_fsSymlinkOwnerSet(const char *pszPath, uid_t owner, gid_t group);
17+
18+/**
19  * Sets the file times of a symlink.
20  *
21  * @returns 0 on success.
22@@ -377,6 +387,17 @@
23 int __libc_Back_fsFileModeSetFH(int fh, mode_t Mode);
24 
25 /**
26+ * Sets the file group/owner of a file.
27+ *
28+ * @returns 0 on success.
29+ * @returns Negative error code (errno.h) on failure.
30+ * @param   pszPath The path to the file to set the mode of.
31+ * @param   owner    The new owner, if -1 do not change it.
32+ * @param   group    The new group, if -1 do not change it.
33+ */
34+int __libc_Back_fsFileOwnerSet(const char *pszPath, uid_t owner, gid_t group);
35+
36+/**
37  * Sets the file the times of a file.
38  *
39  * @returns 0 on success.
40Index: src/emx/src/lib/libc.def
41===================================================================
42--- src/emx/src/lib/libc.def    (revision 3803)
43+++ src/emx/src/lib/libc.def    (working copy)
44@@ -1962,3 +1962,7 @@
45 
46     "___libc_Back_fsPathConf" @1959
47     "___libc_Back_ioPathConf" @1960
48+
49+    "___libc_Back_fsFileOwnerSet" @1961
50+    "___libc_Back_fsFileOwnerSetFH" @1962
51+    "___libc_Back_fsSymlinkOwnerSet" @1963
52Index: src/emx/src/lib/misc/chown.c
53===================================================================
54--- src/emx/src/lib/misc/chown.c        (revision 3803)
55+++ src/emx/src/lib/misc/chown.c        (working copy)
56@@ -30,6 +30,7 @@
57 *   Header Files                                                               *
58 *******************************************************************************/
59 #include "libc-alias.h"
60+#include <errno.h>
61 #include <unistd.h>
62 #include <sys/stat.h>
63 #define __LIBC_LOG_GROUP __LIBC_LOG_GRP_IO
64@@ -40,8 +41,6 @@
65 /**
66  * Change the owner and group over a file.
67  *
68- * This is stub. It only validates the path and returns without changing anything.
69- *
70  * @returns 0 on success.
71  * @returns -1 and errno on failure.
72  * @param   path    Path to the file to change owner/group of.
73@@ -52,9 +51,10 @@
74 {
75     LIBCLOG_ENTER("path=%p:{%s} owner=%d group=%d\n", (void *)path, path, owner, group);
76     struct stat st;
77-    int rc = stat(path, &st);
78+    int rc = __libc_Back_fsFileOwnerSet(path, owner, group);
79     if (!rc)
80-        LIBCLOG_RETURN_INT(rc);
81-    LIBCLOG_ERROR_RETURN_INT(rc);
82+        LIBCLOG_RETURN_INT(0);
83+    errno = -rc;
84+    LIBCLOG_ERROR_RETURN_INT(-1);
85 }
86 
87Index: src/emx/src/lib/io/lchown.c
88===================================================================
89--- src/emx/src/lib/io/lchown.c (revision 3803)
90+++ src/emx/src/lib/io/lchown.c (working copy)
91@@ -30,6 +30,7 @@
92 *   Header Files                                                               *
93 *******************************************************************************/
94 #include "libc-alias.h"
95+#include <errno.h>
96 #include <unistd.h>
97 #include <sys/stat.h>
98 #define __LIBC_LOG_GROUP __LIBC_LOG_GRP_IO
99@@ -40,8 +41,6 @@
100 /**
101  * Change the owner and group over a symbolic link.
102  *
103- * This is stub. It only validates the path and returns without changing anything.
104- *
105  * @returns 0 on success.
106  * @returns -1 and errno on failure.
107  * @param   path    Path to the link to change owner/group of.
108@@ -52,9 +51,10 @@
109 {
110     LIBCLOG_ENTER("path=%p:{%s} owner=%d group=%d\n", (void *)path, path, owner, group);
111     struct stat st;
112-    int rc = lstat(path, &st);
113+    int rc = __libc_Back_fsSymlinkOwnerSet(path, owner, group);
114     if (!rc)
115         LIBCLOG_RETURN_INT(0);
116+    errno = -rc;
117     LIBCLOG_ERROR_RETURN_INT(-1);
118 }
119 
120Index: src/emx/src/lib/sys/b_fsSymlinkModeSet.c
121===================================================================
122--- src/emx/src/lib/sys/b_fsSymlinkModeSet.c    (revision 3803)
123+++ src/emx/src/lib/sys/b_fsSymlinkModeSet.c    (working copy)
124@@ -59,3 +59,28 @@
125     LIBCLOG_ERROR_RETURN_INT(rc);
126 }
127 
128+
129+/**
130+ * Sets the file group/owner of a file.
131+ *
132+ * @returns 0 on success.
133+ * @returns Negative error code (errno.h) on failure.
134+ * @param   fh      Handle to file.
135+ * @param   Mode    The filemode.
136+ */
137+int __libc_Back_fsSymlinkOwnerSet(const char *pszPath, uid_t owner, gid_t group)
138+{
139+    LIBCLOG_ENTER("pszPath=%p:{%s} Owner=%#d Group=%#d\n", (void *)pszPath, pszPath, owner, group);
140+
141+    /*
142+     * Resolve the path.
143+     */
144+    char szNativePath[PATH_MAX];
145+    int rc = __libc_back_fsResolve(pszPath, BACKFS_FLAGS_RESOLVE_FULL_SYMLINK | BACKFS_FLAGS_RESOLVE_DIR_MAYBE, szNativePath, NULL);
146+    if (!rc)
147+        rc = __libc_back_fsNativeFileOwnerSet(szNativePath, owner, group);
148+
149+    if (!rc)
150+        LIBCLOG_RETURN_INT(rc);
151+    LIBCLOG_ERROR_RETURN_INT(rc);
152+}
153Index: src/emx/src/lib/sys/b_fsFileModeSet.c
154===================================================================
155--- src/emx/src/lib/sys/b_fsFileModeSet.c       (revision 3803)
156+++ src/emx/src/lib/sys/b_fsFileModeSet.c       (working copy)
157@@ -59,3 +59,29 @@
158     LIBCLOG_ERROR_RETURN_INT(rc);
159 }
160 
161+
162+/**
163+ * Sets the file group/owner of a file.
164+ *
165+ * @returns 0 on success.
166+ * @returns Negative error code (errno.h) on failure.
167+ * @param   pszPath The path to the file to set the mode of.
168+ * @param   owner    The new owner, if -1 do not change it.
169+ * @param   group    The new group, if -1 do not change it.
170+ */
171+int __libc_Back_fsFileOwnerSet(const char *pszPath, uid_t owner, gid_t group)
172+{
173+    LIBCLOG_ENTER("pszPath=%p:{%s} Owner=%#d Owner=%#d\n", (void *)pszPath, pszPath, owner, group);
174+
175+    /*
176+     * Resolve the path.
177+     */
178+    char szNativePath[PATH_MAX];
179+    int rc = __libc_back_fsResolve(pszPath, BACKFS_FLAGS_RESOLVE_FULL | BACKFS_FLAGS_RESOLVE_DIR_MAYBE, szNativePath, NULL);
180+    if (!rc)
181+        rc = __libc_back_fsNativeFileOwnerSet(szNativePath, owner, group);
182+
183+    if (!rc)
184+        LIBCLOG_RETURN_INT(rc);
185+    LIBCLOG_ERROR_RETURN_INT(rc);
186+}
187Index: src/emx/src/lib/sys/b_fsNativeFileModeSet.c
188===================================================================
189--- src/emx/src/lib/sys/b_fsNativeFileModeSet.c (revision 3803)
190+++ src/emx/src/lib/sys/b_fsNativeFileModeSet.c (working copy)
191@@ -227,3 +227,148 @@
192     LIBCLOG_RETURN_INT(0);
193 }
194 
195+
196+/**
197+ * Sets the file group/owner of a native file.
198+ *
199+ * @returns 0 on success.
200+ * @returns Negative error code (errno.h) on failure.
201+ * @param   pszNativePath   Path to the file to change.
202+ * @param   owner    The new owner, if -1 do not change it.
203+ * @param   group    The new group, if -1 do not change it.
204+ */
205+int __libc_back_fsNativeFileOwnerSet(const char *pszNativePath, uid_t owner, gid_t group)
206+{
207+    LIBCLOG_ENTER("pszNativePath=%p:{%s} Owner=%#d Group=%#d\n", (void *)pszNativePath, pszNativePath, owner, group);
208+    FS_VAR();
209+
210+    /*
211+     * Validate input, refusing named pipes.
212+     */
213+    if (    (pszNativePath[0] == '/' || pszNativePath[0] == '\\')
214+        &&  (pszNativePath[1] == 'p' || pszNativePath[1] == 'P')
215+        &&  (pszNativePath[2] == 'i' || pszNativePath[2] == 'I')
216+        &&  (pszNativePath[3] == 'p' || pszNativePath[3] == 'P')
217+        &&  (pszNativePath[4] == 'e' || pszNativePath[4] == 'E')
218+        &&  (pszNativePath[5] == '/' || pszNativePath[5] == '\\'))
219+        LIBCLOG_ERROR_RETURN_INT(-ENOENT);
220+
221+    /*
222+     * If potential device, then perform real check.
223+     * (Devices are subject to mode in POSIX.)
224+     */
225+    /** @todo copy device check from the path resolver. */
226+
227+    /** @todo YD add right access check */
228+
229+    /*
230+     * This call isn't available in Non-Unix mode - we don't handle symlinks there.
231+     * YD true?
232+     */
233+    if (__predict_false(__libc_gfNoUnix != 0))
234+        LIBCLOG_ERROR_RETURN(-ENOSYS, "ret -ENOSYS - __libc_gfNoUnix=%d\n", __libc_gfNoUnix);
235+
236+    /*
237+     * Do we have UnixEAs on this path?
238+     */
239+    if (__predict_false(!__libc_back_fsInfoSupportUnixEAs(pszNativePath)))
240+        LIBCLOG_ERROR_RETURN(-ENOTSUP, "ret -ENOTSUP - no Unix EAs on '%s'\n", pszNativePath);
241+
242+    /*
243+     * update owner if specified
244+     */
245+    FS_SAVE_LOAD();
246+    int rc;
247+    if (owner != -1)
248+    {
249+            /* construct FEA2 stuff. */
250+            #pragma pack(1)
251+            struct __LIBC_FSUNIXATTRIBSSETMODE
252+            {
253+                ULONG   cbList;
254+                ULONG   off;
255+                BYTE    fEA;
256+                BYTE    cbName;
257+                USHORT  cbValue;
258+                CHAR    szName[sizeof(EA_UID)];
259+                USHORT  usType;
260+                USHORT  cbData;
261+                uint32_t u32Mode;
262+            } EAs =
263+            {
264+                sizeof(EAs), 0, FEA_NEEDEA, sizeof(EA_UID) - 1, sizeof(uint32_t) + 4, EA_UID, EAT_BINARY, sizeof(uint32_t), owner
265+            };
266+            #pragma pack()
267+            EAOP2 EaOp2;
268+            EaOp2.fpGEA2List = NULL;
269+            EaOp2.fpFEA2List = (PFEA2LIST)&EAs;
270+            EaOp2.oError = 0;
271+
272+            /* finally, try update / add the EA. */
273+            rc = DosSetPathInfo((PCSZ)pszNativePath, FIL_QUERYEASIZE, &EaOp2, sizeof(EaOp2), 0);
274+            if (__predict_false(rc != NO_ERROR))
275+            {
276+                LIBCLOG_ERROR("DosSetPathInfo('%s',,,,) -> %d, oError=%#lx\n", pszNativePath, rc, EaOp2.oError);
277+                if (rc == ERROR_EAS_NOT_SUPPORTED)
278+                    rc = 0;
279+                else
280+                    rc = -__libc_native2errno(rc);
281+            }
282+
283+        if (__predict_false(rc != 0))
284+        {
285+            FS_RESTORE();
286+            LIBCLOG_ERROR_RETURN_INT(rc);
287+        }
288+    }
289+   
290+    /*
291+     * update group if specified
292+     */
293+    if (group != -1)
294+    {
295+            /* construct FEA2 stuff. */
296+            #pragma pack(1)
297+            struct __LIBC_FSUNIXATTRIBSSETMODE
298+            {
299+                ULONG   cbList;
300+                ULONG   off;
301+                BYTE    fEA;
302+                BYTE    cbName;
303+                USHORT  cbValue;
304+                CHAR    szName[sizeof(EA_GID)];
305+                USHORT  usType;
306+                USHORT  cbData;
307+                uint32_t u32Mode;
308+            } EAs =
309+            {
310+                sizeof(EAs), 0, FEA_NEEDEA, sizeof(EA_GID) - 1, sizeof(uint32_t) + 4, EA_GID, EAT_BINARY, sizeof(uint32_t), group
311+            };
312+            #pragma pack()
313+            EAOP2 EaOp2;
314+            EaOp2.fpGEA2List = NULL;
315+            EaOp2.fpFEA2List = (PFEA2LIST)&EAs;
316+            EaOp2.oError = 0;
317+
318+            /* finally, try update / add the EA. */
319+            rc = DosSetPathInfo((PCSZ)pszNativePath, FIL_QUERYEASIZE, &EaOp2, sizeof(EaOp2), 0);
320+            if (__predict_false(rc != NO_ERROR))
321+            {
322+                LIBCLOG_ERROR("DosSetPathInfo('%s',,,,) -> %d, oError=%#lx\n", pszNativePath, rc, EaOp2.oError);
323+                if (rc == ERROR_EAS_NOT_SUPPORTED)
324+                    rc = 0;
325+                else
326+                    rc = -__libc_native2errno(rc);
327+            }
328+
329+        if (__predict_false(rc != 0))
330+        {
331+            FS_RESTORE();
332+            LIBCLOG_ERROR_RETURN_INT(rc);
333+        }
334+    }
335+
336+    FS_RESTORE();
337+
338+    LIBCLOG_RETURN_INT(0);
339+}
340Index: src/emx/src/lib/sys/b_fs.h
341===================================================================
342--- src/emx/src/lib/sys/b_fs.h  (revision 3803)
343+++ src/emx/src/lib/sys/b_fs.h  (working copy)
344@@ -388,12 +388,23 @@
345  *
346  * @returns 0 on success.
347  * @returns Negative error code (errno.h) on failure.
348- * @param   fh      Handle to file.
349+ * @param   pszNativePath   Path to the file to change.
350  * @param   Mode    The filemode.
351  */
352 int __libc_back_fsNativeFileModeSet(const char *pszNativePath, mode_t Mode);
353 
354 /**
355+ * Sets the file group/owner of a native file.
356+ *
357+ * @returns 0 on success.
358+ * @returns Negative error code (errno.h) on failure.
359+ * @param   pszNativePath   Path to the file to change.
360+ * @param   owner    The new owner, if -1 do not change it.
361+ * @param   group    The new group, if -1 do not change it.
362+ */
363+int __libc_back_fsNativeFileOwnerSet(const char *pszNativePath, uid_t owner, gid_t group);
364+
365+/**
366  * Sets the file times of a native file.
367  *
368  * @returns 0 on success.
369Index: src/emx/src/lib/sys/b_fsFileModeSetFH.c
370===================================================================
371--- src/emx/src/lib/sys/b_fsFileModeSetFH.c     (revision 3803)
372+++ src/emx/src/lib/sys/b_fsFileModeSetFH.c     (working copy)
373@@ -213,3 +213,161 @@
374     LIBCLOG_RETURN_INT(0);
375 }
376 
377+
378+/**
379+ * Sets the owner/group of a file by filehandle.
380+ *
381+ * @returns 0 on success.
382+ * @returns Negative error code (errno.h) on failure.
383+ * @param   fh      Handle to file.
384+ * @param   owner    The new owner, if -1 do not change it.
385+ * @param   group    The new group, if -1 do not change it.
386+ */
387+int __libc_Back_fsFileOwnerSetFH(int fh, uid_t owner, gid_t group)
388+{
389+    LIBCLOG_ENTER("fh=%d Owner=%#d Group=%#d\n", fh, owner, group);
390+
391+    /*
392+     * Get filehandle.
393+     */
394+    PLIBCFH pFH;
395+    int rc = __libc_FHEx(fh, &pFH);
396+    if (rc)
397+        LIBCLOG_ERROR_RETURN_INT(rc);
398+
399+    /*
400+     * Check the type.
401+     */
402+    switch (pFH->fFlags & __LIBC_FH_TYPEMASK)
403+    {
404+        /* fail */
405+        case F_SOCKET:
406+        case F_PIPE: /* treat as socket for now */
407+            LIBCLOG_ERROR_RETURN_INT(-EINVAL);
408+        /* ignore */
409+        case F_DEV:
410+            LIBCLOG_RETURN_INT(0);
411+
412+        /* use the path access. */
413+        case F_DIR:
414+            if (__predict_false(!pFH->pszNativePath))
415+                LIBCLOG_ERROR_RETURN_INT(-EINVAL);
416+            rc = __libc_back_fsNativeFileOwnerSet(pFH->pszNativePath, owner, group);
417+            if (rc)
418+                LIBCLOG_ERROR_RETURN_INT(rc);
419+            LIBCLOG_RETURN_INT(rc);
420+
421+        /* treat */
422+        default:
423+        case F_FILE:
424+            break;
425+    }
426+
427+    /** @todo YD add right access check */
428+
429+    if (!pFH->pOps)
430+    {
431+        /*
432+         * Standard OS/2 file handle.
433+         */
434+        FS_VAR();
435+        FS_SAVE_LOAD();
436+       /*
437+        * update owner if specified
438+        */
439+       FS_SAVE_LOAD();
440+       int rc;
441+       if (owner != -1)
442+       {
443+               /* construct FEA2 stuff. */
444+               #pragma pack(1)
445+               struct __LIBC_FSUNIXATTRIBSSETMODE
446+               {
447+                   ULONG   cbList;
448+                   ULONG   off;
449+                   BYTE    fEA;
450+                   BYTE    cbName;
451+                   USHORT  cbValue;
452+                   CHAR    szName[sizeof(EA_UID)];
453+                   USHORT  usType;
454+                   USHORT  cbData;
455+                   uint32_t u32Mode;
456+               } EAs =
457+               {
458+                   sizeof(EAs), 0, FEA_NEEDEA, sizeof(EA_UID) - 1, sizeof(uint32_t) + 4, EA_UID, EAT_BINARY, sizeof(uint32_t), owner
459+               };
460+               #pragma pack()
461+               EAOP2 EaOp2;
462+               EaOp2.fpGEA2List = NULL;
463+               EaOp2.fpFEA2List = (PFEA2LIST)&EAs;
464+               EaOp2.oError = 0;
465+   
466+               /* finally, try update / add the EA. */
467+               rc = DosSetFileInfo(fh, FIL_QUERYEASIZE, &EaOp2, sizeof(EaOp2));
468+               if (__predict_false(rc != NO_ERROR))
469+               {
470+                   LIBCLOG_ERROR("DosSetFileInfo('%d',,,,) -> %d, oError=%#lx\n", fh, rc, EaOp2.oError);
471+                   if (rc == ERROR_EAS_NOT_SUPPORTED)
472+                       rc = 0;
473+                   else
474+                       rc = -__libc_native2errno(rc);
475+               }
476+   
477+           if (__predict_false(rc != 0))
478+           {
479+               FS_RESTORE();
480+               LIBCLOG_ERROR_RETURN_INT(rc);
481+           }
482+       }
483+       
484+       /*
485+        * update group if specified
486+        */
487+       if (group != -1)
488+       {
489+               /* construct FEA2 stuff. */
490+               #pragma pack(1)
491+               struct __LIBC_FSUNIXATTRIBSSETMODE
492+               {
493+                   ULONG   cbList;
494+                   ULONG   off;
495+                   BYTE    fEA;
496+                   BYTE    cbName;
497+                   USHORT  cbValue;
498+                   CHAR    szName[sizeof(EA_GID)];
499+                   USHORT  usType;
500+                   USHORT  cbData;
501+                   uint32_t u32Mode;
502+               } EAs =
503+               {
504+                   sizeof(EAs), 0, FEA_NEEDEA, sizeof(EA_GID) - 1, sizeof(uint32_t) + 4, EA_GID, EAT_BINARY, sizeof(uint32_t), group
505+               };
506+               #pragma pack()
507+               EAOP2 EaOp2;
508+               EaOp2.fpGEA2List = NULL;
509+               EaOp2.fpFEA2List = (PFEA2LIST)&EAs;
510+               EaOp2.oError = 0;
511+   
512+               /* finally, try update / add the EA. */
513+               rc = DosSetFileInfo(fh, FIL_QUERYEASIZE, &EaOp2, sizeof(EaOp2));
514+               if (__predict_false(rc != NO_ERROR))
515+               {
516+                   LIBCLOG_ERROR("DosSetFileInfo('%d',,,,) -> %d, oError=%#lx\n", fh, rc, EaOp2.oError);
517+                   if (rc == ERROR_EAS_NOT_SUPPORTED)
518+                       rc = 0;
519+                   else
520+                       rc = -__libc_native2errno(rc);
521+               }
522+   
523+           if (__predict_false(rc != 0))
524+           {
525+               FS_RESTORE();
526+               LIBCLOG_ERROR_RETURN_INT(rc);
527+           }
528+       }
529
530+    FS_RESTORE();
531+    }
532+
533+    LIBCLOG_RETURN_INT(0);
534+}