Solved

Example of a C++ file that displays and manipulates file attributes

Posted on 2011-09-28
9
258 Views
Last Modified: 2012-05-12
I am in need of anExample of a C++ program file that displays and/or manipulates file attributes
0
Comment
Question by:jaypappas
  • 4
  • 3
9 Comments
 
LVL 86

Expert Comment

by:jkr
ID: 36718696
File attributes are not primarily a matter of C++, but the underlying operating system. Which one are you targetting and what attributes do you need to manipulate? 'stat()' probably offers the most portable way of reading them, see http://www.minek.com/files/unix_examples/statfile.html ("stat() example") - e.g.
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <time.h>
#include <pwd.h>

void report(char *, struct stat *);

void main(int argc, char * argv[])
/* Print status of files on argument list. */
{
   struct stat status_buf;
   if (argc < 2)
   {
      fprintf(stderr, "statfile file1 ...\n");
      exit(-1);
   }
   while(--argc)
   {
      if( stat(*++argv, &status_buf) < 0 )
         perror(*argv);
      else
         report(*argv, &status_buf);
   }
}

/* Possible access modes... one for each octal value. */
char * accesses[] = {"...", "..x", ".w.", ".wx", "r..", "r.x", "rw.", "rwx"};

void report( char * name, struct stat * buffer)
/* Decode and present the status information. */
{
   int i;
   struct passwd * passent;
   ushort mode = buffer -> st_mode;
   printf("\n %s :\n", name);
   printf(" Last access : %s\n", ctime( &(buffer -> st_atime)) );
   printf(" Last modification : %s\n", ctime( &(buffer -> st_mtime)));
   printf(" Last status change : %s\n", ctime( &(buffer -> st_ctime)));
   printf(" Current file size : %ld \n", buffer -> st_size);

   /* type dev_t is int*/
   printf(" Directory entry is on device %d.\n", buffer -> st_dev);

   /* type ino_t is unsigned  */
   printf(" Inode number is %lu\n", buffer -> st_ino);

   /* Identify the owner by number and by name. */
   passent = getpwuid(buffer -> st_uid);
   if(passent != NULL)
      printf("The owner of the file is #%d - %s\n",
          buffer -> st_uid, passent -> pw_name);
   else
      printf(" The owner of the file is #%d - unknown\n", buffer -> st_uid);

   printf(" Access mode 0%o: ", mode);
   for(i = 6; i >= 0; i -=3)
      printf("%s", accesses[(mode >> i) & 7]);
   printf("\n");
}

Open in new window

0
 

Author Comment

by:jaypappas
ID: 36719876
This is a windows operating sysytem program.  I need to manipulate read and write
0
 
LVL 86

Expert Comment

by:jkr
ID: 36719959
Well - then here you go:
DWORD dwAtt = GetFileAttributes(_T("c:\\path\\file.txt");

// check for 'readonly'
if (dwAtt & FILE_ATTRIBUTE_READONLY) {

  // remove 'readonly'

  dwAtt &= ~FILE_ATTRIBUTE_READONLY;

  SetFileAttributes(_T("c:\\path\\file.txt"),dwAtt);
}

Open in new window

0
 

Author Comment

by:jaypappas
ID: 36720767
Is it a way to print a list of user/groups and what permission a user has on a file
0
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 
LVL 86

Expert Comment

by:jkr
ID: 36769779
OK, so yo uare talking about NTFS ACLs in that case? Yes, there is, but that's a bit more complicated. Could you be a bit more specific?
0
 

Author Comment

by:jaypappas
ID: 36815759
I attached a sample file with a screen print of permissions.  I want to list each user and group permissions and be able to modify permissions on the group and/or user.
file-attr.docx
0
 
LVL 86

Accepted Solution

by:
jkr earned 500 total points
ID: 36818004
Hmm, cannot check your file from here (hint: If it is a screen print, upload a picture, not a Word document. The following article wil lshow you how to check permissions: http://www.codeproject.com/KB/asp/useraccesscheck.aspx ("Check User’s Permissions On A File or Folder"). To grant access to a specific file or folder, you can use the following:
#include <windows.h>
#include <aclapi.h>

#pragma comment(lib,"advapi32.lib")

void main () {

 LPTSTR FileName = "C:\\tmp\\Directory";
 LPTSTR TrusteeName = "User";

 DWORD InheritFlag = SUB_CONTAINERS_AND_OBJECTS_INHERIT;
 ACCESS_MODE option = GRANT_ACCESS;
 EXPLICIT_ACCESS explicitaccess;

 PACL NewAcl = NULL;

 DWORD dwError = 0;

 CreateDirectory ( FileName, NULL);


 BuildExplicitAccessWithName(
       &explicitaccess,
       TrusteeName,
       GENERIC_READ | STANDARD_RIGHTS_READ | GENERIC_WRITE | STANDARD_RIGHTS_READ,
       SET_ACCESS,
       InheritFlag
       );


 //
 // add specified access to the object
 //

 dwError = SetEntriesInAcl(
         1,
         &explicitaccess,
         NULL,
         &NewAcl
         );

 //
 // apply new security to file
 //

 dwError = SetNamedSecurityInfo(
                 FileName,
                 SE_FILE_OBJECT, // object type
                 DACL_SECURITY_INFORMATION,
                 NULL,
                 NULL,
                 NewAcl,
                 NULL
                 );

}

Open in new window

0
 
LVL 40

Expert Comment

by:evilrix
ID: 37419288
This question has been classified as abandoned and is closed as part of the Cleanup Program. See the recommendation for more details.
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

759 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now