Welcome to mirror list, hosted at ThFree Co, Russian Federation.

ntea.cc « cygwin « winsup - cygwin.com/git/newlib-cygwin.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 72463feb07f7d6899229c383ba866cee6fc00701 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
/* ntea.cc: code for manipulating NTEA information

   Copyright 1997, 1998, 2000, 2001, 2006 Red Hat, Inc.

   Written by Sergey S. Okhapkin (sos@prospect.com.ru)

This file is part of Cygwin.

This software is a copyrighted work licensed under the terms of the
Cygwin license.  Please consult the file "CYGWIN_LICENSE" for
details. */

#include "winsup.h"
#include <stdlib.h>
#include <ntdef.h>
#include "security.h"
#include "ntdll.h"

/* Default to not using NTEA information */
bool allow_ntea;

/*
 * read_ea - read file's Extended Attribute.
 *
 * Parameters:
 *	file	- pointer to filename
 *	attrname- pointer to EA name (case insensitiv)
 *	attrbuf - pointer to buffer to store EA's value.
 *	len	- length of attrbuf.
 * Return value:
 *	0	- if file or attribute "attrname" not found.
 *	N	- number of bytes stored in attrbuf if success.
 *	-1	- attrbuf too small for EA value.
 */

int __stdcall
read_ea (HANDLE hdl, const char *file, const char *attrname, char *attrbuf,
	 int len)
{
  IO_STATUS_BLOCK io;

  /* Prepare buffer which receives the result. */
  ULONG flen = sizeof (FILE_FULL_EA_INFORMATION) + strlen (attrname)
	       + len + 1;
  PFILE_FULL_EA_INFORMATION fea = (PFILE_FULL_EA_INFORMATION) alloca (flen);
  /* Prepare buffer specifying the EA to search for. */
  ULONG glen = sizeof (FILE_GET_EA_INFORMATION) + strlen (attrname);
  PFILE_GET_EA_INFORMATION gea = (PFILE_GET_EA_INFORMATION) alloca (glen);
  gea->NextEntryOffset = 0;
  gea->EaNameLength = strlen (attrname);
  strcpy (gea->EaName, attrname);

  /* If no incoming hdl is given, the loop only runs once, trying to
     open the file and to query the EA.  If an incoming hdl is given,
     the loop runs twice, first trying to query with the given hdl.
     If this fails it tries to open the file and to query with that
     handle again. */
  HANDLE h = hdl;
  NTSTATUS status = STATUS_SUCCESS;
  int ret = 0;
  while (true)
    {
      if (!hdl && (h = CreateFile (file, FILE_READ_EA,
				   FILE_SHARE_READ | FILE_SHARE_WRITE,
				   &sec_none_nih, OPEN_EXISTING,
				   FILE_FLAG_BACKUP_SEMANTICS, NULL))
		  == INVALID_HANDLE_VALUE)
	{
	  debug_printf ("Opening %s for querying EA %s failed, %E",
	  		file, attrname);
	  goto out;
	}
      status = NtQueryEaFile (h, &io, fea, flen, FALSE, gea, glen, NULL, TRUE);
      if (NT_SUCCESS (status) || !hdl)
        break;
      debug_printf ("1. chance, %x = NtQueryEaFile (%s, %s), Win32 error %d",
		    status, file, attrname, RtlNtStatusToDosError (status));
      hdl = NULL;
    }
  if (!hdl)
    CloseHandle (h);
  if (!NT_SUCCESS (status))
    {
      ret = -1;
      debug_printf ("%x = NtQueryEaFile (%s, %s), Win32 error %d",
		    status, file, attrname, RtlNtStatusToDosError (status));
    }
  if (!fea->EaValueLength)
    ret = 0;
  else
    {
      memcpy (attrbuf, fea->EaName + fea->EaNameLength + 1,
	      fea->EaValueLength);
      ret = fea->EaValueLength;
    }

out:
  debug_printf ("%d = read_ea (%x, %s, %s, %x, %d)", ret, hdl, file, attrname,
  		attrbuf, len);
  return ret;
}

/*
 * write_ea - write file's Extended Attribute.
 *
 * Parameters:
 *	file	- pointer to filename
 *	attrname- pointer to EA name (case insensitiv)
 *	attrbuf	- pointer to buffer with EA value.
 *	len	- length of attrbuf.
 * Return value:
 *	true if success, false otherwice.
 * Note: if len=0 given EA will be deleted.
 */

BOOL __stdcall
write_ea (HANDLE hdl, const char *file, const char *attrname,
	  const char *attrbuf, int len)
{
  IO_STATUS_BLOCK io;

  /* Prepare buffer specifying the EA to write back. */
  ULONG flen = sizeof (FILE_FULL_EA_INFORMATION) + strlen (attrname)
	       + len + 1;
  PFILE_FULL_EA_INFORMATION fea = (PFILE_FULL_EA_INFORMATION) alloca (flen);
  fea->NextEntryOffset = 0;
  fea->Flags = 0;
  fea->EaNameLength = strlen (attrname);
  fea->EaValueLength = len;
  strcpy (fea->EaName, attrname);
  memcpy (fea->EaName + fea->EaNameLength + 1, attrbuf, len);

  /* If no incoming hdl is given, the loop only runs once, trying to
     open the file and to set the EA.  If an incoming hdl is given,
     the loop runs twice, first trying to set the EA with the given hdl.
     If this fails it tries to open the file and to set the EA with that
     handle again. */
  HANDLE h = hdl;
  NTSTATUS status = STATUS_SUCCESS;
  bool ret = false;
  while (true)
    {
      if (!hdl && (h = CreateFile (file, FILE_READ_EA,
				   FILE_SHARE_READ | FILE_SHARE_WRITE,
				   &sec_none_nih, OPEN_EXISTING,
				   FILE_FLAG_BACKUP_SEMANTICS, NULL))
		  == INVALID_HANDLE_VALUE)
	{
	  debug_printf ("Opening %s for setting EA %s failed, %E",
	  		file, attrname);
	  goto out;
	}
      status = NtSetEaFile (h, &io, fea, flen);
      if (NT_SUCCESS (status) || !hdl)
        break;
      debug_printf ("1. chance, %x = NtQueryEaFile (%s, %s), Win32 error %d",
		    status, file, attrname, RtlNtStatusToDosError (status));
      hdl = NULL;
    }
  if (!hdl)
    CloseHandle (h);
  if (!NT_SUCCESS (status))
    debug_printf ("%x = NtQueryEaFile (%s, %s), Win32 error %d",
		  status, file, attrname, RtlNtStatusToDosError (status));
  else
    ret = true;

out:
  debug_printf ("%d = write_ea (%x, %s, %s, %x, %d)", ret, hdl, file, attrname,
  		attrbuf, len);
  return ret;
}